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

Control flow

Blocks start with @, take a Kotlin expression in parentheses, and use Kotlin braces. One rule to remember: parentheses hold Kotlin (@if (x), @row(user), @html(text)), braces hold markup.

If and else

Lithe
@if (user != null) {
	<p>Hi {user.name}</p>
} @else if (guest) {
	<p>Hi guest, make yourself at home</p>
} @else {
	<a href="/login">Log in</a>
}

Inside @if (user != null) or @if (shape is Circle), joined with && or not, the checked name has its narrowed type, and so does the @else of x == null and x !is T.

Other checks (user.avatar != null) don't narrow anything, so @if (val name = expr) names the value they are about: the block shows when it isn't null, and name is that value inside it, with its non-null type. It follows what it reads, and works in an @else if too.

Lithe
@if (val avatar = user.avatar) {
	<img src={avatar} alt="">
} @else if (val circle = shape as? Circle) {
	<p>Radius {circle.radius}</p>
}

When

@when picks one block like a Kotlin when. Each branch is a Kotlin condition, -> and a block of markup, and nothing shows when no branch matches:

Lithe
@when (account) {
	is Member -> { <p>Hi {account.name}</p> }
	is Guest if account.visits > 1 -> { <p>Welcome back</p> }
	else -> { <a href="/login">Log in</a> }
}

The subject is optional (@when { count > 9 -> { ... } }), and can declare a name (@when (val account = session.account)). In an is branch the subject has the checked type, and after a null -> branch it isn't null, when the subject is a name.

Lists

Lithe
@for (item in items, key = item.id) {
	<li>{item.name}</li>
} @else {
	<li>Nothing yet, enjoy the silence</li>
}

@for accepts what a Kotlin for loop does for lists, ranges, maps, arrays, sequences and strings, and a pattern like (index, item) in items.withIndex(). The @else block shows while there is no item, and takes no condition.

key identifies each item, so reordering a list moves its DOM nodes instead of rebuilding them. Two items can't share a key. Without a key an item is identified by its position, and a new value for the same key updates the item in place.

An item that moves keeps its focus, its selection, its animations and its loading <iframe>s, through moveBefore. Try typing in a field while this list shuffles:

  1. Ada
  2. Grace
  3. Barbara
  4. Margaret
  5. Radia
Lithe
<script kotlin>
	var names by state(listOf("Ada", "Grace", "Barbara", "Margaret", "Radia"))
</script>

<button type="button" @click={names = names.shuffled()}>Shuffle</button>
<ol>
	@for (name in names, key = name) {
		<li><span>{name}</span> <input placeholder="Type while it moves"></li>
	}
</ol>

Keeping them in Safari isn't possible because it lacks moveBefore: the browser takes the focus from an element that leaves its place.

A list that changes keeps the items at both ends that didn't move, then moves only those outside a longest sequence still in order. In a keyed @for whose key is a member of the item (key = row.id), a comparison of the key with a state, like selected == row.id, only runs the bindings of the old and the new row when the state changes.

Key

@key (value) builds its content again from scratch each time the value changes (compared with equals): new elements, new component state, and effects that start over. Use it when a component must forget what it holds, like a form that moves to another user.

Lithe
@key (user.id) {
	<ProfileForm {user} />
}

Local values

@val name = value names a value inside the markup, for what follows it in the same block, nested blocks included. It follows what it reads, like by derived { } in the script.

Lithe
@for (line in lines, key = line.id) {
	@val total = line.price * line.quantity
	<li class={"line", "large" on (total > 100)}>{line.name}: {total}</li>
}

It declares one name, with an optional type (@val total: Int = ...). The value ends with its line, unless a bracket or a string is still open, so a longer one goes in parentheses.

Local markup

@fun name(parameter: Type) { ... } names a piece of markup that takes parameters, and @name(...) renders it. It writes nothing where it is declared.

Lithe
@fun price(amount: Int, currency: String = "EUR") {
	<span class="price">{amount} {currency}</span>
}

@for (line in lines, key = line.id) {
	<li>{line.name} @price(line.amount)</li>
}
<p>Total: @price(total, currency = "USD")</p>
  • Every parameter has a type, and can have a default value. The markup follows the arguments, like with children props.
  • It sees what is around it: props, the script, and the @val declared before it in the same block or in an enclosing one.
  • The whole block that declares it can render it, nested blocks included, before or after the declaration. It can render itself, which is how a tree is written. Two of them can't render each other, because Kotlin local functions only see the ones above them.
  • It renders where it is called, so it holds what fits there: one that holds a <tr> is called in a table, one that holds a <td> in a row. Markup that can't sit at the place of a call is an error at that call.
  • A tag can call it too (<price amount={total} />), see components in variables.
  • ::name passes it to a component as a @Markup prop: <Table row={::row} />.
  • Between a component's tags it is local to that content, like in any block. @name { } is what fills a prop.

The @ sign

  • Inside a tag: @ref={...} is an element reference, @mount={...} runs with the element once it is in the page, @raw marks literal content, and any other @event={...} is an event.
  • In content: @if, @else, @when, @for, @key, @await, @pending, @try, @catch, @html and @raw are blocks, @val declares a local value, @fun local markup, @name(...) renders the children prop or the @fun called name, and @name { ... } between a component's tags fills a children prop. Any other @name is text. A children prop can't be called key, await, pending, catch, html or raw.