# Generated code

The compiler turns a `.lithe` file into two Kotlin files, one for the browser and one for static rendering, or reports every error it finds. Here is the
counter of the [introduction](/docs/), as the compiler of this site's server writes it, or as the build of the site wrote it when no server answers, like the
[playground](/playground) shows it for its examples:


## The component function

Each component is one function named after the file, with the `Ui` it renders into as a context parameter, a parameter per prop in order, and a callback
after each `prop var`. Every prop is an accessor, so a component follows what its parent passes:

```kotlin
context(_: Ui)
fun Counter(
	start: (() -> Int)? = null,
	value: (() -> Int)? = null,
	onValueChange: (() -> (Int) -> Unit)? = null,
) {
	val start: Int by prop(start) { 0 }
	var value: Int by bindable(prop(value) { 0 }, onValueChange)
	var count by state(start)
	Element("button", {
		on("click") {
			count++
		}
	}) {
		Text { textOf(count) }
	}
}
```

- A required prop is `name: () -> T`. A default value is computed once, unless it reads state.
- A rest prop is `(() -> Attributes<E>)? = null`, empty when the parent gives nothing.
- A `@Markup` type of a prop, a `@fun` parameter, a `@val` or a script declaration is rewritten so each of its parameters is an accessor
  (`@Markup (User) -> Unit` is `(() -> User) -> Unit`), so markup follows the values it receives.
- Script statements are written as they are.

## Markup

- An element whose output never changes is one `StaticHtml` string, scope classes included. Adjacent texts and expressions are one `Text`. A text or an
  attribute without any expression, or whose expressions are constant, is written once, the others are lambdas. A constant expression joins literals and
  the names of script `val`s that hold one with operators: a call, a member, a template or a keyword makes it a lambda, because it could read state.
- Attributes are written in source order, `class` merged with the scope class, `style` with the custom properties. A list is one `spaced(...)` call. An
  element with a spread writes every attribute in one map, later entries overriding earlier ones.
- Listeners, bindings and element hooks come after the attributes, so a bound input has its `type`, `min` and `max` first. A listener that reads `it` or
  names its parameter is typed from its event and element.
- Each `<head>` is a `Head` call before the markup, so the block of a component runs before the ones of the components it contains.

## Blocks

| Block | Becomes |
|-------|---------|
| `@if` | A `Choose` over the branch index. A branch that checks a delegated name (`x != null`, `x is T`) redeclares it with the checked type, since Kotlin can't smart cast a delegated name. |
| `@if (val name = expr)` | `expr` computed before the `Choose`, which tests it against `null`, and the branch reads it as `name`. |
| `@when` | A `Choose` over the index a Kotlin `when` returns. A branch that reads the subject redeclares it with the type of its `is` condition. |
| `@for` | An `Each` over `loopItems(items)`, each name of the pattern a delegate of the item accessor. |
| `@key` | A `Key` over its value. |
| `@val` | A `val name by derived { value }`. |
| `@fun` | A local function whose parameters are accessors, written as early as what it reads allows. |
| `@await` | An `Await` whose first lambda is the expression, followed by `pending`, `catch` and the content. |
| `@try` | A `Try` around the content, with the `@catch` as its fallback. |
| `<{...}>` | A `Key` over its name, checked by `tagName`, around an `Element` of that name. |
| A variable tag | A call of the name with one accessor per attribute: `<view {user} />` is `view({ user })`. |
| A composable tag | A `<div style="display:contents">` that composes the name with its arguments. |

In the browser output, a `@for` row that is one element with elements inside, and only elements, texts and comments, is cloned from a template that holds
its tags and what never changes, and a comparison of the key with a state (`selected == row.id`) is a selector that only runs the bindings of the old and
the new row.

## The two outputs

The output for static rendering leaves out what only runs in the browser:

- Statements that call `mount` or `effect` anywhere, `onDispose` statements, element listeners, `@ref`, `@mount` and the listeners of bindings.
- Every statement of the script that uses a browser API, directly or through another statement left out, and those imports.
- The body of a component event handler that uses a browser API: the prop keeps an empty lambda.

A declaration that follows a [page value](/docs/components/events#page-values), such as `val width by window.innerWidth()`, stays as a state of the value
static rendering takes (`val width by state(0)`). A declaration with a `@Composable` type stays as `val name: Any? = null`, so the markup and the
components that pass it compile. In that output, a default value that reads state is a `lazy`, and so are `@val` and the value of `@if (val ...)`, since a
derived value computes at each read there.

## Whitespace

Only ASCII whitespace counts, a no-break space stays. A run becomes one space. A whitespace-only text next to a block-level sibling is removed, and so is any
whitespace-only text between the children of a list, table or `<select>`. The edges of a block-level element, of the file, of the braces of a block and of
a component's content are trimmed, an inline parent keeps one space. Component tags count as inline. `<pre>` and `<textarea>` keep their content exactly
except the first newline after the start tag, which the HTML parser drops, and `<script>` and `<style>` are never touched.

## Where to read it

- An [editor](/docs/tooling/editors#generated-code) opens both outputs of a component and its CSS from a lens at its top.
- A [site build](/docs/sites/static-sites) writes them to `build/lithe/<module>/generated`, and the Gradle plugin to `build/generated/lithe/kotlin`.
- The [playground](/playground) shows them for a file you write.

## Limits

- An `@await` expression that needs a browser API isn't possible, because static rendering runs it: declare a loader once in `src@jvm` and once in `src@js`.
- Two `@fun` that render each other aren't possible, because Kotlin local functions only see the ones above them.
- `@html` next to other content isn't possible, it replaces the whole content of its element.
- A component placed directly in a `<table>` must render its own `<tbody>`, `<thead>` or `<tfoot>`: the compiler reads one file at a time, so it can't check
  the rows a component renders.
- The stack trace of an exception thrown during static rendering names the generated files, since only the build log of the Kotlin compiler is mapped back
  to the `.lithe` files.
