on

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package on builds reactive event-handler attributes that POST to via actions. It reads at the call site like HTML:

h.Button(h.Text("+"), on.Click(c.Inc))
h.Form(h.Input(...), on.Submit(c.Save))
h.Input(on.Input(c.Filter, on.Debounce("200ms")))
h.Div(on.Key("Enter", c.Send))

Pass a bound method value of signature `func(*via.Ctx) error` or `func(*via.Ctx)` (drop the error when nothing in the body can fail). The method name is resolved via runtime reflection on the closure's PC; the rendered attribute issues a Datastar `@post('/_action/<method>')`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Blur

func Blur[F via.Action](fn F, opts ...Option) h.H

Blur binds a blur handler.

func Change

func Change[F via.Action](fn F, opts ...Option) h.H

Change binds a change handler (e.g. <select>, <input type=checkbox>).

func Click

func Click[F via.Action](fn F, opts ...Option) h.H

Click binds a click handler.

func DblClick

func DblClick[F via.Action](fn F, opts ...Option) h.H

DblClick binds a double-click handler.

func Event

func Event[F via.Action](name string, fn F, opts ...Option) h.H

Event is the escape hatch for any DOM event not covered by a named helper above. Pass the event name as it would appear after `on:` (e.g. "scroll", "wheel", "contextmenu"):

h.Div(on.Event("scroll", p.OnScroll, on.Throttle("100ms")))

name should be a compile-time constant string. The bare-binding cache keys on (event, method) and is never evicted, so deriving name from user input or per-request data would grow the cache unboundedly. The cache is sized correctly when call sites are static — tens to hundreds of bindings for any real app.

func Focus

func Focus[F via.Action](fn F, opts ...Option) h.H

Focus binds a focus handler.

func Indicator added in v0.6.0

func Indicator[T any](sig *via.Signal[T]) h.H

Indicator emits a data-indicator attribute that flips sig to true while an action POST from the same element is in flight and back to false when it settles — drive spinners, aria-busy, or disabled state off sig. Place it alongside the on.* handler on the same element:

h.Button(h.Text("Save"), on.Click(c.Save), on.Indicator(&c.Saving.Signal))

func Input

func Input[F via.Action](fn F, opts ...Option) h.H

Input binds an input handler.

func Key

func Key[F via.Action](key string, fn F, opts ...Option) h.H

Key binds a keydown handler that fires only when the named key matches. "Enter", "Escape", "ArrowUp", … (W3C key codes).

func Load

func Load[F via.Action](fn F, opts ...Option) h.H

Load fires the action once when Datastar evaluates the attribute on the element — useful for kicking off a refresh as soon as a fragment appears in the DOM:

h.Div(on.Load(p.RefreshChart))

func MouseEnter

func MouseEnter[F via.Action](fn F, opts ...Option) h.H

MouseEnter binds a mouseenter handler (does not bubble).

func MouseLeave

func MouseLeave[F via.Action](fn F, opts ...Option) h.H

MouseLeave binds a mouseleave handler (does not bubble).

func Submit

func Submit[F via.Action](fn F, opts ...Option) h.H

Submit binds a form submit handler.

Types

type Option added in v0.6.0

type Option = spec.Option

Option configures a handler's trigger — debounce/throttle timing, DOM modifiers (preventDefault/stopPropagation), or a bundled signal write. Construct one with Debounce, Throttle, Prevent, Stop, or SetSignal and pass it to any handler (Click, Input, …); the set is closed, so there is no user-authored Option. Named here (rather than left as the internal trigger-spec type) so callers can hold and pass option values — e.g. build a []on.Option and spread it into on.Click(fn, opts...).

func Confirm added in v0.6.0

func Confirm(message string) Option

Confirm gates the action behind a browser confirm() dialog: the @post fires only if the user accepts. message is JSON-encoded so arbitrary text is safe inside the generated JS.

func Debounce

func Debounce(d string) Option

Debounce returns a trigger option that debounces firing.

func Once added in v0.6.0

func Once() Option

Once fires the handler at most once, then removes the listener.

func Outside added in v0.6.0

func Outside() Option

Outside fires only when the event originates outside the bound element — the standard click-away pattern for closing menus and popovers.

func Prevent

func Prevent() Option

Prevent calls e.preventDefault() before invoking the action.

func SetSignal

func SetSignal[T any](sig *via.Signal[T], value T) Option

SetSignal bundles a typed signal write into the same trigger as the action — the signal updates client-side first, then the @post fires (and reads the new value):

h.Button(h.Text("Step 5"),
    on.Click(c.Apply, on.SetSignal(&c.Step, 5)),
)

sig must be a Signal[T] handle bound at Mount (any Signal[T] field reached through the composition struct satisfies this). value is JSON-encoded into the rendered JS expression.

func Stop

func Stop() Option

Stop calls e.stopPropagation() before invoking the action.

func Throttle

func Throttle(d string) Option

Throttle returns a trigger option that throttles firing.

func Window added in v0.6.0

func Window() Option

Window attaches the listener to window rather than the element, so the handler fires for the event anywhere on the page (e.g. global shortcuts).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL