# Full Documentation Archive for Hydrargyri This file contains the complete Markdown documentation for Hydrargyri. > Reactive web components in the light DOM — declarative binds and handlers on markup you already wrote. Below is every documentation page of the `hydrargyri` library, concatenated in full: the API reference, the two declarative attributes, composition between elements, and the deliberate limits. Hydrargyri defines reactive custom elements in the light DOM — no shadow roots, no build step, no expression language. `bind` and `on` hold names, never code, and the markup an author wrote is what renders before, during and after the script. Code samples are authoritative for the current release. Repository and issues: . The link index for this site is at . --- # `on` URL: https://stamat.github.io/hydrargyri/docs/on.html `on="event:name"`, several separated by `;` — `on="focus:note;input:note"`. Like [`bind`](bind.html), the attribute holds a **name**, never code. The event name is whatever `addEventListener` accepts, which includes your own custom events. ```html

Hello, stranger!

``` ```js demo 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. ```html

The menu is closed. Click anywhere else in this preview to close it.

``` ```js demo 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()`](api.html#reactivemodel) 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](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/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](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) like any other event, and a handler keyed by the exact command string (`handlers: { '--add-item': fn }`) answers its command directly — see [composition](composition.html#no-common-ancestor-at-all). 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 `