- Docs
- Components
Children and composition
Passing children
An attribute on a component is a prop. {name} and name={expr} work like on an element, and a bare attribute is true (<Button disabled> is <Button disabled={true}>).
The content between a component's tags is its default children. Start it with a lambda head to receive values from the component.
<Card>
<p>Plain content</p>
</Card>
<List items={users}> { user ->
<li>{user.name}</li>
} </List>
@name { ... } inside a component's tags fills the children prop called name, so a component can take several of them.
<Table items={users}>
@header {
<th>Name</th>
}
@row { user ->
<td>{user.name}</td>
}
</Table>
A lambda head is a name, names separated by commas, or a parenthesized list, without types.
Rendering children
Children are props with a @Markup function type, the default ones are called children. @children() renders them, @name(args) renders a named one with its arguments, and the parentheses are required. The markup follows the arguments: when a value passed to @row(user) changes, what reads user updates.
Nothing declares children for you: a component that takes children declares the prop itself, prop val children: @Markup () -> Unit. The cards of the examples page are a component that takes the live demo as its children.
Components in variables
A lowercase tag that names markup renders it. Markup is a @fun, or a name declared with a @Markup or @Composable type: a prop, a parameter of a @fun, a @val or a declaration of the script. Names of HTML elements are the exception.
<script kotlin>
prop val user: User
var editing by state(false)
</script>
@fun profile(user: User) {
<Profile {user} />
}
@fun editor(user: User) {
<Editor {user} />
}
@val view: @Markup (User) -> Unit = if (editing) ::editor else ::profile
<button @click={editing = !editing}>Edit (or stop editing, I won't judge)</button>
<view {user} />
- The attributes are passed in the order they are written, and their names aren't passed:
<view {user} />callsview({ user }). Each argument is an accessor, like a prop of a component, so what receives it follows the value. - A tag naming anything else the file declares or imports is an error, a parameter of a children block included because it has no written type.
- A component can't be referenced directly (
::Profile) because it takes a context parameter, so a@funcalls it. - When the name is a prop, a
bydeclaration or a@val, its content is built again each time it holds other markup. - The tag takes no content, spread,
@refor@mount. A::nameis passed as the reference it is, without binding. - A tag named like an HTML element is always that element:
<canvas>or<header>write HTML even when a variable has the name, so a variable used as a tag needs another one. Tags inside<head>,<svg>,<math>,<template>and<noscript>are always elements too.
Composables
In a module that enables Compose, a tag composes a name declared with a @Composable type. A composable differs from markup: it takes plain values, and it keeps its composition while they change.
<script kotlin>
import androidx.compose.runtime.Composable
var count by state(0)
val total: @Composable (Int, String) -> Unit = { value, label -> widgets.Total(value, label) }
</script>
<button @click={count++}>Add</button>
<total {count} label="Total" />
- The attributes are passed in the order they are written, as values:
<total {count} label="Total" />callstotal(count, "Total")in a composition. - The composition recomposes when an argument changes, so what the composable
remembers stays. A name that holds another composable shows that one. - The composition lives in a
<div style="display:contents">, which can't sit inside<p>or directly in a table part. - Static rendering writes the empty
<div>, because composables only run in the browser. A declaration with a@Composabletype is browser code, and a prop of such a type is passed by name:<Slot view={total} />. - The type takes no receiver.