qat

Events: modifiers, gestures, events as signals

One syntax for every handler: on:event|modifiers={handler}, on elements and components, the same in .qx and .tsx. Native events pass through unchanged; there is no synthetic event system. What the compiler can work out from literals it compiles inline. The rest comes from @qatjs/core/events, and only the helpers a file uses are imported. Every helper together costs 397 B over the core, and the core itself has none of them.

Modifiers

.qx10 lines, 2 reactive namesOpen in the playground
export default function Search() {
  let q = state('')
  let open = state(false)
  return (
    <form on:submit|prevent={() => (open = false)}>
      <input aria-label="Search" on:input|debounce:300={(e) => (q = e.currentTarget.value)} on:keydown|key:Ctrl+K|prevent={() => (open = true)} />
      <ul on:scroll|throttle:100|passive={(e) => (q = String(e.currentTarget.scrollTop))} />
    </form>
  )
}
q stateopen state
Modifier Meaning
|prevent |stop |self |once |capture |passive as before, chained in any order
|debounce:300 one call, 300 ms after the last event
|throttle:100 at most one call per 100 ms: the first at once, the last one kept
|key:Enter, |key:Ctrl+K that KeyboardEvent.key, with exactly those of Ctrl, Shift, Alt and Meta held (Space is the space bar)
|long:800 on on:long: how long the pointer is held (500 ms without it)
|threshold:4 on on:drag: the pixels the pointer moves before the drag starts (0 without it: at once). A press that moves less is a click, and no drag event fires. It compiles to a test inline, so @qatjs/core/events does not grow
  • Parameters are literals and the compiler checks them. Milliseconds are positive whole numbers, and a key must be a KeyboardEvent.key name or a single character.
  • There are no defaults. |debounce alone is an error, and its fix-it writes |debounce:300 for you to change.
  • |debounce and |throttle on one handler is an error.
  • watch(x, fn, { debounce: 300 }) counts the same 300 ms the same way.
  • When a timed handler runs, e.currentTarget is still the element.
  • Checks run at the event. |prevent and |stop apply at the event itself, and only the handler waits.

Two warnings, never a silent default

Code When Fix-it
QX-EVENT-UNDEBOUNCED an on:input/on:keyup handler that calls fetch or load, reloads a load, or writes a state some load reads adds |debounce:300
QX-EVENT-UNTHROTTLED an on:scroll/on:resize/on:pointermove handler that measures layout (scrollTop, getBoundingClientRect(), …) or writes more than one signal adds |throttle:100

The framework never debounces or throttles on its own: the warning tells you, and the number is yours.

The handler: (e, el, value)

.qx10 lines, 2 reactive namesOpen in the playground
export default function Form() {
  let qty = state(1)
  let note = state('')
  return (
    <form>
      <input type="number" aria-label="Quantity" on:input={(e, el, v) => (qty = v)} />
      <textarea aria-label="Note" on:input|debounce:300={(e, el, v) => (note = v)} />
    </form>
  )
}
qty statenote state

A handler that declares more than the event also gets the element and, for input and change, the field's value:

  • el is the element the attribute is on. The compiler binds it, so it is never currentTarget, and it is the same after a |debounce as at the event.
  • value is typed by the field. A number or range input gives a number; a checkbox or radio gives checked, a boolean; a file input gives files; a <select multiple> gives the selected values; everything else gives the string.
  • A handler that takes only e gets only e, and nothing is added to the output.
  • A component's event passes its payload as the handler's argument, as before: <Picker on:pick={(value) => …} />. There is no element there to bind.

Gestures

.qx11 lines, 2 reactive namesOpen in the playground
export default function Card() {
  let open = state(false)
  let x = state(0)
  return (
    <div class="card" on:outside={() => (open = false)} on:escape={() => (open = false)}>
      <img alt="" src="/a.png" on:doubletap={() => (open = !open)} on:swipe:left={() => x--} on:swipe:right={() => x++} />
      <p on:long={() => (open = true)}>Hold for the menu</p>
      <div class="handle" on:drag={(d) => d.phase === 'move' && (x = d.dx)} />
    </div>
  )
}
open statex state
Gesture Fires
on:outside a pointerdown outside the element (dropdowns, dialogs)
on:escape, on:enter keydown of that key, as on:keydown|key:Escape
on:long the pointer held 500 ms (on:long|long:800 for another time); moving does not end it
on:swipe:left :right :up :down more than 30 px, mostly along that axis
on:doubletap two taps within 300 ms
on:drag one stream { dx, dy, phase: 'start' | 'move' | 'end', event }, the pointer captured

on:drag is the pointer gesture; HTML drag and drop (a draggable element dropped elsewhere) is on:dragstart, on:dragover, on:drop. In development, two mounted handlers of one |key: combination are a console warning that names both components.

Events as signals

export default function Header() {
  let scrolled = on:scroll|throttle:100(window, () => scrollY > 100)
  let online = on:online(window, () => true, on:offline(window, () => false))
  let pos = on:pointermove(window, (e) => [e.x, e.y])
  return <header class:compact={scrolled}>{online === false ? 'offline' : ''} {pos?.[0]}</header>
}

on:<event>(target, map) is a value that updates on the event: what map returns for the last one, undefined before the first. It is read like a derived value, and its listener goes with the component.

  • A third argument is another event-signal to write: one value, two events.
  • Modifiers: |debounce, |throttle and |key apply. It reads the event and does not handle it, so |prevent is an error.
  • An object key with the name on keeps a space after the colon: { on: x }.

Forwarding: on:*

export default function Button(props) {
  return <button class="btn" on:*={props}>{props.label}</button>
}

Every on:<event> handler the component received goes onto the element. Its types are the element's events: a handler for the wrong event is a type error.

React's names

onClick, onInput, onKeyDown and the rest are accepted on elements and compile to on:click, on:input, on:keydown (onDoubleClick is on:dblclick). They take no modifiers: write the on: form for those. On a component, onClick is an ordinary prop. qatjs migrate leaves them as they are, except onChange on a text field, which is React's every-keystroke event: on:input.

In .tsx

.qx .tsx
on:input|debounce:300={fn} on:input={withModifiers(fn, ['debounce:300'])}
on:swipe:left={fn} on:swipe-left={fn} (a TSX name has one colon)
on:*={props} on:_={props}
let y = on:scroll(window, map) const y = listen(window, 'scroll', map) from @qatjs/core/events (y() reads it)
.tsx8 lines, 1 reactive name
import { state, withModifiers } from '@qatjs/core'
import { listen } from '@qatjs/core/events'

export default function Search() {
  const q = state('')
  const y = listen(window, 'scroll', () => scrollY)
  return <input aria-label="Search" data-y={y()} on:input={withModifiers((e) => q.set(e.currentTarget.value), ['debounce:300'])} />
}
q state