- Docs
- Components
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).
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:
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:
<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.
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.
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.
<!-- Slider.lithe -->
<script kotlin>
prop var value: Double = 0.0
</script>
<input type="range" valueAsNumber={::value}>
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:
<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:
hrefon 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-*andaria-*. Values aren't typed: they are written like the ones of an attribute, sotruemakes a bare one andfalseornullleaves it out. - An event is typed from the element (
it.currentTargetis anHTMLButtonElement). 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
classreplaces theclass="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.attributesreads as aMap<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 asHTMLTableCellElementfor<td>and<th>.- A component has one
*prop, avalwithout default. Its type isAttributesof an HTML element (HTMLButtonElement) or of an SVG one (SVGRectElement,SVGElementfor 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*prophas 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,UiandMarkupare reserved by the runtime. - A prop named like a Kotlin keyword is fine, the generated code puts it in backticks.
onNameChangeis the callback generated forprop var name, so no other prop takes that name.