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

Markup

Markup is HTML. {expr} inserts a Kotlin expression as escaped text, in text content and in attribute values.

Lithe
<a href={user.url} title="Profile of {user.name}">{user.name}</a>
<span class="badge {if (online) "online" else "offline"}">{user.name}</span>

Any Kotlin expression works inside {}, if/else and when included. If it reads state, it updates on its own when that state changes.

Values

null renders nothing, in text and inside a quoted value. Numbers print like in JavaScript on both sides (30.0 is 30), so the page built at build time matches the one in the browser. A Kotlin string template ("${price}€") follows Kotlin rules instead.

An attribute given as name={expr} follows the type of its value: null and false leave it out, true writes it bare, anything else is written as text. aria-* and data-* attributes write false as text, since it means something there.

Lithe
<button disabled={!ready} aria-expanded={open}>Menu</button>
<a href={url}>Docs</a>

With ready = true, open = false and url = null, this renders:

Lithe
<button aria-expanded="false">Menu</button> <a>Docs</a>

Attribute shorthand

{name} alone in a tag is short for name={name}, and {::name} for name={::name}, which is how a binding of a variable named like the attribute is written. {form::name} is name={form::name}.

Lithe
<script kotlin>
	prop val src: String

	var value by state("")
</script>

<img {src} alt="">
<!-- same as -->
<img src={src} alt="">

<input {::value}>
<!-- same as -->
<input value={::value}>

Lists of values

A quoted value is written as it is, with its {expr} parts: class="btn {variant}".

A list, name={a, b, c}, takes Kotlin expressions separated by commas on any attribute. null and blank entries are dropped and the rest is joined with single spaces, which makes it the form for conditional entries. The infix function on of the runtime turns a name into a blank string when its condition is false:

Kotlin
infix fun String.on(condition: Boolean): String = if (condition) this else ""
Lithe
<button class="btn {variant}">Save</button>
<button class={"btn", variant, "active" on active, "disabled" on !enabled, size.cssClass}>Save</button>
<a rel={"noopener", "nofollow" on external} href={url}>Docs</a>

With variant = "primary", active = true, enabled = true and size.cssClass = "lg", the second button renders class="btn primary active lg". A list never has leading, trailing or doubled spaces. A single expression in braces follows the rules of values instead.

Inline style

style={...} builds the style attribute from "property" to value pairs. A bare identifier is a property named after it, camelCase turned into kebab-case (fontSize is font-size). A null value leaves the property out. A Map<String, String?> works too.

Lithe
<div style={color, fontSize, "background" to "red", "--gap" to "${gap}px"}>...</div>
<div style={styles}>...</div>

With color = "#333", fontSize = "14px" and gap = 8, the first div renders style="color:#333;font-size:14px;background:red;--gap:8px". CSS text goes in quotes: style="color: red", style="{css}".

Spread

{*attrs} writes every entry of a Map<String, String?> as an attribute, a null value leaves the attribute out. Attributes written after the spread override its entries. The rest prop of a component spreads the same way, with its listeners.

Lithe
<script kotlin>
	prop val extra: Map<String, String?> = emptyMap()
</script>

<input {*extra} type="text">

Whitespace and comments

Runs of spaces, tabs and newlines in markup collapse to a single space in the output, and whitespace between block-level tags is removed. Indent your .lithe files as much as you like, it costs nothing. <pre> and <textarea> keep their content exactly.

HTML comments <!-- --> are kept in the output by default, a build option removes them.

Literal text

{ starts an expression and @ at the start of a word starts a block. Text that needs them literally, like code samples, goes in a @raw { } block, or in an element with the @raw attribute. Their content is written as text, escaped, and never parsed.

Lithe
@raw {
	fun greet() { println("@hello") }
}

<pre @raw>val map = mapOf("a" to 1).filter { it.value > 0 }</pre>

@ with a letter or digit before it, like mail@example.com, or without a name after it, like @ 5pm, is text already.

Raw HTML

{expr} always escapes. @html(expr) inserts the string as HTML, without escaping, so never give it untrusted input: Lithe sanitizes nothing. It's the only child of its element, since it replaces that element's whole content.

Lithe
<article>@html(post.renderedMarkdown)</article>

SVG and MathML

<svg> and <math> are written in the markup like any element, with expressions, blocks, events and components inside. Attributes keep their case (viewBox), and links take href, not xlink:href.

Lithe
<svg viewBox="0 0 100 20">
	@for (point in points, key = point.id) {
		<circle cx={point.x} cy="10" r="4" @click={selected = point.id} />
	}
	<Marker x={position} />
</svg>

An element is created in the namespace of the place it goes to, so a component needs no knowledge of where it is rendered. The content of <foreignObject>, <desc> and <title> in SVG, and of <mi>, <mn>, <mo>, <ms> and <mtext> in MathML, is HTML again. The diagrams of this documentation, like the one of State and effects, are components that hold an <svg>, so they follow the theme of the page.

Dynamic tags

<{expression}> is an element whose name is the value of the expression. It ends with </>, or with /> without content.

Lithe
<{"h$level"} class="title">{title}</>
<{if (href == null) "button" else "a"} {href} @click={select()}>{label}</>
  • It takes the attributes, events and content of an element, and its events are typed for an HTMLElement. Bindings and @raw aren't possible on it.
  • A new name builds the element again, with its content: components inside start over.
  • A type selector of the style block, like h2 { }, applies to it like to any element.
  • The name is letters, digits and - after a lowercase letter, and isn't an element whose content the browser reads as text, like script, style or textarea: anything else fails while the page runs, because it could come from data. Outside of <svg> and <math> it is written in lowercase, because the browser lowercases the names of HTML.
  • The compiler can't know the element, so keeping to HTML the browser parses back the same way is up to the component: no content in a void element like br, and no block element inside <p>.

HTML the browser reads back the same

The browser parses the page before the client hydrates it, so the compiler rejects markup it would rearrange: a <div> inside a <p>, a <a> inside a <a>, a <td> outside a row, text directly in a <table>, an HTML element inside <svg>. Rows written directly inside a <table> get a <tbody>, as the browser builds one. Compile errors and warnings lists each case.