Elements

3 min·Assumes: state, events

Focus an input, play a video or open a modal from your code.

import { element } from '@markless/core';

export default function Lesson() @{
	const clip = element<HTMLVideoElement>();

	<section>
		<h1>How to fold a fitted sheet</h1>
		<video el={clip} src="/fold.mp4" />
		<button onClick={() => clip.play()}>
			Play part 1 of 14
		</button>
	</section>
}

The button starts the video by calling the play() function on the <video> element. element() makes the variable clip, and el={clip} points it at the <video>.

Call any function the browser gives an element

Any function the browser gives an element works this way: focus() on an input, play() on an <audio>, animate() on anything.

Tap the girl to swing. Each swing calls animate() on the stick and on the piñata.

PlaygroundRestored your edit

Five hits to break it.

Try this
  1. To do: Swing five times. The piñata bursts, then swing again at the empty rope.
  2. To do: Make it swing harder: in swing.ts, change 18deg to 40deg.

An element inside an @if can be missing

An element inside an @if isn't always on the page. Add | undefined to its type and write ?. before the function, and a call while the element is gone does nothing. Leave out | undefined and the build tells you.

const note = element<HTMLTextAreaElement | undefined>();

@if (writing) {
	<textarea el={note} />
}
<button onClick={() => note?.focus()}>
	Back to my letter
</button>

An element that is always on the page needs neither: clip.play().

Drag down on the stick to bend it until it snaps, then try to bend it again.

An ink drawing seen through your own eyes: your two fists hold a knobbly willow stick over a wooden table, thumbs pressed together in the middle. Beyond them lie the open red and cream packet that says One wish, and a mug of tea. Drag down on the stick, or hold the down arrow, to bend it until it snaps: your wish comes true and a frog in a tiny crown sits up in your tea. Your hands are left holding two splintered halves; bend again and they close on nothing. Escape puts a fresh stick in your hands.

Find out where an element is

The variable is the real DOM element, so anything the browser lets you do with an element works. Here getBoundingClientRect() tells each press where the ball and the flipper are.

Pull the
, then press a
as the ball reaches it.
PlaygroundRestored your edit
0Ball 1
Try this
  1. To do: Pull the plunger back and let go: the longer you hold it, the harder it shoots. Then hit the ball with a flipper as it comes down. A hit scores +500.
  2. To do: Make a flipper hit worth more: in pinball.tsrx, change scored(500) to scored(5000), then hit the ball again.

Connect a label to its input

A label has to know which input it belongs to. Put the same variable on both:

<label for={rain}>Rain</label>
<input el={rain} type="range" />

That connects them. You never write an id.

Press a word on the mixer, then move its slider with the arrow keys.

PlaygroundRestored your edit

Quiet.

Try this
  1. To do: Press the word Cat, then hold the up arrow. Her cat starts to purr.
  2. To do: Make the cat a label too: in study-stream.tsrx, change <i class="cat" to <label class="cat" for={cat}, then press the cat.

Get every element in a list

An @for makes many elements. To get all of them, add [] to the type:

const fans = element<HTMLButtonElement[]>();

@for (const fan of crowd; key fan.seat) {
	<button el={fans} />
}

Now fans is an array of every button, in the order they are on the page.

Tap any fan to start a wave.

PlaygroundRestored your edit

Nobody started the wave.

Try this
  1. To do: Tap Diego, the last fan in the front row. The wave rolls all the way to the other end.
  2. To do: Make the wave slower: in wave.ts, change 70 to 200, then tap a fan in the middle.
What if code needs the element as soon as it appears?

Give the element attach and a function. Markless calls it with the element the first time the element is used: an event on it or inside it, onVisible, or onIdle. An element an @if shows later gets it straight away. Return a function, and Markless calls that one when the element leaves the page.

<canvas attach={(canvas) => drawChart(canvas)} onVisible={() => {}} />
Coming from React?
  • element() is useRef without .current: field is the element itself.
  • No ?. unless the element can be missing: an element that is always on the page is never null.
  • Write el={field} where React writes ref={field}.
  • There is no forwardRef. Pass the variable as an ordinary prop.
  • There is no useId. Put the variable in for or aria-describedby and Markless writes the id.

Next, split a page into components.

Button sounds are from Josh Comeau's Button Sounds library.