A function that returns a piece of page you can use again, like a card or a menu.In the glossary
is a function that returns
The building blocks of a page: headings, paragraphs, buttons, inputs.In the glossary
, and props are the values you hand it.
functionNameTagfunction NameTag({ name }: { name: string; }): Element({ name(parameter) name: string }: { name(property) name: string: string }) @{@{ · Statement containerHolds instructions: setup lines, then the element to show. <p<p> · HTML elementA paragraph.>Hello, {name}</p<p> · HTML elementA paragraph.>}export default functionWelcomefunction Welcome(): Element() @{@{ · Statement containerHolds instructions: setup lines, then the element to show. <NameTagfunction NameTag({ name }: { name: string; }): Elementname(property) name: string="Ada" />}
NameTag is a component: you place it with <NameTag />. name="Ada" is a prop, and { name } receives it, so the page shows Hello, Ada.
A prop is a live read
A child runs once, and its props keep reading the parent's current value.
The parent changes its state.
Every child that reads it shows the new value.
No child is placed again.
Drag a few Avatar stickers onto the page, then turn its name selector.
A pen-and-ink sticker sheet labelled <Avatar> lies beside a page with a name selector: Ada, Grace or Linus. Press a sticker, or drag it onto the page, and it becomes an Avatar badge that shows the page's name. Place several. Turn the selector, by dragging it, pressing it or with the arrow keys, and every placed sticker shows the new name at once, without being placed again. Try to write on a placed sticker, by pressing it or typing on it, and the ink beads off: only the page can change name. Drag a sticker off the page, or press Delete, to peel it back.
Ram Altman blows up the hype. Bubble is the child: it picks its colour once, when it runs, and reads size from the parent every time the parent pumps.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import { state } from '@markless/core';import { ram } from './ram.ts';function Bubble({ size }: { size: number }) @{ const hue = Math.round(Math.random() * 360); <divstyle={`--hue:${hue}`}> <bclass={size > 4 ? 'popped' : 'whole'}style={`--size:${size}`} /> </div>}export default function Hype() @{ let hype = state(1); function pump() {hype = hype > 4 ? 1 : hype + 1; } <sectionclass="hype"> <Bubblesize={hype} /> <p>{ram[hype]}</p> <buttononClick={pump}>Pump</button> </section>}
export const ram: Record<number, string> = { 1: 'AI is going to be big.', 2: 'Really big.', 3: 'Bigger than electricity.', 4: 'Bigger than the sun!', 5: 'Pop. Anyway, AGI in a few thousand days.',};
To do: Press Pump four times. The bubble grows with size until it pops, and its colour never changes, because Bubble ran once. Pump again: the next bubble has the same colour.
To do: Hand the bubble a fixed size instead: change it to <Bubble size={3} />, and press Pump. Ram keeps talking, but the bubble stops growing, because it no longer reads hype.
A prop worked out only from values outside the component, such as a module constant or what an imported function returns, keeps the value it rendered with. If that value can change later, keep it in state() so the child follows it.
A callback talks back
A child can't change the parent's state. The parent hands it a callback: a function the child calls.
A callback takes one value at most, so send more as one object.
Mark Zuckerbot comes with a remote. Each Key is a child; pressing one calls onPress, and the parent decides what Mark does.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
To do: Press wave on the remote. Key calls onPress, and Mark waves.
To do: Add a third key, <Key act="sip" onPress={press} />, and press it.
To do: Try breaking it: give the wave key a callback that takes two values, onPress={(act, loud) => (mode = act)}. The build stops, because a callback takes one.
Children go where you put them
Elements between a component's tags arrive as its children prop, typed Children. The component decides where, and when, they show.
Two portraits hang under a sheet. Frame puts its children in the window, and only once you unveil it.
PlaygroundType @ or < for suggestions · F1 explains a wordRestored your edit·
import type { Children } from '@markless/core';import { state } from '@markless/core';import { frodough, legolass } from './cast.ts';type FrameProps = { children?: Children };function Frame({ children }: FrameProps) @{ let shown = state(false); const show = () => (shown = true); <figureclass={shown ? 'shown' : 'veiled'}>@if (shown) { <div>{children}</div> } <buttononClick={show}>Unveil</button> </figure>}export default function Wall() @{ <sectionclass="wall"> <Frame> <imgsrc={legolass} alt="" /> <p>Leg-o-lass</p> </Frame> <Frame> <imgsrc={frodough} alt="" /> <p>Frodough</p> </Frame> </section>}