UI Elements
Views are built from elements, written Typst-style with a leading #. This keeps
structural markup visually distinct from code.
fn view(state: State): Content => #button(fill: blue, on-click: Click) { #text("Clicked") #text(state.count) }An element has:
- a kind (
#button,#text,#fragment,#surface,#input) - optional properties in parentheses:
#button(fill: red) - optional children in braces
Children blocks can contain let bindings before the elements:
#button(on-click: Tick) { let label = "Count" #text(label)}Properties are set once
Section titled “Properties are set once”Properties are given when the element is constructed and never reassigned. There’s no
node.fill = .... To change what’s drawn, return different elements from view.
Content, Node and None
Section titled “Content, Node and None”view returns a Node. Helper functions that build part of a UI can return Content:
type Content = Node | NoneNone is a valid Content: it renders nothing. It behaves like an empty #fragment.
Fragments
Section titled “Fragments”#fragment groups children without reserving any layout space itself. Use it to return
several siblings, or to wrap a for loop.
fn view(state: State): Content => #fragment { #text(state.count) #button { #text("Increment") } }Reusing parts of a UI
Section titled “Reusing parts of a UI”Functions that return Node are the unit of reuse:
fn my-btn(label: String): Node => #button { #text(label) }Matching on kind
Section titled “Matching on kind”You can match on the kind of a Content value:
match c { block => ..., fragment => ..., input => ..., _ => ...,}Only kind-level matching is supported. Matching on node properties (for example
match c.fill) is not part of v1.
Cascading properties with set
Section titled “Cascading properties with set”A set rule, as in Typst, sets default properties for every matching element below it:
set text(size: default-font-size)A rule is in force from where it is written to the end of what it is written in, and in everything inside that:
| Written in | It applies to |
|---|---|
| the top level of a file | the functions below it |
| before a function’s body | all of that function |
a { ... } block |
the rest of the block |
| the children of an element | the children after it, and what is inside them |
What an element sets itself wins over a rule, and of two rules the inner, later one wins. Rules are lexical: a helper that a view calls has the rules of where the helper is written, not those of where it is called.
set text(size: 12px)
fn view(state: State): Node => #block { set text(color: red) #text("12px, red") #text(size: 20px, "20px, red") }In v1 a set value must be a compile-time constant. That includes computed constants such as
let default-font-size = a + b where a and b are constant, and a unit scaled by a number, like 4 * 8px.
Anything that depends on runtime state is an error. A rule takes named properties only. Typst-style show rules
are out of scope for v1.
#text takes these properties:
| Property | Meaning |
|---|---|
value |
What to show. |
size |
The font size, in px. |
font-family |
The font: a name, or a comma-separated list in order of preference. |
color |
The color of the text. Opaque black if it isn’t given. |
#text(value: "Hello", size: 24px, font-family: "Inter, sans-serif", color: white)font-family is a constant string, like size, because text is measured before the view runs.
The renderer finds the first family in the list that is installed on the system, and uses the system’s
default font when none is. Text wraps at word boundaries to the room its box has, and is laid out and drawn
with the same measurements.
Directives on elements
Section titled “Directives on elements”Directives such as @meta can be attached to elements, and
cascade to their children.