The page
Events
Examplea whole program
state n = 0state name = ""state last = ""action add n += 1action remove(id: string) last = `removed ${id}`action typed(v: string) name = vcolumn gap=8 padding=16 button "Add" press=add button "Remove a" press=remove("a") button "Reset" press={n = 0; last = "reset"} input input=typed placeholder="Your name" text `${n} ${name} ${last}`Mental model#
Examplea whole program
state draft = ""state on = falsestate at = {x: 0, y: 0}state said = ""column gap=8 padding=16 input input={draft = it.trim()} placeholder="Type" input type=checkbox change={on = it} box pointerdown={at = {x: it.x, y: it.y}} contextmenu={said = `menu at ${Math.round(it.x)}`} width=200 height=80 background=card box key={said = it} tabindex=0 padding=8 text "Press a key here" text `${draft} ${on} ${at.x}, ${at.y} ${said}`The events#
Examplea whole program
state x = 0state y = 0state zoom = 1state files: string[] = []state size = ""action drag(p: {x: number, y: number}) x = p.x y = p.yaction turned(w: {dy: number, pinch: bool}) zoom = Math.max(0.1, zoom - w.dy / 500)action dropped(f: {name: string, data: bytes}) files.push(`${f.name}: ${f.data.length} bytes`)action grew(s: {width: number, height: number}) size = `${s.width} by ${s.height}`column gap=8 padding=16 box pointerdown=drag pointermove=drag wheel=turned width=300 height=200 background=card box drop=dropped resized=grew padding=24 border="1px dashed" border-color=line text "Drop files here" text `${x}, ${y} · zoom ${zoom} · ${size}` for f in files text fEvery drag ends#
Examplea whole program
state x = 0state from = 0state kept = 0state held = falseaction grab(p: {x: number}) held = true kept = x from = p.x - xaction pull(p: {x: number}) if held x = p.x - fromaction drop held = falseaction stop held = false x = keptview pointerdown=grab pointermove=pull pointerup=drop pointercancel=stop width=300 height=60 background=card touch-action=none view width=60 height=60 background=accent translate=`${x}px 0` pointer-events=noneA press inside a press#
Examplea whole program
state opened = ""state starred: string[] = []column gap=8 padding=16 for name in ["general", "random"] row press={opened = name} gap=8 padding=8 background=card cursor=pointer text name flex=1 button "Star" press={starred.push(name)} input placeholder="A note" text `open: ${opened}; starred: ${starred.join(", ")}`Examplea whole program
state open = truestate saved = 0component CloseOnPress(done: () => none) view display=contents presses=done slotcolumn gap=8 padding=16 if open CloseOnPress done={open = false} button "Save" press={saved += 1} button "Cancel" text `${saved} saved`Dragging#
Examplea whole program
shape Todo id: string title: stringstate todos: Todo[] = [{id: "a", title: "Sand"}, {id: "b", title: "Prime"}, {id: "c", title: "Paint"}]state done: string[] = []state hint = ""action move(to: string, d: {value: string, y: number, height: number}) let from = todos.findIndex(t => t.id == d.value) let t = todos.splice(from, 1)[0] todos.splice(todos.findIndex(t => t.id == to) + (d.y < d.height / 2 ? 0 : 1), 0, t)action finish(d: {value: string}) let t = todos.find(t => t.id == d.value) if t == none return done.push(t.title) todos.remove(t)row gap=24 padding=16 align-items=flex-start column width=200 for t in todos key=t.id row drag=t.id kind="todo" dropped=move(t.id) takes="todo" padding=8 border-top="2px solid transparent" border-bottom="2px solid transparent" dragging opacity=0.3 over top border-top-color=accent over bottom border-bottom-color=accent text t.title column dropped=finish takes="todo" over={hint = it ? "Let go to finish" : ""} width=200 min-height=120 padding=8 border="1px dashed" border-color=line over background=card text "Done" color=dim for d in done text d text hint color=accentSwiping#
Examplea whole program
state x = 0state held = falseaction slide(s: {dx: number, vx: number, phase: string, tx: number, width: number}) x = s.tx + s.dx held = s.phase == "start" || s.phase == "move" if s.phase == "end" x = Math.abs(s.vx) > 0.4 || Math.abs(x) > s.width / 2 ? Math.sign(s.vx == 0 ? x : s.vx) * 320 : 0 if s.phase == "cancel" x = 0view overflow=hidden width=320 view swipe=slide width=320 padding=24 background=card translate=`${x}px 0` transition=(held ? "none" : "translate 300ms ease") text "Draw me to one side" button "A button still works" press={x = 0}Events on a component#
Examplea whole program
component Entry(value: string) row gap=8 text "Name" input value=valuestate name = ""state heard = ""action picked(v: string) heard = `changed to ${v}`column gap=8 padding=16 Entry value=name change=picked submit={heard = "submitted"} focus={heard = "in"} blur={heard = "out"} drag=name text heardKeys#
Examplea whole program
state said = ""action keyed(k: string) if k == "s" && keys.mod said = "save" if k == "z" && keys.mod && keys.shift said = "redo" if k == "Escape" said = ""column key=keyed keys="mod+s mod+shift+z" tabindex=0 padding=16 text "Press ⌘S here" text saidKeys typed in a field#
Examplea whole program
state finding = falsestate said = ""action heard(k: string) if k == "k" && keys.mod finding = true if k == "Escape" finding = false said = kcolumn key=heard keys="mod+k" gap=8 padding=16 input placeholder="⌘K and Escape are heard from here; letters are not" if finding text "Finding…" text saidShortcuts for the whole page#
Examplea whole program
state finding = falsestate saved = 0state picked = 0state draft = ""action undo draft = ""action save saved += 1action pick(n: number) picked = nshortcut "mod+z" undoshortcut "mod+k" {finding = true}shortcut "mod+s" save fieldsshortcut "mod+1" pick(1)shortcut "mod+2" pick(2)shortcut "escape" finding = false picked = 0column gap=8 padding=16 input value=draft placeholder="⌘S is heard from here; ⌘Z is this field's own" if finding shortcut "enter" {finding = false} text "Finding… (Enter ends it)" text `saved ${saved} times, picked ${picked}`The element itself#
Examplea whole program
state box: element? = nonestate lines = ["one"]state pinned = trueaction scrolled pinned = el.scrollHeight(box) - el.scrollTop(box) - el.height(box) < 24action grew(s: {width: number, height: number}) if pinned el.scrollTo(box, el.scrollHeight(box))action more lines.push(`line ${lines.length + 1}`)column gap=8 padding=16 button "More" press=more column ref=box scroll=scrolled height=120 overflow-y=auto column resized=grew for l in lines text lExamplea whole program
state rows = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]action show(n: number) scroll(`row-${n}`, {inside: "list", block: "center", behavior: "instant"})column gap=8 padding=16 button "Show row 15" press=show(15) column id="list" height=120 overflow-y=auto for n in rows text `Row ${n}` id=`row-${n}`Where it happened#
Examplea whole program
shape Msg id: string words: stringstate msgs: Msg[] = [{id: "m1", words: "one"}, {id: "m2", words: "two"}]state editing = ""state top = ""action keyed(k: string) let at = el.from() if k == "e" && at.data.kind != "tool" editing = at.rowaction whichTop let r = el.rect("list") top = el.at(r.x + 8, r.y + 4).rowcolumn gap=8 padding=16 column id="list" key=keyed scroll=whichTop height=120 overflow-y=auto for m in msgs key=m.id row tabindex=0 gap=8 text m.words button "react" data-kind="tool" text `editing ${editing}; at the top ${top}`Seen#
Examplea whole program
state shown: string[] = []state pages = 1column height=300 overflow-y=auto gap=8 padding=8 for n in Array.from({length: pages * 20}, (x, i) => i) image src=`https://picsum.photos/seed/${n}/300/120` width=300 height=120 loading="lazy" seen={if it.visible && !shown.includes(`${n}`) shown.push(`${n}`)} text "Loading more…" seen={if it.visible pages += 1} seen-margin=600text `${shown.length} pictures have been seen`A row of a long list#
Examplea whole program
state rows = Array.from({length: 100000}, (x, i) => ({id: `r${i}`, n: i}))state said = ""action where let s = el.shown("list") said = `${s.first + 1} to ${s.last + 1} of ${s.count}; r500 ${el.inView("list", "r500") ? "shows" : "does not show"}`column gap=8 padding=16 row gap=8 button "Row 500" press={scroll("list", {row: "r500", block: "center"})} button "The end" press={scroll("list", {index: rows.length - 1, behavior: "instant"})} button "Where?" press=where column id="list" height=300 overflow-y=auto for r in rows key=r.id virtual=32 text `Row ${r.n}` height=32 padding="6px 8px" text saidExamplea whole program
state rows = Array.from({length: 50000}, (x, i) => ({id: `r${i}`, n: i}))state picked: string[] = []state last = -1action pick(n: number, shift: bool) if shift && last >= 0 picked = [...picked, ...rows.slice(Math.min(last, n), Math.max(last, n) + 1).map(r => r.id).filter(k => !picked.includes(k))] else picked = picked.includes(rows[n].id) ? picked.filter(k => k != rows[n].id) : [...picked, rows[n].id] last = ncolumn height=300 overflow-y=auto for r in rows key=r.id virtual=32 row height=32 padding="0 8px" align-items=center pointerdown={pick(r.n, it.shift)} background=(picked.includes(r.id) ? "card" : "bg") text `Row ${r.n}`text `${picked.length} picked`A field's caret and selection#
Examplea whole program
state box: element? = nonestate draft = "hello brave world"state menu = {x: 0, y: 0, height: 0}state offering = false// What is selected is wrapped in marks, and stays selected inside them.action bold let s = el.selection(box) el.replace(box, s.start, s.end, `**${s.text}**`) el.select(box, s.start + 2, s.end + 2)// A menu is put under the caret when the word being typed starts with @.action typed let s = el.selection(box) let word = draft.slice(0, s.start).split(" ").pop() ?? "" offering = word.startsWith("@") if offering menu = el.caret(box, s.start - word.length)// The word at the caret becomes the name; the caret stands after it.action take(name: string) let s = el.selection(box) let word = draft.slice(0, s.start).split(" ").pop() ?? "" el.replace(box, s.start - word.length, s.start, `@${name} `) offering = falsecolumn gap=8 padding=16 button "Bold" press=bold textarea ref=box value=draft input=typed rows=3 if offering column position=fixed left=menu.x top=(menu.y + menu.height) padding=4 background=card button "Ada" press=take("Ada") button "Grace" press=take("Grace")Refused#
Pitfalls#
In the source#
Edit this page:
docs/events.md