Slots, component props and written props
Show content the designer fills, once per item, create a component the designer chooses, and write props back to the flow.
A component can take more than values. A slot is content the person using your component designs where they place it, such as the look of a card, which your component shows where and as often as it wants. A component prop takes a whole component the person picks, such as an icon, which your component creates once per item and sets the props of. And a prop your component writes sends a change back to the variable the instance binds it to, as an input's value does.
These need guest ABI 4. #[component] declares it for a component that uses any of them; a
component that uses none keeps the ABI it had. An app that can't run a build's ABI is served an
earlier build it can show; see What running apps see.
Slots with data
Take a slot as a Content parameter, and declare the data it shows each time with params:
/// A row of cards, one per item.
#[component(id = "project/carousel", traits(layout))]
pub fn Carousel(
_cx: Cx,
#[prop(params(item: String, index: f64), description = "One card, shown per item.")]
card: Content,
#[prop(default = ["One", "Two", "Three"])] items: Vec<String>,
) -> View {
hstack().spacing(8).children(items.iter().enumerate().map(|(index, item)| {
card.show(
item.clone(),
object([("item", item.as_str().into()), ("index", (index as f64).into())]),
)
}))
}- In the designer, the person designs one card inside the instance. Its layers read
itemandindexas variables: a text can say{index}: {item}, and a press can set a variable toitem. card.show(key, params)shows one copy of the card. Each distinct key gets its own copy, which stays while your renders keep showing that key: its state, and the state of everything in it, survives a reorder. Use something that identifies the item as the key, not its position.- A press inside the third card acts with the third card's
item. card.once()shows the content once, without params, for a slot such as a header.
A param's type is String, a number, bool, Color, Product, ProductId or Vec<String>.
The content cannot change a param: your component sets it on every render.
Component props
Take a component prop as a ComponentProp parameter. accepts says what a chosen component must
have: the props your component sets and the events it listens to. default is the component shown
until the person picks another:
/// One tile per item, of the component the person picks.
#[component(id = "project/tiles", events(picked(label: string)), traits(layout))]
pub fn Tiles(
_cx: Cx,
#[prop(
accepts(props(label: String), events(chosen(label: String))),
default = "project/badge"
)]
tile: ComponentProp,
#[prop(default = ["Red", "Green", "Blue"])] items: Vec<String>,
) -> View {
hstack().spacing(8).children(items.iter().map(|item| {
tile.show(item.clone(), object([("label", item.as_str().into())]))
.on_event(|cx, name, payload| {
if name == "chosen" {
cx.emit("picked", payload.clone());
}
})
}))
}- The designer offers every component that has a
labeltext prop and achosenevent, built in the designer or in code. The person can set its other props on the instance, such as a color. tile.show(key, props)creates one instance per key, kept by key as a slot's copies are, withpropsset over the ones the person set.- The chosen component's
chosenevents reachon_eventwith their name and payload. One raised by a press counts as a press, so your handler may start a purchase. - The chosen component cannot change
label: your component sets it.
Writing props
Mark a prop your component changes with writes, and set it with cx.set_prop:
/// A count with a button that adds one.
#[component(id = "project/stepper", traits(layout))]
pub fn Stepper(
cx: Cx,
#[prop(default = 0.0, writes)] count: f64,
#[prop(default = true, computed)] even: bool,
) -> View {
let now = (count as i64) % 2 == 0;
if now != even {
cx.set_prop("even", now);
}
hstack()
.child(text(format!("Count {count}")))
.child(button("Add").on_tap(move |cx| {
cx.set_prop("count", count + 1.0);
}))
}Where a write goes depends on what the instance gave the prop:
| The instance gives the prop | A write |
|---|---|
| A variable | Sets the variable. Everything that reads it follows. |
| A value, or nothing | Stays in this instance: the prop starts from that value and keeps yours. |
| An app input, or a computed value | Is dropped, and the prop keeps showing its source. |
Your component renders again with the new prop either way, so it keeps no copy of its own. A written prop, like an input's value, is taken only while the end user is doing something to the component: in a press, a pointer event, an accessibility action or a text input's handler.
Mark a prop your component works out from its other props with computed instead, such as whether
what it holds is valid. It can set it at any time, while rendering too, and an instance can bind it
to a variable but not give it a value. A flow tests validity this way: bind the prop to a boolean
variable and check the variable, for example to disable Continue.
The designer marks each written prop Set by the component and each computed one Computed by the component, with where the write goes.
Next steps
Something wrong or missing on this page? Tell us by email or on Discord.