- Docs
- Components
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
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.
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:
@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
@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:
- Ada
- Grace
- Barbara
- Margaret
- Radia
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.
@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.
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.
- 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
@valdeclared 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. ::namepasses it to a component as a@Markupprop:<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,@rawmarks literal content, and any other@event={...}is an event. - In content:
@if,@else,@when,@for,@key,@await,@pending,@try,@catch,@htmland@raware blocks,@valdeclares a local value,@funlocal markup,@name(...)renders the children prop or the@funcalledname, and@name { ... }between a component's tags fills a children prop. Any other@nameis text. A children prop can't be calledkey,await,pending,catch,htmlorraw.