event()
expressionsince 0.9.0requires runtimeInside a mutation of an :on() rule: one field of the current event, such as event(local-x), event(key) or event(value), read once as a typed value.
On this page · 10 sections+−
Syntax
--<state>: set(event(<field>));registry syntax: event(field)
Parameters
- fieldone of 32 field names (see Behavior)required
- Checked at compile time against the rule’s trigger. A field that the trigger does not provide is a compile error that lists the fields it does provide.
Return value
A typed value: px lengths for coordinates and distances, numbers, booleans or strings. A field with no value for this event, such as value-as-number of a field that holds no number, writes nothing.
Example
.pad { --x: state(0px); --y: state(0px); --key: state("none");}/* A snapshot of where the press happened, relative to the pad. */.pad:on(pointerdown) { --x: set(event(local-x)); --y: set(event(local-y));}/* Keyboard fields exist only for keyboard triggers. */.pad:on(keydown) { --key: set(event(key));}.pad .dot { translate: var(--x) var(--y); }.pad .x { content: var(--x); }.pad .y { content: var(--y); }.pad .k { content: var(--key); }state()Function · hover any red tokenMakes each matching element the owner of an independent state with an initial value.Reference →
Resultfocusable pad, pointer and keyboard
waiting for interaction
Correct and incorrect usage
correctRecord where a press happened
pad.cssreactive
.pad { --x: state(0px); --y: state(0px); }.pad:on(pointerdown) { --x: set(event(local-x)); --y: set(event(local-y));}.pad .dot { translate: var(--x) var(--y); }The values are read once, at the press. The dot stays where the press happened until the next one.
correctCopy a field’s value to a shared owner
search.cssreactive
.search { --q: state(""); }.search input:on(input) { --q: set(event(value)); }.search .echo { content: var(--q); }The state lives on .search, so the field and the .echo element both see it.
incorrectA keyboard field on a click
pad.cssreactive
.pad { --key: state(""); }.pad:on(click) { --key: set(event(key)); }A compile error: click has no key. The message lists the fields that click provides.
Behavior
- Release status. The 0.9.0 release recommendation (hardening report, section H) lists the pointer, keyboard and form fields as stable, so this page does not mark
event()experimental. The drag fields stay experimental. The runtime’sdescribe()still reports the whole function asexperimental. - A snapshot, not a JavaScript `Event`.
event()copies one value when the event fires. There is no property access, notargetorcurrentTarget, and no script. - General, every trigger:
type, the DOM event type (dragfor drag phases)..log:on(click) { --last: set(event(type)); } - Pointer, for
click,contextmenu, thepointer*events, themouse*events anddblclick:client-xandclient-y(viewport),page-xandpage-y(document),local-xandlocal-y(from the border box of the element the rule matched),button,buttons,alt-key,ctrl-key,shift-keyandmeta-key.click,contextmenuand thepointer*events also givepointer-id,pressureandpointer-type(mouse,penortouch); themouse*events anddblclickdo not..pad:on(pointerdown) { --x: set(event(local-x)); } - Keyboard, for
keydownandkeyup:key,code,repeatand the four modifier keys..pad:on(keydown) { --key: set(event(key)); } - Form, for
inputandchange:value(a string),value-as-numberandchecked, read fromevent.target, which must be an input, select or textarea. Password, one-time-code and payment fields give""..signup input:on(input) { --greeting: set(event(value)); } - Drag (experimental), for
:on(drag)and its phases, in addition to the pointer fields:xandy(the mutated state at drag start plus the distance moved),delta-xanddelta-y(total distance since the start),movement-xandmovement-y(since the previous drag event), andstart-xandstart-y(viewport coordinates of the press)..card:on(drag) { --x: set(event(x)); } - Scroll, for
scrollandscrollend:scroll-topandscroll-leftof the element the rule matched..feed:on(scroll) { --top: set(event(scroll-top)); } - Focus, submit, reset, toggle, animation and transition triggers provide only
type.outside(…)provides the fields of its event. - Compared with other functions. Outside mutations,
mouse(x)is a live value that follows the pointer; inside a mutation it is the event’s client coordinate, andmouse(x, local)isevent(local-x).element(width)in a mutation is the border-box width of the element the rule matched, at the event, andattr(data-id)reads that element’s attribute at the event. Keys, form values and drag distances exist only throughevent().
Supported contexts
- Only inside
set(),increment()anddecrement()of a mutation in an:on()rule. Elsewhere there is no event: Reactive CSS does not evaluateevent(), and the browser ignores the declaration as invalid. - Combines with the typed math of mutations:
set(clamp(0px, event(x), 200px)),set(round(event(local-x), 10px)).
Limitations
- Only the fields listed here.
target,currentTarget,detailand other event properties are not exposed. valueandcheckedread the control the event happened on. A bubbling:on(input)rule on an ancestor reads whichever field changed.- Coordinates are px. They need a pointer or drag trigger;
mouse()in a mutation has the same rule.
Examples
- Click coordinates · API examples, runs the real runtime
- Draggable card · API examples, runs the real runtime
- Form mirror · API examples, runs the real runtime
Related
Experimental · syntax may change between minor versions