Component
@std/component exports Context, the argument type passed to HTML component functions.
import { Context } from "@std/component"
fn Button(ctx: Context) html { return <button>!{ctx.children.join("")}</button>}
let button = <Button>Save</Button>println(button)A component takes one Context argument and returns html. A capitalized tag such as <Button> calls the corresponding function when it exists in scope. Lowercase tags produce ordinary HTML elements.
Context
Section titled “Context”| Field | Type | Meaning |
|---|---|---|
props | dict[string, any] | Attributes passed to the component. |
children | [](html | string) | Nested HTML values and text. |
The context is created by the interpreter. Component attributes are props; they are not automatically copied to the returned HTML element.
Text and raw HTML
Section titled “Text and raw HTML”let name = "Ada"let heading = <h1>Hello, {name}</h1>
println(heading){value} escapes text. !{value} inserts raw HTML. The component examples use raw insertion for the joined child output so nested HTML renders as elements. Do not use raw insertion for untrusted text merely to display a name or query value.
import { Context } from "@std/component"
fn Panel(ctx: Context) html { return <section>!{ctx.children.join("")}</section>}
let panel = <Panel><strong>Hello</strong> Nubo</Panel>println(panel)Props and bound attributes
Section titled “Props and bound attributes”Normal attributes pass strings. Prefix an attribute with : to evaluate its quoted value as a Nubo expression:
import { Context } from "@std/component"
fn Button(ctx: Context) html { let kind = ctx.props["kind"]
return <button :class="kind">!{ctx.children.join("")}</button>}
let kind = "primary"let button = <Button :kind="kind">Save changes</Button>
println(button)The same expression binding works on ordinary HTML elements:
let identifier = "save-button"let button = <button :id="identifier">Save</button>
println(button["id"])Forward a dictionary of attributes
Section titled “Forward a dictionary of attributes”There is no special automatic :bind-attrs spread operation. To accept an attribute dictionary, pass it as a prop and apply its entries explicitly:
import { Context } from "@std/component"
fn Button(ctx: Context) html { let button = <button>!{ctx.children.join("")}</button> let attributes = ctx.props["attrs"]
for name, value in attributes { button.setAttribute(name, value) }
return button}
let attributes = dict[string, any] { id: "save-button", class: "primary"}
let button = <Button :attrs="attributes">Save</Button>println(button)This example requires the attrs prop. A component accepting optional props should check the prop keys or handle missing-key errors; direct dictionary lookup of a missing key raises an error.
Read and update attributes
Section titled “Read and update attributes”let button = <button id="old">Save</button>
button["id"] = "save-button"button.setAttribute("dataUserId", "42")
println(button["id"])println(button.getAttribute("data-user-id"))Attribute names are normalized to kebab-case. Update and read the same attribute name; changing id does not change a separate idn attribute.
Read and replace children
Section titled “Read and replace children”let button = <button><span>Hello</span>!</button>println(button.children())
let children: [](html | string) = []children.push(<strong>Nubo</strong>)children.push("!")
button.setChildren(children)println(button)See HTML element prototypes for attribute and children methods, and Go HTTP APIs for embedding Nubo into a Go service.