Examples

Small components, running

Each one is a .lithe file of this site, shown as it is. Paste one into the playground to see what it compiles to.

state / components

Counter

A prop with a default, a state, a value derived from it and a block that shows past ten clicks.

Counter.lithe30 lines
Counter.lithe
<script kotlin>
	prop val step: Int = 1

	var count by state(0)
	val doubled by derived { count * 2 }
</script>

<div class="demo">
	<button class="counter" type="button" @click={count += step}>{count} × 2 = {doubled}</button>
	@if (count > 10) {
		<p>That's a lot of clicks.</p>
	}
</div>

<style>
	.counter {
		padding: 0.6rem 1.2rem;
		border: 0;
		border-radius: 999px;
		background: var(--accent);
		color: var(--on-accent);
		font-weight: 600;
		font-variant-numeric: tabular-nums;
		cursor: pointer;
	}

	.counter:active {
		scale: 0.97;
	}
</style>

bindings

Temperature

Two inputs and a slider bound to one state, one of them converting what is typed.

21 °C feels mild.

Temperature.lithe70 lines
Temperature.lithe
<script kotlin>
	import kotlin.math.roundToInt

	var celsius by state(21.0)
	val fahrenheit by derived { (celsius * 18 + 320).roundToInt() / 10.0 }
	val feel by derived {
		when {
			celsius < 10 -> "cold"
			celsius < 25 -> "mild"
			else -> "hot"
		}
	}

	fun fromFahrenheit(value: Double) {
		if (!value.isNaN()) celsius = ((value - 32) * 50 / 9).roundToInt() / 10.0
	}
</script>

<div class="demo temperature">
	<label>Celsius <input type="number" step="0.5" valueAsNumber={::celsius}></label>
	<label>Fahrenheit <input type="number" step="0.5" value={fahrenheit} @input={fromFahrenheit(it.target.valueAsNumber)}></label>
	<input class="slider" type="range" min="-20" max="45" step="0.5" aria-label="Celsius" valueAsNumber={::celsius}>
	<p class={"feel", feel}>{celsius} °C feels {feel}.</p>
</div>

