A name that points at one real element on the page, once that element exists.In the glossary
is a name for one real element on the page.
import { element } from '@markless/core';export default functionSearchBoxfunction SearchBox(): Element() @{@{ · Statement containerHolds instructions: setup lines, then the element to show. constfieldconst field: HTMLInputElement | undefined =elementelement · Element handleCreates a handle that el={…} points at an element. In handlers and attach the handle is that element, or undefined while it is not on the page.<HTMLInputElementinterface HTMLInputElementThe HTMLInputElement interface provides special properties and methods for manipulating the options, layout, and presentation of input elements.>(); <input<input> · HTML elementA field a person types in or picks a value with.elel · Element bindingPoints an element() handle at this element, so handlers and attach can reach it.={field} type(property) type?: string | undefined="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.
fieldfield.focus()
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.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { element, state } from '@markless/core';import { typeIn } from './keys.ts';export default function Terminal() @{ const cmd = element<HTMLInputElement>(); let typed = state(false); function run(command: string) { cmd?.focus();typeIn(cmd, command);typed = true; } const deploy = () => run('git push --force'); const fix = () => run('rm -rf node_modules'); <sectionclass="shell"> <labelfor={cmd}>$</label> <inputel={cmd} autocomplete="off" /> <buttononClick={deploy}> Friday deploy </button> <buttononClick={fix}>Fix everything</button> <p> {typed ? 'Typed into the real input' : ''} </p> </section>}
type Input = HTMLInputElement | undefined;export function typeIn( input: Input, text: string,) { if (input) input.value = text;}
To do: Press Friday deploy. The cursor lands in the real input and the command is in it.
To do: Add a third button that runs run('sudo make it work').
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.
A handle links elements without an id
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.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { element } from '@markless/core';export default function Gate() @{ const pass = element<HTMLInputElement>(); const rules = element<HTMLParagraphElement>(); <sectionclass="gate"> <h2>You shall not pass</h2> <labelfor={pass}>Password</label> <inputel={pass}type="password" aria-describedby={rules} /> <buttonpopovertarget={rules}>Rules</button> <pel={rules} popover="auto"> One capital, one digit, one rune and the answer to a riddle. </p> </section>}
One capital, one digit, one rune and the answer to a riddle.
Try this
To do: Change the rules to Must contain a dragon., and press Rules again.
To do: Rename the label to Secret word, then press it. The cursor jumps into the field.
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<canvas> · HTML elementA surface your code draws pixels on.attachattach · Element setupRuns your function with this element, for setup like an observer. A function it returns runs when the element leaves the page. On a server-rendered page it waits until a handler on the element runs, or onVisible or onIdle fires.={(canvas(parameter) canvas: HTMLCanvasElement) =>drawChartimport drawChart(canvas)} onVisibleonVisible · Visibility handlerRuns once, the first time this element scrolls into view.={() => {}} />
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.