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 ¶
- func Blur[F via.Action](fn F, opts ...Option) h.H
- func Change[F via.Action](fn F, opts ...Option) h.H
- func Click[F via.Action](fn F, opts ...Option) h.H
- func DblClick[F via.Action](fn F, opts ...Option) h.H
- func Event[F via.Action](name string, fn F, opts ...Option) h.H
- func Focus[F via.Action](fn F, opts ...Option) h.H
- func Indicator[T any](sig *via.Signal[T]) h.H
- func Input[F via.Action](fn F, opts ...Option) h.H
- func Key[F via.Action](key string, fn F, opts ...Option) h.H
- func Load[F via.Action](fn F, opts ...Option) h.H
- func MouseEnter[F via.Action](fn F, opts ...Option) h.H
- func MouseLeave[F via.Action](fn F, opts ...Option) h.H
- func Submit[F via.Action](fn F, opts ...Option) h.H
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Event ¶
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 Indicator ¶ added in v0.6.0
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 Key ¶
Key binds a keydown handler that fires only when the named key matches. "Enter", "Escape", "ArrowUp", … (W3C key codes).
func Load ¶
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 ¶
MouseEnter binds a mouseenter handler (does not bubble).
func MouseLeave ¶
MouseLeave binds a mouseleave handler (does not bubble).
Types ¶
type Option ¶ added in v0.6.0
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
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 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 SetSignal ¶
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.