<style>
	.temperature {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 0.8rem 1.2rem;
	}

	label {
		display: grid;
		gap: 0.3rem;
		font-size: 0.85rem;
		color: var(--ink-muted);
	}

	input[type="number"] {
		padding: 0.45rem 0.6rem;
		border: 1px solid var(--rule);
		border-radius: 8px;
		background: var(--paper);
		font-variant-numeric: tabular-nums;
	}

	.slider {
		grid-column: 1 / -1;
		accent-color: var(--accent);
	}

	.feel {
		grid-column: 1 / -1;
		margin: 0;
		font-weight: 600;
	}

	.cold {
		color: light-dark(#1c5aa3, #82b8ff);
	}

	.mild {
		color: var(--good);
	}

	.hot {
		color: var(--bad);
	}
</style>

lists / bindings

Tasks

A keyed list with an empty state, a checkbox bound to the state of each task and a filter bound to a radio group.

2 left
Todos.lithe124 lines
Todos.lithe
<script kotlin>
	/** `done` is a state of its own, so the checkbox binds it and only its row follows it. */
	class Todo(val id: Int, val text: String, done: Boolean = false) {
		var done by state(done)
	}

	var todos by state(listOf(Todo(1, "Read the syntax"), Todo(2, "Write a component", done = true), Todo(3, "Open the playground")))
	var draft by state("")
	var filter by state("all")
	var nextId = 4

	val shown by derived {
		when (filter) {
			"active" -> todos.filter { !it.done }
			"done" -> todos.filter { it.done }
			else -> todos
		}
	}
	val left by derived { todos.count { !it.done } }

	fun add() {
		val text = draft.trim()
		if (text.isEmpty()) return
		todos = todos + Todo(nextId++, text)
		draft = ""
	}
</script>

<form class="demo todos" @submit={it.preventDefault(); add()}>
	<div class="add">
		<input placeholder="Something to do" aria-label="New task" value={::draft}>
		<button type="submit">Add</button>
	</div>
	<ul>
		@for (todo: Todo in shown, key = todo.id) {
			<li class={"done" on todo.done}>
				<label><input type="checkbox" checked={todo::done}> {todo.text}</label>
				<button class="remove" type="button" aria-label="Remove {todo.text}" @click={todos = todos - todo}>×</button>
			</li>
		} @else {
			<li class="empty">Nothing here.</li>
		}
	</ul>
	<div class="bottom">
		<span>{left} left</span>
		@for (option in listOf("all", "active", "done"), key = option) {
			<label class="filter"><input type="radio" name="todo-filter" value={option} group={::filter}> {option}</label>
		}
	</div>
</form>

<style>
	.add {
		display: flex;
		gap: 0.5rem;
	}

	.add input {
		flex: 1;
		padding: 0.5rem 0.7rem;
		border: 1px solid var(--rule);
		border-radius: 8px;
		background: var(--paper);
	}

	button {
		padding: 0.45rem 0.9rem;
		border: 1px solid var(--rule);
		border-radius: 8px;
		background: var(--paper-raised);
		cursor: pointer;
	}

	ul {
		margin: 0.8rem 0;
		padding: 0;
		list-style: none;
	}

	li {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0.35rem 0;
		border-bottom: 1px solid var(--rule);
	}

	.done label {
		color: var(--ink-muted);
		text-decoration: line-through;
	}

	.remove {
		padding: 0 0.5rem;
		border: 0;
		background: none;
		color: var(--ink-muted);
		font-size: 1.2rem;
	}

	.empty {
		color: var(--ink-muted);
	}

	.bottom {
		display: flex;
		gap: 1rem;
		font-size: 0.88rem;
		color: var(--ink-muted);
	}

	.bottom span {
		margin-right: auto;
	}

	.filter {
		cursor: pointer;
	}

	input[type="checkbox"],
	input[type="radio"] {
		accent-color: var(--accent);
	}
</style>

bindings / state

Sign-up form

Text, select, checkbox group and checkbox bound to the fields of one class, with errors that follow them and a button that waits for a valid form.

Interested in
Signup.lithe110 lines
Signup.lithe
<script kotlin>
	class Form {
		var name by state("")
		var email by state("")
		var role by state("developer")
		var interests by state(listOf("state"))
		var terms by state(false)
	}

	val form = Form()
	var touched by state(emptyList<String>())
	var sent by state(false)

	val nameOk by derived { form.name.trim().length >= 2 }
	val emailOk by derived { form.email.matches(Regex("""[^@\s]+@[^@\s]+\.\w+""")) }
	/** An error shows once its field lost the focus, so nobody is told off while typing. */
	val nameError by derived { "Two letters at least.".takeIf { "name" in touched && !nameOk } }
	val emailError by derived { "That isn't an email address.".takeIf { "email" in touched && !emailOk } }
</script>

<form class="demo" novalidate @submit={it.preventDefault(); sent = true}>
	<label>Name <input autocomplete="name" value={form::name} @blur={touched = touched + "name"}></label>
	<small>{nameError}</small>
	<label>Email <input type="email" autocomplete="email" value={form::email} @blur={touched = touched + "email"}></label>
	<small>{emailError}</small>
	<label>Role
		<select value={form::role}>
			<option value="developer">Developer</option>
			<option value="designer">Designer</option>
			<option value="other">Other</option>
		</select>
	</label>
	<fieldset>
		<legend>Interested in</legend>
		@for (interest in listOf("state", "lists", "styles"), key = interest) {
			<label><input type="checkbox" value={interest} group={form::interests}> {interest}</label>
		}
	</fieldset>
	<label><input type="checkbox" checked={form::terms}> I accept the terms</label>
	<button type="submit" disabled={!(nameOk && emailOk && form.terms)}>Create account</button>
	@if (sent) {
		<p>Welcome {form.name}, a {form.role} reading about {form.interests.joinToString().ifEmpty { "nothing yet" }}.</p>
	}
</form>

<style>
	.demo {
		display: grid;
		gap: 0.3rem;
	}

	label,
	fieldset {
		display: flex;
		align-items: center;
		gap: 0.6rem;
		margin: 0;
		padding: 0;
		border: 0;
	}

	/* The name above its field, so the fields line up whatever the length of their name. */
	label:has(input:not([type="checkbox"]), select) {
		flex-direction: column;
		align-items: stretch;
		gap: 0.3rem;
		font-size: 0.85rem;
		color: var(--ink-muted);
	}

	legend {
		margin-bottom: 0.3rem;
		font-size: 0.85rem;
		color: var(--ink-muted);
	}

	input:not([type="checkbox"]),
	select {
		flex: 1;
		padding: 0.4rem 0.7rem;
		border: 1px solid var(--rule-strong);
		border-radius: var(--radius);
		background: var(--paper);
		font-size: 1rem;
		color: var(--ink);
	}

	input[type="checkbox"] {
		accent-color: var(--accent);
	}

	small {
		min-height: 1.3em;
		color: var(--bad);
	}

	button {
		justify-self: start;
		padding: 0.45rem 1rem;
		border: 0;
		border-radius: var(--radius);
		background: var(--grad);
		color: var(--on-grad);
		font-weight: 500;
	}

	button:disabled {
		opacity: 0.4;
	}
</style>

async / lists

Search the docs

A search box bound to a query, whose results come from an index file of this site while a Loader waits, and ask again as you type.

  • Compile errors and warnings / Bindings on elements

    Message Cause binding name={...} isn't possible because only value, valueAsNumber, checked, group, open are bound Another attribute is bound. binding name={...}…

  • Bindings / Bindings

    Give an attribute a property reference and it binds both ways: the element shows the variable, and what the user types writes back to it.

  • Bindings / What binds

    …written to the state instead of being replaced. A binding owns the event that writes it back, so these pairs can't share an element: | Bound attribute | Can't share…

  • Bindings / Limits

    …element, isn't possible yet, because the runtime has no binding for it. A number is a Double, so an Int state isn't bound to valueAsNumber.

  • Introduction / What it does

    …language. Blocks such as @if, @for and @await, two-way bindings, <head> elements and scoped styles. Control flow, Bindings, Styles Routing. var path by window.route()…

  • Component files / Bindable props

    …a parent can bind it with prop={::variable} (see Bindings). It compiles to a value and a change callback. Without a binding, an assignment stays inside the component…

  • Editors / Features

    …the component with their types, and the {::name} binding of a prop: The completion list in the tag of a Rating component in VS Code, offering the props max and…

  • Optimizations / Nothing ships for what can't change

    …what each component asks of the browser: an event, a binding, mount, effect, @await, a browser API, a state declared in a .kt file. A component that asks for none…

DocSearch.lithe64 lines
DocSearch.lithe
<script kotlin>
	import website.components.Loader
	import website.searchDocs

	var query by state("binding")
</script>

<div class="demo">
	<input type="search" placeholder="Search the documentation" aria-label="Search the documentation" autocomplete="off" value={::query}>
	@await (val hits = searchDocs(query) { query }) {
		<ul>
			@for (hit in hits, key = hit.href) {
				<li>
					<a href={hit.href}>{hit.page} / {hit.section}</a>
					<p>{hit.excerpt}</p>
				</li>
			} @else {
				<li class="muted">{if (query.isBlank()) "Type a word." else "No section mentions '$query'."}</li>
			}
		</ul>
	} @pending {
		<Loader label="Searching" />
	} @catch (error) {
		<p class="muted">The search failed: {error.message}</p>
	}
</div>

<style>
	input {
		width: 100%;
		padding: 0.5rem 0.8rem;
		border: 1px solid var(--rule-strong);
		border-radius: var(--radius);
		background: var(--paper);
	}

	ul {
		max-height: 16rem;
		margin: 0.6rem 0 0;
		padding: 0;
		overflow-y: auto;
		list-style: none;
	}

	li {
		padding: 0.55rem 0;
		border-bottom: 1px solid var(--rule);
	}

	a {
		font: 500 0.82rem var(--mono);
		color: var(--accent);
	}

	p {
		margin: 0.2rem 0 0;
		font-size: 0.86rem;
		color: var(--ink-muted);
	}

	.muted {
		color: var(--ink-muted);
	}
</style>

async

Loading data

An @await on an API route of this site, which the export of the page holds already, and shows another section for each click.

The <script kotlin> block is plain Kotlin. Its top-level statements become the body of the component function, in order, and run once. It goes at the top level of the file, once, and takes no other attribute.

Component files: Script
Tips.lithe53 lines
Tips.lithe
<script kotlin>
	import io.github.ayfri.lithe.kobweb.apiData
	import website.model.Tip

	var round by state(0)
</script>

<div class="demo tips">
	@await (val tips = apiData<List<Tip>>("tips")) {
		@if (val tip = tips.getOrNull(round * 7919 % tips.size)) {
			<blockquote>
				<p>{tip.text}</p>
				<a href={tip.href}>{tip.page}: {tip.section}</a>
			</blockquote>
		}
	} @pending {
		<p class="waiting">Loading the sections…</p>
	} @catch (error) {
		<p class="failed">No tip this time: {error.message}</p>
	}
	<button type="button" @click={round++}>Another one</button>
</div>

<style>
	blockquote {
		margin: 0 0 0.9rem;
		padding: 0 0 0 1rem;
		border-left: 3px solid var(--accent);
	}

	blockquote p {
		margin: 0 0 0.4rem;
	}

	blockquote a {
		font-size: 0.85rem;
		color: var(--ink-muted);
	}

	.waiting,
	.failed {
		margin: 0 0 0.9rem;
		color: var(--ink-muted);
	}

	button {
		padding: 0.45rem 1rem;
		border: 1px solid var(--rule);
		border-radius: 8px;
		background: var(--paper-raised);
		cursor: pointer;
	}
</style>

components

Tabs from children

A Tabs component that hands the current title to the panel its parent writes between its tags.

Kotlin that runs once: props, state, derived values and effects.

val doubled by derived { count * 2 }
Parts.lithe, Tabs.lithe79 lines
Parts.lithe
<div class="demo">
	<Tabs titles={listOf("Script", "Markup", "Style")}> { tab ->
		@when (tab) {
			"Script" -> {
				<p>Kotlin that runs once: props, state, derived values and effects.</p>
				<code @raw>val doubled by derived { count * 2 }</code>
			}
			"Markup" -> {
				<p>HTML with Kotlin in braces, and blocks that start with an at sign.</p>
				<code @raw>@for (item in items, key = item.id) { <li>{item.name}</li> }</code>
			}
			else -> {
				<p>CSS scoped to the component, with Kotlin values that follow the state.</p>
				<code @raw>.bar { width: ${progress}%; }</code>
			}
		}
	} </Tabs>
</div>

<style>
	p {
		margin: 0 0 0.6rem;
	}

	code {
		display: block;
		padding: 0.5rem 0.8rem;
		border: 1px solid var(--rule);
		border-radius: var(--radius);
		background: var(--paper);
		overflow-x: auto;
		white-space: nowrap;
	}
</style>
Tabs.lithe
<script kotlin>
	prop val titles: List<String>
	/** The panel, which receives the title of the current tab. */
	prop val children: @Markup (String) -> Unit

	var current by state(titles.first())
</script>

<div class="tabs">
	<div class="list" role="group">
		@for (title in titles, key = title) {
			<button type="button" aria-pressed={current == title} @click={current = title}>{title}</button>
		}
	</div>
	<div class="panel">
		@children(current)
	</div>
</div>

<style>
	.list {
		display: flex;
		border-bottom: 1px solid var(--rule-strong);
	}

	button {
		margin-bottom: -1px;
		padding: 0.45rem 1rem;
		border: 0;
		border-bottom: 2px solid transparent;
		background: none;
		font: 500 0.82rem var(--mono);
		color: var(--ink-muted);
		cursor: pointer;
	}

	button[aria-pressed="true"] {
		border-bottom-color: var(--accent);
		color: var(--ink);
	}

	.panel {
		padding-top: 0.9rem;
	}
</style>

components / bindings

Rating input

A Rating component with a bindable prop: the form binds its value like it would an input.

Great
120 characters left
Feedback.lithe, Rating.lithe115 lines
Feedback.lithe
<script kotlin>
	val LIMIT = 120
	val moods = listOf("Poor", "Fair", "Good", "Great", "Excellent")

	var stars by state(4)
	var comment by state("")
	var sent by state(false)

	val left by derived { LIMIT - comment.length }
</script>

<form class="demo" @submit={it.preventDefault(); sent = true}>
	<div class="rate">
		<Rating value={::stars} />
		<span>{moods.getOrElse(stars - 1) { "Pick a rating" }}</span>
	</div>
	<textarea rows="3" maxlength={LIMIT} placeholder="What did you think?" aria-label="Comment" value={::comment}></textarea>
	<div class="send">
		<small class={"low" on (left < 20)}>{left} characters left</small>
		<button type="submit" disabled={stars == 0}>Send</button>
	</div>
	@if (sent) {
		<p>Thanks, {stars} out of 5 noted.</p>
	}
</form>

<style>
	.demo {
		display: grid;
		gap: 0.7rem;
	}

	.rate {
		display: flex;
		align-items: center;
		gap: 0.8rem;
		font: 0.82rem var(--mono);
		color: var(--ink-muted);
	}

	textarea {
		padding: 0.5rem 0.7rem;
		border: 1px solid var(--rule-strong);
		border-radius: var(--radius);
		background: var(--paper);
		resize: vertical;
	}

	.send {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	small {
		font: 0.78rem var(--mono);
		color: var(--ink-muted);
	}

	.low {
		color: var(--bad);
	}

	button {
		padding: 0.45rem 1rem;
		border: 0;
		border-radius: var(--radius);
		background: var(--grad);
		color: var(--on-grad);
		font-weight: 500;
	}

	button:disabled {
		opacity: 0.4;
	}
</style>
Rating.lithe
<script kotlin>
	prop var value: Int = 0
	prop val max: Int = 5

	var hover by state(0)
	val lit by derived { if (hover > 0) hover else value }
</script>

<div class="rating" role="radiogroup" aria-label="Rating" @pointerleave={hover = 0}>
	@for (star in 1..max, key = star) {
		<button type="button" role="radio" aria-checked={value == star} aria-label="{star} of {max}" class={"on" on (star <= lit)}
			@click={value = star} @pointerenter={hover = star}>★</button>
	}
</div>

<style>
	.rating {
		display: inline-flex;
	}

	button {
		padding: 0 0.1rem;
		border: 0;
		background: none;
		font-size: 1.6rem;
		line-height: 1.2;
		color: var(--rule-strong);
		cursor: pointer;
		transition: color 0.1s, scale 0.1s;
	}

	button:active {
		scale: 0.85;
	}

	.on {
		color: var(--accent);
	}
</style>

events / lists

Drawing with state

An SVG whose dots are a keyed list and whose line is derived from them: click to add a point, click a point to remove it.

4 points, click the plane to add one and a point to remove it.

Draw.lithe75 lines
Draw.lithe
<script kotlin>
	class Point(val id: Int, val x: Double, val y: Double)

	var points by state(listOf(Point(1, 20.0, 78.0), Point(2, 65.0, 30.0), Point(3, 115.0, 58.0), Point(4, 175.0, 14.0)))
	var nextId = 5

	/** The line follows the points from left to right, whatever order they were added in. */
	val line by derived { points.sortedBy { it.x }.joinToString(" ") { "${it.x},${it.y}" } }
</script>

<div class="demo">
	<svg viewBox="0 0 200 100" role="img" aria-label="A line through {points.size} points"
		@click={
			val box = it.currentTarget.getBoundingClientRect()
			points = points + Point(nextId++, (it.clientX - box.left) / box.width * 200, (it.clientY - box.top) / box.height * 100)
		}>
		<polyline points={line} />
		@for (point in points, key = point.id) {
			<circle cx={point.x} cy={point.y} r="4" @click={it.stopPropagation(); points = points - point} />
		}
	</svg>
	<p>
		<span>{points.size} points, click the plane to add one and a point to remove it.</span>
		<button type="button" @click={points = emptyList()}>Clear</button>
	</p>
</div>

<style>
	svg {
		display: block;
		width: 100%;
		border: 1px solid var(--rule);
		border-radius: var(--radius);
		background: radial-gradient(var(--grid) 1px, transparent 1px) 0 0 / 10px 10px, var(--paper);
		cursor: crosshair;
	}

	polyline {
		fill: none;
		stroke: var(--accent);
		stroke-width: 1.5;
		stroke-linejoin: round;
	}

	circle {
		fill: var(--paper);
		stroke: var(--accent);
		stroke-width: 1.5;
		cursor: pointer;
		transition: r 0.12s;
	}

	circle:hover {
		r: 5.5;
	}

	p {
		display: flex;
		align-items: center;
		justify-content: space-between;
		gap: 1rem;
		margin: 0.6rem 0 0;
		font: 0.78rem var(--mono);
		color: var(--ink-muted);
	}

	button {
		padding: 0.25rem 0.7rem;
		border: 1px solid var(--rule-strong);
		border-radius: var(--radius);
		background: var(--paper-raised);
		font: inherit;
		cursor: pointer;
	}
</style>

state / events

Stopwatch

An effect that starts an interval while the watch runs, and cleans it up when it stops or leaves.

0:00.00
    Stopwatch.lithe85 lines
    Stopwatch.lithe
    <script kotlin>
    	import web.timers.clearInterval
    	import web.timers.setInterval
    	import kotlin.time.TimeSource
    
    	var running by state(false)
    	var elapsed by state(0)
    	var laps by state(emptyList<Lap>())
    
    	/** Runs again when `running` changes: the interval of the last run stops before the next one starts, and when the component leaves. */
    	effect {
    		if (!running) return@effect
    		val base = untracked { elapsed }
    		val start = TimeSource.Monotonic.markNow()
    		val timer = setInterval({ elapsed = base + start.elapsedNow().inWholeMilliseconds.toInt() }, 31)
    		onDispose { clearInterval(timer) }
    	}
    
    	fun reset() {
    		running = false
    		elapsed = 0
    		laps = emptyList()
    	}
    </script>
    
    <div class="demo stopwatch">
    	<output>{clock(elapsed)}</output>
    	<div class="buttons">
    		<button type="button" @click={running = !running}>{if (running) "Stop" else "Start"}</button>
    		<button type="button" disabled={!running} @click={laps = listOf(Lap(laps.size + 1, elapsed)) + laps}>Lap</button>
    		<button type="button" disabled={running || elapsed == 0} @click={reset()}>Reset</button>
    	</div>
    	<ol>
    		@for (lap in laps, key = lap.number) {
    			<li><span>Lap {lap.number}</span> {clock(lap.millis)}</li>
    		}
    	</ol>
    </div>
    
    <style>
    	output {
    		display: block;
    		font: 600 2.4rem/1 var(--mono);
    		font-variant-numeric: tabular-nums;
    	}
    
    	.buttons {
    		display: flex;
    		gap: 0.5rem;
    		margin: 0.9rem 0;
    	}
    
    	button {
    		padding: 0.45rem 1rem;
    		border: 1px solid var(--rule);
    		border-radius: 8px;
    		background: var(--paper-raised);
    		cursor: pointer;
    	}
    
    	button:disabled {
    		opacity: 0.45;
    		cursor: default;
    	}
    
    	ol {
    		margin: 0;
    		padding: 0;
    		list-style: none;
    		max-height: 8.5rem;
    		overflow-y: auto;
    		font: 0.9rem var(--mono);
    	}
    
    	li {
    		display: flex;
    		justify-content: space-between;
    		padding: 0.2rem 0;
    		border-bottom: 1px solid var(--rule);
    	}
    
    	li span {
    		color: var(--ink-muted);
    	}
    </style>

    lists

    Moving rows

    Rows that move keep their nodes: type in a field while the list shuffles, the text and the focus stay.

    1. Ada
    2. Grace
    3. Barbara
    4. Margaret
    5. Radia
    Shuffle.lithe55 lines
    Shuffle.lithe
    <script kotlin>
    	import web.timers.clearInterval
    	import web.timers.setInterval
    
    	var names by state(listOf("Ada", "Grace", "Barbara", "Margaret", "Radia"))
    	var shuffling by state(false)
    
    	effect {
    		if (!shuffling) return@effect
    		val timer = setInterval({ names = names.shuffled() }, 1200)
    		onDispose { clearInterval(timer) }
    	}
    </script>
    
    <div class="demo shuffle">
    	<label><input type="checkbox" checked={::shuffling}> Shuffle every second</label>
    	<ol>
    		@for (name in names, key = name) {
    			<li><span>{name}</span> <input placeholder="Type while it moves" aria-label="Note about {name}"></li>
    		}
    	</ol>
    </div>
    
    <style>
    	label {
    		display: flex;
    		gap: 0.5rem;
    		align-items: center;
    		cursor: pointer;
    	}
    
    	ol {
    		margin: 0.8rem 0 0;
    		padding: 0;
    		list-style: none;
    	}
    
    	li {
    		display: grid;
    		grid-template-columns: 6rem 1fr;
    		align-items: center;
    		padding: 0.25rem 0;
    	}
    
    	li input {
    		padding: 0.35rem 0.6rem;
    		border: 1px solid var(--rule);
    		border-radius: 8px;
    		background: var(--paper);
    	}
    
    	input[type="checkbox"] {
    		accent-color: var(--accent);
    	}
    </style>

    async / components

    Error boundary

    A gauge that throws past 100: the boundary shows its fallback in its place, and retries on demand.

    60 %

    Boundary.lithe, Gauge.lithe75 lines
    Boundary.lithe
    <script kotlin>
    	var level by state(60.0)
    </script>
    
    <div class="demo">
    	<label>Level <input type="range" min="0" max="150" valueAsNumber={::level}> {level}</label>
    	@try {
    		<Gauge value={level.toInt()} />
    	} @catch { error, retry ->
    		<p class="failed">The gauge failed: {error.message}</p>
    		<button type="button" @click={level = 100.0; retry()}>Set it to 100 and retry</button>
    	}
    </div>
    
    <style>
    	label {
    		display: flex;
    		align-items: center;
    		gap: 0.6rem;
    		margin-bottom: 0.9rem;
    		font-variant-numeric: tabular-nums;
    	}
    
    	input {
    		flex: 1;
    		accent-color: var(--accent);
    	}
    
    	.failed {
    		margin: 0 0 0.6rem;
    		color: var(--bad);
    	}
    
    	button {
    		padding: 0.45rem 1rem;
    		border: 1px solid var(--rule);
    		border-radius: 8px;
    		background: var(--paper-raised);
    		cursor: pointer;
    	}
    </style>
    Gauge.lithe
    <script kotlin>
    	prop val value: Int
    
    	/** Throws for a value out of range, while the gauge builds or once the page updates it. */
    	val percent by derived {
    		check(value in 0..100) { "a gauge reads 0 to 100, not $value" }
    		value
    	}
    </script>
    
    <div class="gauge" role="meter" aria-valuemin="0" aria-valuemax="100" aria-valuenow={percent}>
    	<span style={"width" to "$percent%"}></span>
    </div>
    <p class="reading">{percent} %</p>
    
    <style>
    	.gauge {
    		height: 0.8rem;
    		border-radius: 999px;
    		background: var(--paper-sunk);
    		overflow: hidden;
    	}
    
    	span {
    		display: block;
    		height: 100%;
    		background: var(--accent);
    	}
    
    	.reading {
    		margin: 0.4rem 0 0;
    		font: 0.9rem var(--mono);
    	}
    </style>

    styles / bindings

    Styles from Kotlin

    Kotlin values in the style block, which become CSS custom properties the runtime updates.

    oklch(0.7 0.17 30)
    Swatch.lithe40 lines
    Swatch.lithe
    <script kotlin>
    	var hue by state(30.0)
    	var corner by state(14.0)
    </script>
    
    <div class="demo swatch">
    	<div class="sample">oklch(0.7 0.17 {hue})</div>
    	<label>Hue <input type="range" min="0" max="360" valueAsNumber={::hue}></label>
    	<label>Corners <input type="range" min="0" max="48" valueAsNumber={::corner}></label>
    </div>
    
    <style>
    	.swatch {
    		display: grid;
    		gap: 0.7rem;
    	}
    
    	.sample {
    		display: grid;
    		place-items: center;
    		height: 6.5rem;
    		background: oklch(0.7 0.17 $hue);
    		border-radius: ${corner}px;
    		color: #fff;
    		font: 600 0.95rem var(--mono);
    		text-shadow: 0 1px 2px rgb(0 0 0 / 0.35);
    	}
    
    	label {
    		display: flex;
    		align-items: center;
    		gap: 0.6rem;
    		font-size: 0.9rem;
    	}
    
    	input {
    		flex: 1;
    		accent-color: oklch(0.7 0.17 $hue);
    	}
    </style>

    events

    Page values

    The size and the scroll of the window as values that follow the page, and a listener of the window.

    Window
    1280 × 720 px
    Scrolled
    0 px
    Last key
    none yet
    PageValues.lithe55 lines
    PageValues.lithe
    <script kotlin>
    	import web.window.window
    
    	val width by window.innerWidth()
    	val height by window.innerHeight()
    	var scroll by window.scrollY()
    	var lastKey by state("none yet")
    
    	window.onkeydown { lastKey = it.key }
    </script>
    
    <dl class="demo values">
    	<dt>Window</dt>
    	<dd>{width} × {height} px</dd>
    	<dt>Scrolled</dt>
    	<dd>{scroll.toInt()} px <button type="button" @click={scroll = 0.0}>Back to top</button></dd>
    	<dt>Last key</dt>
    	<dd><kbd>{lastKey}</kbd></dd>
    </dl>
    
    <style>
    	.values {
    		display: grid;
    		grid-template-columns: max-content 1fr;
    		gap: 0.5rem 1.2rem;
    		align-items: center;
    	}
    
    	dt {
    		color: var(--ink-muted);
    		font-size: 0.88rem;
    	}
    
    	dd {
    		margin: 0;
    		font-family: var(--mono);
    		font-variant-numeric: tabular-nums;
    	}
    
    	button {
    		margin-left: 0.6rem;
    		padding: 0.2rem 0.6rem;
    		border: 1px solid var(--rule);
    		border-radius: 6px;
    		background: var(--paper-raised);
    		font: 0.8rem var(--sans);
    		cursor: pointer;
    	}
    
    	kbd {
    		padding: 0.1rem 0.4rem;
    		border: 1px solid var(--rule);
    		border-radius: 5px;
    	}
    </style>