on
on="event:name", several separated by ; — on="focus:note;input:note".
Like bind, the attribute holds a name, never code. The event
name is whatever addEventListener accepts, which includes your own custom
events.
<demo-greeter name="stranger">
<label>Name <input on="input:rename"></label>
<p>Hello, <span bind="name">stranger</span>!</p>
</demo-greeter>
hg("demo-greeter", {
attributes: ["name"],
handlers: {
rename(e, el) { el.name = e.target.value || null }
}
});DOM to state goes through a handler you wrote; state to DOM through a bind.
Nothing writes back on its own — clear the field and name becomes null,
which removes the attribute, because rename says so and not because hydrargyri
decided.
The signature
A handler is called as (event, element). The first argument is the ordinary
DOM event, with e.target being the node that was clicked or typed in. The
second is the hydrargyri element that owns the handler — the thing whose state you
are about to change — which is rarely e.target and is the argument that saves
a closest() call in every handler.
Use the second argument, not this. A method on a subclass is called on the
element, so this is the element there — but a handlers entry is called on
the handlers object, so this is that object and this.count is
undefined. The argument is the same value in both cases, and it survives an
arrow function.
How a name resolves
| Order | Where hydrargyri looks |
|---|---|
| 1 | a method you wrote on the element — your HgElement subclass |
| 2 | the handlers object |
| 3 | nowhere: a warning on the first fire |
First match wins, and only one of the two runs — a handlers entry cannot
double-fire behind a subclass method of the same name. An unknown name warns
when the event first fires rather than throwing, so a typo costs you a feature
and not the page.
Step 1 is authored methods only — what your subclass (or the element instance)
defines, never what it inherits from the platform. on="click:remove" with a
handlers.remove entry runs your entry; with none, it warns that the name only
matches the platform's remove() and calls nothing — Element.remove()
detaching the element on a click is not a handler anyone meant. Platform names
are still better avoided: a reader of the markup has to know the resolution
rule to know remove is yours.
data-on
data-on works identically, for markup that must satisfy a validator. Where
both sit on one element the bare form wins.
@window and @document
on="resize@window:relayout" and on="click@document:close" put the listener
on the global instead of the element, for the events that never reach it —
resize fires on window, a click that should close this menu happens
somewhere else entirely. The handler still belongs to the element, still gets
(event, element), and the listener is unhooked on disconnect like every
other one hydrargyri added — the leak that pattern usually costs is the part you
stop writing.
<demo-menu on="click@document:close">
<button on="click:toggle">Menu</button>
<p>The menu is <strong bind="state">closed</strong>. Click anywhere else in
this preview to close it.</p>
</demo-menu>
hg("demo-menu", {
properties: ["state"],
connected(el) {
el.state = "closed";
},
handlers: {
toggle(e, el) {
e.stopPropagation(); // or the click closes what it just opened
el.state = el.state === "open" ? "closed" : "open";
},
close(e, el) {
el.state = "closed";
}
}
});The stopPropagation is the pattern's one moving part, and it is the
platform's, not hydrargyri's: the opening click bubbles to document too, and
without the stop it closes the menu in the same breath.
An @ pointing anywhere else — click@body — warns and is skipped, and the
entry's neighbours still wire. For an event many elements share, a global
listener per element is the wrong shape anyway: one module-scope listener
writing into a reactive() model fans out to every
element holding it, and the elements carry no listeners at all.
Listener options
|passive, |once and |capture — the three
addEventListener options,
spelled after the event — on="touchstart|passive:reveal",
on="click|once:arm", on="focus|capture@document:track", and they stack:
wheel|passive|capture:zoom. |passive is the one that pays rent: a
touchstart, touchmove or wheel listener is one the browser must wait on
before it may scroll, and passive is the author promising not to
preventDefault, which lets the scroll go immediately. |once spends the
listener on its first event; |capture hears it on the way down — and is
unhooked with its flag at disconnect, which is the half a hand-written
removeEventListener forgets.
Options configure the listener; what happens inside the handler stays in the
handler, which is why the list ends there — a |stop or |prevent would be
behaviour in an attribute, and behaviour lives in JS files, where it can be
tested. The pipe is the same separator bind already uses for its formatters,
and not the . other frameworks chose, because an event type is any string
and dotted custom events exist in the wild. An option the grammar does not
know — click|nope:go — warns and skips its pair; the neighbours still wire.
command events
on="command:name" hears
Invoker Commands
like any other event, and a handler keyed by the exact command string
(handlers: { '--add-item': fn }) answers its command directly — see
composition. An element with a
-- key declared warns on a command it does not know; one without stays
silent, on the assumption an on listener like the above is handling them.
static wires
Some pairs are not the author's choice. A class wrapping an <audio> element
needs play, pause and their kin bound on it in every instance — plumbing,
not authorship — and one pair forgotten in the markup is a component that
half-works with nothing saying why. wires moves those pairs into the class:
class MediaThing extends HgElement {
static wires = { 'audio, video': 'play:onPlay;pause:onPause' }
onPlay(e) { /* … */ }
onPause(e) { /* … */ }
}
Each key is a selector, each value the pair grammar on takes — @window and
@document included — and hg() accepts the same under options.wires. At
scan, every node in the element's scope matching the selector gets the
listeners, the element itself included when it matches. A name resolves
exactly as it does from on.
The markup wins where the two meet: a pair the node's own on attribute
already carries is skipped, so markup written before the wires existed keeps
firing once, and a differing pair the author adds rides alongside. A selector
that will not parse warns and is skipped; the other selectors still wire.
Wires are for pairs that are invariantly the element's own. A binding the
author should see, choose or vary belongs in the markup — the wiring being
readable in the page is most of the point here, and wires spend that
visibility. One shape to know: a @document or @window pair registers once
per matched node, so a selector matching three buttons puts three
listeners on the document — aim a global pair at a selector the element
matches once.
What is scanned, and when
Handlers are wired when the element connects, on itself and every descendant
carrying on or data-on — except those inside a nested hydrargyri element, which
owns them instead. static wires land in the same sweep, on the nodes their
selectors match at that moment.
Disconnecting removes every listener hydrargyri added. Re-connecting rescans and
wires them again, so an element moved across the page keeps working and does
not accumulate a second copy of its listeners. Listeners you added in
connected are yours to remove in disconnected.