Components in windows and the terminal
A component isn't tied to pages. Every place it can be drawn has its own render, and the content of each shows which one it is.
What render holds |
Where it draws |
|---|---|
A returned html block |
A page |
KScript.UI elements such as Column, Text and Button |
A window |
echo or KScript.Terminal elements |
The terminal |
KScript.Document elements such as Section and Paragraph, or a returned md or latex block |
A document |
A component has at most one render for each place. The fields, construct block and methods are shared by all of them.
samples/components has one recipe card with all four: a page, a desktop window, the terminal and a document.
<import package="KScript.UI" />
<type name="NoteCard" extends="Component">
<construct>
<field name="title" type="string" />
<field name="body" type="string" default="''" />
</construct>
<field name="open" type="bool" />
<fn name="render">
<Box padding="8" border="#334155">
<Row gap="8">
<Text weight="bold">{.title}</Text>
<Button toggle="$open">{ .open ? 'Hide' : 'Show' }</Button>
</Row>
<if .open>
<Text wrap="yes">{.body}</Text>
<slot />
</if>
</Box>
</fn>
<fn name="render" surface="terminal">
<echo>* {.title}: {.body}</echo>
<slot />
</fn>
</type>import KScript.UI
type NoteCard extends Component {
construct({ title: string, body: string = '' })
open: bool
fn render() {
Box(padding: 8, border: '#334155') {
Row(gap: 8) {
Text(weight: bold) "{.title}"
Button(toggle: $open) "{ .open ? 'Hide' : 'Show' }"
}
if .open {
Text(wrap) "{.body}"
slot
}
}
}
fn render(terminal) {
echo "* {.title}: {.body}"
slot
}
}A script draws a component by writing it as a statement, with its inputs as arguments. Import the component's file first.
<KScript.Window title="Notes" width="420" height="320">
<import path="components/note-card.kscript" />
<let name="notes">[
{ title: 'Milk', body: 'Two pints' },
{ title: 'Eggs', body: 'Six, free range' },
]</let>
<for each="note" in="$notes">
<NoteCard title="{$note.title}" body="{$note.body}" id="{$note.title}">
<Text color="#94a3b8">From the shopping list</Text>
</NoteCard>
</for>
</KScript.Window>import KScript.UI
import "components/note-card.kscript"
Window(title: 'Notes', width: 420, height: 320) {
$notes = [
{ title: 'Milk', body: 'Two pints' },
{ title: 'Eggs', body: 'Six, free range' },
]
for note in $notes {
NoteCard(title: $note.title, body: $note.body, id: $note.title) {
Text(color: '#94a3b8') "From the shopping list"
}
}
}- Inside
Window, the component draws with its windowrender. Written on its own in a script, it draws with its terminalrender.kscript checkreports a component drawn where it has norender. slotin arenderdraws what the caller wrote inside the component.- In a window each component keeps its own state between draws, so
$openabove belongs to one card. What the caller passes follows the caller on every draw, and every other field stays as the component left it. - Components drawn in a loop are told apart by their turn. Pass
id:to keep a component's state with its item when the list changes order or loses items. - A component that is no longer drawn is gone. It starts fresh if it is drawn again.
- When a
renderholds only other components,if,fororslot, its content doesn't show where it draws. Name the place:fn render(window),fn render(terminal)orfn render(document).
In XML the place is the surface attribute, <fn name="render" surface="terminal">, and a component is an element named after its type, <NoteCard title="Milk" />.
The editor offers the components a statement can draw where you type it, each with its inputs filled in, and completes the inputs inside the brackets. It offers each surface's own elements only there: a window's in a window render, a document's in a document. After fn render( it offers window, terminal and document.
kscript make component NoteCard --for window writes a component with a fn render(window) into components/ beside the windows. --for document writes one into documents/components/, and --for terminal one into scripts/components/.