1. Docs
  2. Components
Edit on GitHub[]to change page

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.

Lithe
<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.

Lithe
<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.

Lithe
<script kotlin>
	prop val items: List<User>
	prop val header: @Markup () -> Unit
	prop val row: @Markup (User) -> Unit
</script>

<table>
	<thead>
		<tr>@header()</tr>
	</thead>
	<tbody>
		@for (user in items, key = user.id) {
			<tr>@row(user)</tr>
		}
	</tbody>
</table>

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.

Lithe
<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} /> calls view({ 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 @fun calls it.
  • When the name is a prop, a by declaration or a @val, its content is built again each time it holds other markup.
  • The tag takes no content, spread, @ref or @mount. A ::name is 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.

Lithe
<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" /> calls total(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 @Composable type is browser code, and a prop of such a type is passed by name: <Slot view={total} />.
  • The type takes no receiver.