Element handles

2 min·Assumes: state, events

A
is a name for one real element on the page.
import { element } from '@markless/core';

export default function SearchBox() @{
	const field = element<HTMLInputElement>();

	<input el={field} type="search" />
}

element() makes the handle field. el={field} binds it to the <input>, so a handler can reach that input.

Reach the real element

A handle is not a copy: in a handler, field is the element itself, or undefined if it isn't on the page.

  • field?.focus() puts the cursor in the real input.
  • The ?. covers the moment the input is gone.

Press Focus search, then type, then flip the switch and press again.

An inked search form: a real text input and a real Focus search button. A dotted pencil line named field runs from the button to the input. Press the button and the cursor blinks in the input. Type in it, it is a real input. The small switch under the input takes it off the page: then the line dangles to undefined, and pressing the button focuses nothing. Escape puts the input back.

A handle reaches the element from a handler. It can't be shown on the page, because the element doesn't exist yet when the page is set up.

A terminal is a good place for it: each button puts the cursor in the real prompt, then writes a command into it.

PlaygroundRestored your edit

Try this
  1. To do: Press Friday deploy. The cursor lands in the real input and the command is in it.
  2. To do: Add a third button that runs run('sudo make it work').
  3. To do: Try breaking it: under the buttons, add <p>{cmd.value}</p>. The build stops, because the input isn't on the page yet when the page is set up.

Pass a handle where HTML wants an id, and Markless writes one id on both elements. It works in:

  • for, so pressing the label focuses the field
  • aria-labelledby, aria-describedby and aria-controls
  • popovertarget

Grandalf the Beige guards this password field. The label, the field and his rules are joined by handles, with no id written anywhere.

PlaygroundRestored your edit

You shall not pass

One capital, one digit, one rune and the answer to a riddle.

Try this
  1. To do: Change the rules to Must contain a dragon., and press Rules again.
  2. To do: Rename the label to Secret word, then press it. The cursor jumps into the field.
  3. To do: Try breaking it: delete el={pass} from the input. The label now points at nothing, and the build stops.

attach runs setup code on the element

attach hands the element to your function, for an observer or an outside library. Return a function, and Markless calls it when the element leaves.

<canvas attach={(canvas) => drawChart(canvas)} onVisible={() => {}} />

On a server-rendered page, attach starts at the first of these:

  • a handler like onClick on the element or inside it, just before it runs
  • onVisible, when the element scrolls into view
  • onIdle, once the page has loaded and the browser is idle

Give element() an array type, like element<HTMLLIElement[]>(), and the handle collects every element bound to it, in page order.

Some values should outlive a reload. Keep them with storage().