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

Component files

A .lithe file is one component. The file name is the component name (Counter.lithe is Counter), and the folder under src/ is the package, like in Kotlin (src/ui/Counter.lithe is ui.Counter). The file holds three parts, each optional:

  • a <script kotlin> block with the Kotlin of the component,
  • the markup,
  • a <style> block, scoped to the component (see Styles).
Lithe
<script kotlin>
	prop val start: Int = 0
	prop val step: Int = 1

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

<button @click={count += step}>{count} × 2 = {doubled}</button>

<style>
	button { padding: .5rem 1rem; }
</style>

A tag starting with an uppercase letter is a component. A lowercase one is an HTML element, or calls a variable when HTML has no element of that name. Components of the same package need no import, and others are imported in <script kotlin> like any Kotlin declaration.

The compiler turns the file into a Kotlin function that runs once and builds the page, and the parts that can change update on their own:

Kotlin
context(_: Ui)
fun Counter(start: (() -> Int)? = null, step: (() -> Int)? = null) {
	val start: Int by prop(start) { 0 }
	val step: Int by prop(step) { 1 }
	var count by state(start)
	val doubled by derived { count * 2 }
	Element("button", { on("click") { count += step } }) { Text { "${textOf(count)} × 2 = ${textOf(doubled)}" } }
}

Generated code has the rest of what it writes.

Script

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.

Any other <script> is part of the markup. It's written to the page as it is and its content is never parsed, so JSON-LD and third-party scripts need no escaping:

Lithe
<script type="application/ld+json">{"@context": "https://schema.org", "@type": "WebSite"}</script>
<script async src="https://stats.example.com/s.js"></script>

Declarations outside the component

The script only holds the component body, plus import lines at its top. Classes, helper functions and constants shared by every instance go in regular .kt files next to the component.

Kotlin
import com.example.todo.Todo

prop val todos: List<Todo>

package isn't possible in the script, because the package comes from the folder of the file.

Props

Declare each prop on its own line with prop val, typed, with an optional default value. Props become the parameters of the component function, in declaration order, so a prop without a default value is required. Reading a prop always gives the value the parent passes right now.

Kotlin
prop val label: String
prop val size: Size = Size.Md
prop val select: (event: SelectEvent) -> Any? = {}

A default value that continues on the next line goes in parentheses, since the declaration ends with its line.

Bindable props

A prop var is bindable: the component can assign it, and 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 until the parent passes another value.

Lithe
<!-- Slider.lithe -->
<script kotlin>
	prop var value: Double = 0.0
</script>

<input type="range" valueAsNumber={::value}>
Kotlin
context(_: Ui)
fun Slider(value: (() -> Double)? = null, onValueChange: (() -> (Double) -> Unit)? = null)

Rest prop

A *prop receives the attributes and events a parent gives that no other prop takes. Its type names the element they are for, and {*attributes} writes them on one:

Lithe
<!-- ui/Button.lithe -->
<script kotlin>
	import web.html.HTMLButtonElement

	prop val label: String
	*prop val attributes: Attributes<HTMLButtonElement>
</script>

<button class="action" {*attributes}>{label}</button>
Lithe
<script kotlin>
	import ui.Button

	var saving by state(false)

	fun save(x: Double) {
		saving = true
	}
</script>

<Button label="Save" disabled={saving} data-kind="primary" @click={save(it.clientX)} />
  • The parent is checked against the element: href on this <Button> is a compile error, because <button> has no such attribute. The allowed names are the ones the HTML specification gives the element, the ones every element has, data-* and aria-*. Values aren't typed: they are written like the ones of an attribute, so true makes a bare one and false or null leaves it out.
  • An event is typed from the element (it.currentTarget is an HTMLButtonElement). It is added next to the listener the element has for the same event, so both run.
  • The spread follows the usual rule, the later one wins: the parent's class replaces the class="action" written before the spread, and an attribute written after the spread overrides the parent's. The scope class of the styles stays.
  • style={...} lists declarations like on an element.
  • attributes reads as a Map<String, String?>: attributes["title"] follows the parent's value.
  • Attributes<HTMLElement> takes the attributes every element has, and spreads on any element. A type shared by several tags takes theirs, such as HTMLTableCellElement for <td> and <th>.
  • A component has one *prop, a val without default. Its type is Attributes of an HTML element (HTMLButtonElement) or of an SVG one (SVGRectElement, SVGElement for any). The attribute names of an SVG element aren't checked, since no table of them is kept.
  • Another component receives the whole of it by name, <Button label="x" {attributes} />, when its *prop has the same name. Spreading it on a component or binding one of its attributes isn't possible.

Names

  • The file name is an uppercase identifier, because it names the component. Text, Element, ClonedElement, StaticHtml, Comment, RawElement, Choose, Each, Key, Await, Head, Ui and Markup are reserved by the runtime.
  • A prop named like a Kotlin keyword is fine, the generated code puts it in backticks.
  • onNameChange is the callback generated for prop var name, so no other prop takes that name.