9:41
Messages
Lunch at one?
On my way!
I'll get a table
Type a word, a function name or an error code.
Opening the index…
The search index did not load. Type again to retry.
Nothing in the docs matches “”.
↑↓ to moveenter to goesc to close
4 min·Assumes: statestate, eventsevents, computedcomputed, conditionalsconditionals
function Avatarfunction Avatar({ name }: { name: string; }): Element({ name(parameter) name: string }: { name(property) name: string: string }) @{@{ · Statement containerHolds instructions: setup lines, then the element to show.
<img<img> · HTML elementAn image.
src(property) src?: string | undefined={`/faces/${name(parameter) name: string}.jpg`}
alt(property) alt?: string | undefined={`${name(parameter) name: string}'s profile picture`}
/>
}
export default function Postfunction Post(): Element() @{@{ · Statement containerHolds instructions: setup lines, then the element to show.
<article<article> · HTML elementA piece of content that stands on its own, like a post or a card.>
<h1<h1> · HTML elementThe top-level heading.>Why our standup takes an hour</h1<h1> · HTML elementThe top-level heading.>
<p<p> · HTML elementA paragraph.>Written by</p<p> · HTML elementA paragraph.>
<Avatarfunction Avatar({ name }: { name: string; }): Element name(property) name: string="Priya" />
<Avatarfunction Avatar({ name }: { name: string; }): Element name(property) name: string="Tom" />
<Avatarfunction Avatar({ name }: { name: string; }): Element name(property) name: string="Ines" />
</article<article> · HTML elementA piece of content that stands on its own, like a post or a card.>
}Avatar is the component here. Use it like a tag, <Avatar />, once for each author. name="Tom" passes it a value, called a prop.
State inside a component belongs to that one copy. Use <Toy /> three times and each toy counts its own turns.
Turn the robot's pink key clockwise, then let go. Only the robot walks.
Toy is in its own file, toy.tsrx. Import it wherever you want to use it.
import Toy from './toy.tsrx';export default function Nursery() @{ <section class="nursery"> <Toy kind="robot" /> <Toy kind="mouse" /> <Toy kind="frog" /> </section>}
import { state } from '@markless/core';import * as wind from './wind.ts';type ToyProps = { kind: string };export default function Toy({ kind }: ToyProps) @{ let turns = state(0); let steps = state(0); <div class="toy" data-kind={kind} data-wound={turns > 0} style={'--steps:' + steps} > <i class="tin" onAnimationIteration={() => { turns--; steps = wind.forward(steps); }} /> <button class="key" aria-label={'Key of the ' + kind} onPointerDown={wind.grab} onPointerMove={(event) => (turns = wind.turn(event, turns))} onPointerUp={wind.drop} onPointerCancel={wind.drop} onKeyDown={(event) => (turns = wind.key(event, turns))} /> <p class="tag">Turns: {turns}</p> </div>}
// A spring holds eight turns at most, and each// turn walks the toy one step.export const most = 8;// Where the key's centre was when it was taken// hold of, and how far it has turned since.type Grip = { x: number; y: number; angle: number; turned: number;};const grips = new WeakMap<HTMLElement, Grip>();const shown = new WeakMap<HTMLElement, number>();export const forward = (steps: number) => Math.min(most, steps + 1);function angle( event: PointerEvent, { x, y }: { x: number; y: number },) { const dx = event.clientX - x; const dy = event.clientY - y; return (Math.atan2(dy, dx) * 180) / Math.PI;}function spin(el: HTMLElement, degrees: number) { const turned = (shown.get(el) ?? 0) + degrees; shown.set(el, turned); el.style.rotate = `${turned}deg`;}export function grab(event: PointerEvent) { const el = event.currentTarget; if (!(el instanceof HTMLElement)) return; el.setPointerCapture(event.pointerId); el.classList.add('held'); const { left, top, width, height } = el.getBoundingClientRect(); const centre = { x: left + width / 2, y: top + height / 2, }; grips.set(el, { ...centre, angle: angle(event, centre), turned: 0, });}// Clockwise only: a full turn winds it once.export function turn( event: PointerEvent, turns: number,) { const el = event.currentTarget; if (!(el instanceof HTMLElement)) return turns; const grip = grips.get(el); if (!grip) return turns; if (!event.buttons) { drop(event); return turns; } const now = angle(event, grip); const step = ((now - grip.angle + 540) % 360) - 180; grip.angle = now; if (step <= 0 || turns >= most) return turns; spin(el, step); grip.turned += step; if (grip.turned < 360) return turns; grip.turned -= 360; return turns + 1;}export function drop(event: PointerEvent) { const el = event.currentTarget; if (!(el instanceof HTMLElement)) return; grips.delete(el); el.classList.remove('held');}// The right or up arrow turns the key once.export function key( event: KeyboardEvent, turns: number,) { if ( event.key !== 'ArrowRight' && event.key !== 'ArrowUp' ) return turns; event.preventDefault(); const el = event.currentTarget; if (!(el instanceof HTMLElement)) return turns; if (turns >= most) return turns; spin(el, 360); return turns + 1;}
/* A lane of floor is 200 x 80 units of --u. */.nursery { --u: calc(100cqw / 200); container-type: inline-size; width: 100%; max-width: 28rem; margin: 0 auto; padding-top: 23%; background: 0 0 / 100% no-repeat url(/examples/ink/cp-toys-wall.svg); font-family: 'Shantell Sans', sans-serif; user-select: none;}.nursery .toy { --row: 0; --hop: 1.5; --key-x: 18; --key-y: 40; --at: calc(46 + var(--steps) * 7); --room: min(1, 8 - var(--steps)); position: relative; height: calc(80 * var(--u)); margin: 0; background: 0 0 / 100% no-repeat url(/examples/ink/cp-toys-lane.svg);}.nursery [data-kind='mouse'] { --row: 1; --hop: 0.6; --key-x: 16; --key-y: 46;}.nursery [data-kind='frog'] { --row: 2; --hop: 5; --key-x: 18; --key-y: 44;}/* The letter block at the end of the lane. */.nursery .toy::after { content: ''; position: absolute; top: calc(44 * var(--u)); left: calc(174 * var(--u)); width: calc(24 * var(--u)); height: calc(24 * var(--u)); background: url(/examples/ink/cp-toys-blocks.svg) 0 0 / 300% 100% no-repeat; background-position: calc(var(--row) * 50%) 0;}/* A pencil note until any key turns. */.nursery [data-kind='robot']::before { content: 'wind\A me'; position: absolute; top: calc((var(--key-y) - 24) * var(--u)); left: calc( (var(--at) + var(--key-x) - 56) * var(--u) ); width: calc(60 * var(--u)); height: calc(40 * var(--u)); padding: calc(13 * var(--u)) 0 0 calc(5 * var(--u)); box-sizing: border-box; background: 0 0 / 100% no-repeat url(/examples/ink/cp-toys-wind.svg); color: color-mix( in srgb, currentColor 62%, #0000 ); font-size: max(10px, calc(8.5 * var(--u))); line-height: 1.05; white-space: pre; pointer-events: none;}.nursery:has(.key[style]) .toy::before { content: none;}/* The toy, its key and its tag move as one. */.nursery .tin,.nursery .key,.nursery .tag { position: absolute; margin: 0;}.nursery .tin { top: 0; left: calc(var(--at) * var(--u)); width: calc(72 * var(--u)); height: calc(72 * var(--u)); background: url(/examples/ink/cp-toys-toys.svg) 0 0 / 200% 300% no-repeat; background-position: 0 calc(var(--row) * 50%);}.nursery .key { top: calc((var(--key-y) - 13) * var(--u)); left: calc( (var(--at) + var(--key-x) - 13) * var(--u) ); width: calc(26 * var(--u)); height: calc(26 * var(--u)); padding: 0; border: 0; border-radius: 50%; background: 0 0 / 100% no-repeat url(/examples/ink/cp-toys-key.svg); cursor: grab; touch-action: none;}/* A wider grip than the drawing, for a thumb. */.nursery .key::before { content: ''; position: absolute; inset: -10px; border-radius: 50%;}.nursery .key:focus-visible { outline: 2px dashed; outline-offset: 2px;}.nursery .held { cursor: grabbing;}.nursery .tag { top: calc(66 * var(--u)); left: calc((var(--at) + 18) * var(--u)); width: calc(50 * var(--u)); height: calc(14 * var(--u)); padding-left: calc(9 * var(--u)); box-sizing: border-box; background: 0 0 / 100% no-repeat url(/examples/ink/cp-toys-tag.svg); color: #2d2a24; font-size: calc(8.4 * var(--u)); font-weight: 700; line-height: calc(14 * var(--u)); white-space: nowrap;}/* Wound, it walks a step a beat until the spring runs down; held, it waits. */.nursery [data-wound='true'] .tin { animation: nursery-beat 0.3s linear infinite;}@media (prefers-reduced-motion: no-preference) { .nursery [data-wound='true'] .tin { animation-name: nursery-step; } .nursery [data-wound='true'] .tag { animation: nursery-carry 0.3s linear infinite; } .nursery [data-wound='true'] .key { animation: nursery-carry 0.3s linear infinite, nursery-unwind 0.3s linear infinite; } .nursery .key:focus-visible { transition: rotate 0.2s ease-out; }}/* After the shorthands above, or they restart. */.nursery .toy:has(.held) > * { animation-play-state: paused;}@keyframes nursery-beat { to { opacity: 1; }}@keyframes nursery-step { 0%, 49.9% { background-position-x: 0; } 25% { translate: calc(var(--room) * 1.75 * var(--u)) calc(var(--hop) * -1 * var(--u)); } 50%, 100% { background-position-x: 100%; } 50% { translate: calc(var(--room) * 3.5 * var(--u)) 0; } 75% { translate: calc(var(--room) * 5.25 * var(--u)) calc(var(--hop) * -1 * var(--u)); } 100% { translate: calc(var(--room) * 7 * var(--u)) 0; }}@keyframes nursery-carry { 25% { translate: calc(var(--room) * 1.75 * var(--u)) calc(var(--hop) * -1 * var(--u)); } 50% { translate: calc(var(--room) * 3.5 * var(--u)) 0; } 75% { translate: calc(var(--room) * 5.25 * var(--u)) calc(var(--hop) * -1 * var(--u)); } 100% { translate: calc(var(--room) * 7 * var(--u)) 0; }}@keyframes nursery-unwind { to { transform: rotate(-360deg); }}
Turns: 0
Turns: 0
Turns: 0
<Toy kind="frog" /> below the first. The new frog has its own key and its own turns.Pass state to a component like any other value. Change it once, and every spot that shows it changes. No component runs again.
Tap Biscuit's photo under Switch account.
import { state } from '@markless/core';import Avatar from './avatar.tsrx';import { pick } from './accounts.ts';export default function Feed() @{ let me = state('Robin'); <section class="feed"> <header class="bar"> <Avatar name={me} /> </header> <div class="card"> <Avatar name={me} /> </div> <p class="comment"> <Avatar name={me} /> <b>{me}</b> Great view! </p> <nav aria-label="Switch account" onClick={(event) => (me = pick(event, me))} > <button value="Robin" aria-label="Robin" /> <button value="Mo" aria-label="Mo" /> <button value="Biscuit" aria-label="Biscuit" /> </nav> </section>}
import { photoOf } from './accounts.ts';type AvatarProps = { name: string };export default function Avatar({ name,}: AvatarProps) @{ <img class="avatar" src={photoOf(name)} alt={name + "'s photo"} />}
const folder = '/examples/ink/components-live3-';// The account whose photo was tapped, or the one// already signed in.export function pick( event: MouseEvent, me: string,) { const button = event.target; if (!(button instanceof HTMLButtonElement)) { return me; } return button.value;}export const photoOf = (name: string) => folder + name.toLowerCase() + '.svg';
/* The app is 240 x 200 units of --u: the window above, the switch-account tray under it. */.feed { --u: calc(100cqw / 240); container-type: inline-size; position: relative; width: 100%; max-width: 34rem; margin: 0 auto; padding-top: calc(100% * 200 / 240); background: 0 0 / 100% no-repeat url(/examples/ink/components-live3-feed.svg); color: light-dark(#2d2a24, #ebe2cc); font-family: 'Shantell Sans', sans-serif; user-select: none;}.feed .bar,.feed .card,.feed .comment,.feed nav { position: absolute; margin: 0;}.feed .avatar { display: block; width: 100%; aspect-ratio: 1; border-radius: 50%;}.feed .bar { top: calc(5.5 * var(--u)); left: calc(210 * var(--u)); width: calc(19 * var(--u));}.feed .card { top: calc(41 * var(--u)); left: calc(21 * var(--u)); width: calc(66 * var(--u));}/* One placed straight in the app is a tag on the lake photo. */.feed > .avatar { position: absolute; top: calc(52 * var(--u)); left: calc(178 * var(--u)); width: calc(26 * var(--u));}/* The avatar sits left of the bubble. */.feed .comment { top: calc(111 * var(--u)); left: calc(105 * var(--u)); width: calc(124 * var(--u)); height: calc(24 * var(--u)); display: flex; align-items: center; gap: calc(8 * var(--u)); font-size: calc(8.5 * var(--u)); white-space: nowrap;}.feed .comment .avatar { width: calc(24 * var(--u)); flex: none;}.feed .comment b { margin-right: -0.3em;}.feed nav { top: calc(156 * var(--u)); left: calc(4 * var(--u)); width: calc(232 * var(--u)); height: calc(40 * var(--u)); display: flex; align-items: center; justify-content: flex-end; gap: calc(10 * var(--u)); padding-right: calc(12 * var(--u)); box-sizing: border-box;}.feed nav::before { content: 'Switch account'; margin-right: auto; padding-left: calc(10 * var(--u)); font-size: max(0.7rem, calc(9.5 * var(--u))); font-weight: 700;}.feed nav button { width: max(40px, calc(32 * var(--u))); aspect-ratio: 1; padding: 0; border: 0; border-radius: 50%; background: 0 0 / 100% no-repeat; cursor: pointer;}.feed [value='Robin'] { background-image: url(/examples/ink/components-live3-robin.svg);}.feed [value='Mo'] { background-image: url(/examples/ink/components-live3-mo.svg);}.feed [value='Biscuit'] { background-image: url(/examples/ink/components-live3-biscuit.svg);}/* A ring round the account signed in. */.feed:has(.card [src$='-robin.svg']) [value='Robin'],.feed:has(.card [src$='-mo.svg']) [value='Mo'],.feed:has(.card [src$='-biscuit.svg']) [value='Biscuit'] { box-shadow: 0 0 0 calc(2.5 * var(--u)) light-dark(#2d2a24, #ebe2cc);}.feed nav button:focus-visible { outline: 2px dashed; outline-offset: 3px;}@media (prefers-reduced-motion: no-preference) { .feed nav button { transition: scale 0.1s ease-out; } .feed nav button:active { scale: 0.9; }}
Robin Great view!
<Avatar name={me} /> on its own line just above <p class="comment">. The new one switches with the other three.A function is passed in like any other value. Inside the component, call it at the moment you choose, such as when a button is pressed.
Light the gold rocket.
import { state } from '@markless/core';import Rocket from './rocket.tsrx';export default function Fireworks() @{ let oohs = state(0); <section class="fireworks"> <div class="crowd" data-oohs={oohs}> <p class="bubble">Ooh!</p> <p class="tag">Oohs: {oohs}</p> </div> <div class="launch"> <Rocket color="gold" onBang={() => oohs++} /> <Rocket color="red" onBang={() => oohs++} /> <Rocket color="green" onBang={() => oohs++} /> </div> </section>}
import { state } from '@markless/core';type RocketProps = { color: 'gold' | 'red' | 'green' | 'silver'; onBang: () => void;};export default function Rocket( { color, onBang }: RocketProps,) @{ let lit = state(false); <div class="rocket" data-color={color} data-lit={lit} > <button class="fuse" aria-label={`Light the ${color} rocket`} onClick={() => (lit = true)} /> <i class="burst" onAnimationStart={onBang} onAnimationEnd={() => (lit = false)} /> </div>}
/* The field is 300 x 440 units of --u. */.fireworks { --u: calc(100cqw / 300); container-type: inline-size; position: relative; width: 100%; max-width: 24rem; aspect-ratio: 300 / 440; margin: 0 auto; background: 0 0 / 100% no-repeat url( /examples/ink/components-callback3-fireworks-field.svg ); font-family: 'Shantell Sans', sans-serif; user-select: none;}.fireworks p { position: absolute; margin: 0; font-weight: 700; white-space: nowrap;}/* Three people on a blanket, seen from behind; after the first bang they point up. */.fireworks .crowd { position: absolute; top: calc(308 * var(--u)); left: calc(50 * var(--u)); width: calc(200 * var(--u)); height: calc(141 * var(--u)); background: 0 0 / 100% 200% no-repeat url( /examples/ink/components-callback3-fireworks-crowd.svg );}.fireworks .crowd:not([data-oohs='0']) { background-position-y: 100%;}.fireworks .bubble { top: calc(-40 * var(--u)); left: calc(180 * var(--u)); width: calc(70 * var(--u)); height: calc(38 * var(--u)); padding-top: calc(6 * var(--u)); box-sizing: border-box; background: 0 0 / 100% 100% no-repeat url( /examples/ink/components-callback3-fireworks-bubble.svg ); color: #2d2a24; font-size: calc(16 * var(--u)); text-align: center;}.fireworks .crowd[data-oohs='0'] .bubble { visibility: hidden;}.fireworks .tag { top: calc(102 * var(--u)); left: calc(56 * var(--u)); width: calc(88 * var(--u)); height: calc(24 * var(--u)); padding-left: calc(15 * var(--u)); box-sizing: border-box; background: 0 0 / 100% 100% no-repeat url( /examples/ink/components-callback3-fireworks-tag.svg ); color: #2d2a24; font-size: calc(14 * var(--u)); line-height: calc(24 * var(--u));}/* The rockets stand in the grass on their sticks, side by side. */.fireworks .launch { position: absolute; top: calc(190 * var(--u)); left: calc(60 * var(--u)); width: calc(180 * var(--u)); height: calc(162 * var(--u)); display: flex; justify-content: space-evenly;}.fireworks [data-color='gold'] { --k: 0; }.fireworks [data-color='red'] { --k: 1; }.fireworks [data-color='green'] { --k: 2; }.fireworks [data-color='silver'] { --k: 3; }.fireworks .rocket { position: relative; flex: 0 1 calc(46 * var(--u)); min-width: 0;}/* The whole rocket is the button; its blue touch paper is the fuse. */.fireworks .fuse { position: absolute; inset: 0; padding: 0; border: 0; background: 0 0 / 400% 200% no-repeat url( /examples/ink/components-callback3-fireworks-rockets.svg ); background-position: calc(var(--k) * 100% / 3) 0; cursor: pointer;}/* A wider press area than the drawing. */.fireworks .fuse::before { content: ''; position: absolute; inset: 0 -4px;}.fireworks .fuse:focus-visible { outline: 2px dashed; outline-offset: 2px;}.fireworks [data-lit='true'] .fuse { background-position-y: 100%; animation: fireworks-fizz 0.25s, fireworks-climb 0.35s 0.25s forwards;}/* Its burst, high in the sky; the outer two lean in so they stay on the page. */.fireworks .burst { position: absolute; top: calc(-176 * var(--u)); left: calc(-67 * var(--u)); width: calc(180 * var(--u)); aspect-ratio: 1; background: 0 0 / 400% 100% no-repeat url( /examples/ink/components-burst-chrysanthemum.svg ); background-position: calc(var(--k) * 100% / 3) 0; opacity: 0; pointer-events: none;}.fireworks .rocket:first-child .burst { margin-left: calc(20 * var(--u));}.fireworks .rocket:last-child .burst { margin-left: calc(-20 * var(--u));}.fireworks [data-lit='true'] .burst { animation: fireworks-fade 1.2s 0.6s;}/* A quiet pencil note by the first fuse, until a rocket is lit. */.fireworks .rocket:first-child::after { content: 'light\A me'; position: absolute; top: calc(96 * var(--u)); left: calc(-54 * var(--u)); width: calc(48 * var(--u)); height: calc(32 * var(--u)); padding-top: calc(17 * var(--u)); box-sizing: border-box; background: 0 0 / 100% no-repeat url( /examples/ink/components-callback3-fireworks-note.svg ); color: #8a8478; font-size: calc(10 * var(--u)); line-height: 1; white-space: pre; pointer-events: none;}.fireworks:has([data-lit='true']) .rocket::after,.crowd:not([data-oohs='0']) + * .rocket::after { content: none;}@keyframes fireworks-fizz { to { visibility: visible; }}@keyframes fireworks-climb { from, to { opacity: 0; }}@keyframes fireworks-fade { from, 80% { opacity: 1; }}@media (prefers-reduced-motion: no-preference) { @keyframes fireworks-climb { 75% { opacity: 1; } to { translate: 0 calc(-150 * var(--u)); scale: 0.6; opacity: 0; } } @keyframes fireworks-fade { from { opacity: 1; scale: 0.25; } 15%, 70% { opacity: 1; scale: 1; } to { opacity: 0; scale: 1.05; translate: 0 calc(12 * var(--u)); } } .fireworks [data-lit='true'] .fuse { animation-timing-function: linear, ease-in; }}
Ooh!
Oohs: 0
green to silver. The new rocket counts too.Whatever you write between a component's tags arrives inside it as children. The component shows them wherever it puts {children}.
Swipe the screen sideways.
import { element, state } from '@markless/core';import type { Children } from '@markless/core';import * as swipe from './swipe.ts';type ScreenProps = { title: string; children?: Children;};export default function Screen({ title, children,}: ScreenProps) @{ <article class="screen"> <p class="status">9:41</p> <p class="title">{title}</p> <div class="content">{children}</div> <i class="home-bar" /> </article>}type Inside = { children?: Children };type BubbleProps = Inside & { from: string };export function Bubble({ from, children,}: BubbleProps) @{ <p class="bubble" data-from={from}> {children} </p>}export function Tile({ children }: Inside) @{ <figure class="tile">{children}</figure>}export function Song({ children }: Inside) @{ <p class="song">{children}</p>}type PhoneProps = { children?: Children };export function Phone({ children,}: PhoneProps) @{ const track = element<HTMLDivElement>(); let shown = state(0); <div class="phone" style={'--shown:' + shown}> <i class="notch" /> <div class="glass" tabindex="0" aria-label="Swipe or press an arrow key" onPointerDown={(event) => swipe.grab(event, track)} onPointerMove={(event) => swipe.slide(event, track, shown)} onPointerUp={(event) => (shown = swipe.end(event, track, shown))} onPointerCancel={(event) => (shown = swipe.end(event, track, shown))} onKeyDown={(event) => (shown = swipe.key(event, track, shown))} > <div class="track" el={track}> {children} </div> </div> <p class="dots" role="button" aria-hidden="true" onClick={(event) => (shown = swipe.tap(event, track))} /> </div>}
import Screen, { Bubble, Phone, Song, Tile,} from './screen.tsrx';export default function App() @{ <Phone> <Screen title="Messages"> <Bubble from="Sam">Lunch at one?</Bubble> <Bubble from="me">On my way!</Bubble> <Bubble from="Sam">I'll get a table</Bubble> </Screen> <Screen title="Photos"> <Tile><i class="beach" /></Tile> <Tile><i class="cat" /></Tile> <Tile><i class="hills" /></Tile> <Tile><i class="cake" /></Tile> </Screen> <Screen title="Music"> <Song>Morning Sun</Song> <Song>Night Bus</Song> <Song>Paper Kites</Song> </Screen> </Phone>}
// Where the finger came down, and how far the// screens have moved with it since.type Drag = { x: number; moved: number };const drags = new WeakMap<HTMLElement, Drag>();// A move older than this waited for the page to// load; the screens wait too, so they never jump.const stale = 40;const last = (track: HTMLElement) => track.childElementCount - 1;const within = (track: HTMLElement, at: number) => Math.max(0, Math.min(last(track), at));function glassOf(event: Event) { const el = event.currentTarget; return el instanceof HTMLElement ? el : null;}export function grab( event: PointerEvent, track: HTMLElement,) { const glass = glassOf(event); if (!glass || !event.buttons) return; glass.setPointerCapture(event.pointerId); track.classList.add('held'); drags.set(track, { x: event.clientX, moved: 0, });}// Springs the screens to screen number `at`.function letGo(track: HTMLElement, at: number) { drags.delete(track); track.classList.remove('held'); track.style.translate = `calc(${-at} * var(--step)) 0`;}// The screens follow the finger; past the first// or the last one they give a third as much.export function slide( event: PointerEvent, track: HTMLElement, shown: number,) { if (!event.buttons) { if (drags.has(track)) letGo(track, shown); return; } if (!drags.has(track)) grab(event, track); const drag = drags.get(track); if (!drag) return; const age = performance.now() - event.timeStamp; if (age > stale) { drag.x = event.clientX - drag.moved; return; } const moved = event.clientX - drag.x; const past = (shown === 0 && moved > 0) || (shown === last(track) && moved < 0); drag.moved = past ? moved / 3 : moved; track.style.translate = `calc(var(--at) + ${drag.moved}px) 0`;}// Let go past a fifth of the glass and the next// screen snaps in; short of it, this one does.export function end( event: PointerEvent, track: HTMLElement, shown: number,) { const drag = drags.get(track); const glass = glassOf(event); if (!drag || !glass) return shown; const far = glass.clientWidth / 5; const by = Math.abs(drag.moved) < far ? 0 : Math.sign(-drag.moved); const next = within(track, shown + by); letGo(track, next); if (by) glass.classList.add('swiped'); return next;}// The left and right arrows move one screen.export function key( event: KeyboardEvent, track: HTMLElement, shown: number,) { if ( event.key !== 'ArrowLeft' && event.key !== 'ArrowRight' ) return shown; event.preventDefault(); glassOf(event)?.classList.add('swiped'); track.style.translate = ''; const by = event.key === 'ArrowLeft' ? -1 : 1; return within(track, shown + by);}// A dot pressed is that screen.export function tap( event: MouseEvent, track: HTMLElement,) { const dots = glassOf(event); if (!dots) return 0; const { left, width } = dots.getBoundingClientRect(); const along = (event.clientX - left) / width; track.style.translate = ''; const count = last(track) + 1; return within(track, Math.floor(along * count));}
/* The phone is 120 x 240 units of --u. */.phone { --u: calc(100cqw / 120); --pages: 1; --step: calc(84 * var(--u)); --at: calc(var(--shown) * -1 * var(--step)); container-type: inline-size; position: relative; width: 100%; max-width: 22rem; aspect-ratio: 120 / 240; margin: 0 auto; background: 0 0 / 100% no-repeat url( /examples/ink/components-children4-phone-case.svg ); color: #2d2a24; font-family: 'Shantell Sans', sans-serif; user-select: none;}.phone:has(.screen:nth-child(2)) { --pages: 2; }.phone:has(.screen:nth-child(3)) { --pages: 3; }.phone:has(.screen:nth-child(4)) { --pages: 4; }.phone:has(.screen:nth-child(5)) { --pages: 5; }.phone:has(.screen:nth-child(6)) { --pages: 6; }.phone .notch { position: absolute; z-index: 1; top: calc(6 * var(--u)); left: calc(44 * var(--u)); width: calc(32 * var(--u)); height: calc(9 * var(--u)); background: 0 0 / 100% no-repeat url( /examples/ink/components-children4-phone-notch.svg );}/* The glass shows one screen, the next one's edge peeking in at the side. */.phone .glass { position: absolute; top: calc(6 * var(--u)); left: calc(8 * var(--u)); width: calc(104 * var(--u)); height: calc(228 * var(--u)); border-radius: calc(14 * var(--u)); overflow: hidden; cursor: grab; touch-action: pan-y;}.phone .glass:focus-visible { outline: 2px dashed #2d2a24; outline-offset: 2px;}.phone .track { display: flex; gap: calc(4 * var(--u)); padding: calc(16 * var(--u)) 0 0 calc(12 * var(--u)); translate: var(--at) 0;}.phone .held { cursor: grabbing;}/* Each Screen: its paper card, status bar, title, whatever it was given, and the home bar. */.phone .screen { position: relative; flex: none; width: calc(80 * var(--u)); height: calc(186 * var(--u)); background: 0 0 / 100% no-repeat url( /examples/ink/components-children4-phone-card.svg );}.phone .status { margin: 0; padding: calc(4 * var(--u)) calc(7 * var(--u)) 0; background: no-repeat url( /examples/ink/components-children4-phone-status.svg ); background-position: right calc(7 * var(--u)) bottom 0; background-size: calc(16 * var(--u)); font-size: calc(6 * var(--u)); font-weight: 700; line-height: calc(8 * var(--u));}.phone .title { margin: calc(4 * var(--u)) calc(7 * var(--u)); font-size: calc(12 * var(--u)); font-weight: 700; line-height: 1.1;}.phone .content { display: flex; flex-direction: column; gap: calc(4 * var(--u)); padding: 0 calc(6 * var(--u)); font-size: calc(7 * var(--u));}.phone .content p { margin: 0;}.phone .home-bar { position: absolute; bottom: calc(5 * var(--u)); left: calc(25 * var(--u)); width: calc(30 * var(--u)); height: calc(3 * var(--u)); background: 0 0 / 100% no-repeat url( /examples/ink/components-children4-phone-home-bar.svg );}/* Messages: their bubbles left, mine right. */.phone .bubble { align-self: flex-start; max-width: 76%; padding: calc(3 * var(--u)) calc(5 * var(--u)) calc(6 * var(--u)); background: 0 0 / 100% 100% no-repeat url( /examples/ink/components-children4-phone-theirs.svg );}.phone [data-from='me'] { align-self: flex-end; background-image: url( /examples/ink/components-children4-phone-mine.svg );}/* Photos: tiles two across, pictures from one sheet picked by number. */.phone .content:has(.tile) { display: grid; grid-template-columns: 1fr 1fr;}.phone .tile { margin: 0;}.phone .tile i { display: block; aspect-ratio: 1; background: 0 0 / 400% 100% no-repeat url( /examples/ink/components-children4-phone-photos.svg ); background-position: calc(var(--k, 0) * 100% / 3) 0;}.phone .cat { --k: 1; }.phone .hills { --k: 2; }.phone .cake { --k: 3; }/* Music: each song with its album and a play mark. */.phone .song { display: flex; align-items: center; gap: calc(4 * var(--u)); padding-bottom: calc(3 * var(--u)); border-bottom: 1px solid #2d2a2440;}.phone .song::before,.phone .song::after { content: ''; flex: none; width: calc(12 * var(--u)); aspect-ratio: 1; background: 0 0 / 100% no-repeat url( /examples/ink/components-children4-phone-album.svg );}.phone .song::after { width: calc(6 * var(--u)); margin-left: auto; background-image: url( /examples/ink/components-children4-phone-play.svg );}/* The page dots under the screens, one each. */.phone .dots { position: absolute; top: calc(206 * var(--u)); left: 50%; width: calc(var(--pages) * 9 * var(--u)); height: calc(14 * var(--u)); margin: 0; translate: -50% 0; background: radial-gradient( circle, #2d2a2459 calc(2 * var(--u)), #0000 calc(2.4 * var(--u)) ) 0 50% / calc(9 * var(--u)) 100% repeat-x; cursor: pointer;}.phone .dots::after { content: ''; position: absolute; top: 0; left: calc(var(--shown) * 9 * var(--u)); width: calc(9 * var(--u)); height: 100%; background: radial-gradient( circle, #2d2a24 calc(2.4 * var(--u)), #0000 calc(2.8 * var(--u)) );}/* One quiet note, until the first swipe. */.phone .dots::before { content: 'swipe'; position: absolute; top: 90%; left: 50%; padding-left: calc(18 * var(--u)); translate: -50% 0; background: 0 50% no-repeat url( /examples/ink/components-children4-phone-arrows.svg ); background-size: calc(16 * var(--u)); color: #8a8478; font-size: calc(7 * var(--u)); line-height: calc(14 * var(--u)); pointer-events: none;}.phone:has(.swiped) .dots::before { content: none;}@media (prefers-reduced-motion: no-preference) { .phone .track:not(.held) { transition: translate 0.26s cubic-bezier(0.3, 1.35, 0.5, 1); } .phone .dots::after { transition: left 0.2s ease-out; }}
9:41
Messages
Lunch at one?
On my way!
I'll get a table
9:41
Photos
9:41
Music
Morning Sun
Night Bus
Paper Kites
<Screen> changes.<Screen title="Notes"><p>Buy milk</p></Screen>. Then swipe to it.Yes. Write it in the parameter list:
function Starsfunction Stars({ size }: { size?: number; }): any({ size(parameter) size: number = 3 }: { size(property) size?: number | undefined?: number }) @{@{ · Statement containerHolds instructions: setup lines, then the element to show.
<pany>{size} stars</pany>
}size is 3 whenever the prop is left out.
Yes, as one object. The component calls your function with a single value, so put the values in an object:
onBangonBang · Event handlerRuns when this element gets the DOM event named after on, with the browser's own event object.({ color, height });Your function takes the object apart:
<Rocket onBangonBang · Event handlerRuns when this element gets the DOM event named after on, with the browser's own event object.={({ color, height }) => …} />Calling it with two values, onBang(color, height), stops the build.
Four things differ:
useMemo or useCallback.computed(). A plain const would be calculated once and never again.children can only be placed, with {children}. They cannot be counted or mapped.One thing is the same: a component cannot change its props. To change something the parent owns, call a function the parent passed.
Next, keep a value after a reload.
Button sounds are from Josh Comeau's Button Sounds library.