# The kit

The components that come with Mote: how they work, the everyday ones, and an
index into the pages under [kit/](kit/). Back to the [map](README.md).

```mote
// docs: whole
state name = ""
state agree = false
state plan = "Free"
state asking = false

action save
  toast("Saved", name)
  asking = false

column gap=12 padding=24 max-width=420
  Card title="Account" description="A few details."
    Label "Name"
    Input value=name placeholder="Ada Lovelace"
    Select value=plan options=["Free", "Pro"]
    Checkbox "I agree" checked=agree
  Button "Save" press={asking = true} disabled=(!agree)
  Dialog open=asking title="Save it?" description="You can change it later."
    row gap=8 justify-content=flex-end
      Button "Cancel" press={asking = false} variant="outline"
      Button "Save" press=save
```

## Mental model

A kit component is an ordinary Mote component, written in Mote
(`kit/*.mote`), built into the compiler. Nothing is imported: **a capitalised
tag that the page does not declare itself is looked for in the kit**
(`compiler/src/build.rs`, `with_kit`).

- **A page carries only the components it uses**, and the ones those use.
- **A page's own component of the same name takes the kit's place**, for
  the kit's own components too: the kit's is not brought in beside it.
- **They follow the colour roles** (`fg`, `bg`, `card`, `line`, `dim`,
  `accent`, `danger`) and the scheme, so they match the page and follow
  light and dark. Set `app … accent=#…` to colour them.
- A kit component sees none of the page's names: only its props. Nor is it
  touched by them: a page's `action picked` and the Calendar's own
  `fn picked` are two things, wherever the Calendar is written.

Everything in [markup.md](markup.md#components) holds for them:

- **A prop the component changes is written back** to what the page passed,
  when that can be written: a `Switch`'s `on`, an `Input`'s `value`, a
  `Dialog`'s `open`.
- **`Switch`, `Checkbox` and `Toggle` can be driven instead**: `value=` in,
  `change=` out, nothing written back ([below](#driven-switches)).
- **An event's name is a handler**: `Button "Save" press=save`.
- **What is not a prop goes on the component's first element**: `id`,
  `class`, `aria-*`, events, and styles, which win over its own:
  `Button "Save" width=100% id="save"`. Where that element is only a
  wrapper that takes no room (`AlertDialogAction`, `AlertDialogCancel`,
  `DialogClose`, `DrawerClose`: a form around a button), it goes on the
  button: `AlertDialogAction "Delete" id="yes"` is pressed at `#yes`.
- **A prop that was not given writes nothing**: a `Button` with no `title`
  has no `title` attribute.
- **`Select` can be set by a script**, with `options=` and with `items=`
  alike: a native `select` with the same choices is inside it (hidden), so
  a test or an agent reads the list from its `option`s and chooses by
  setting its value and sending `input`.
- **Children, and a positional text, go where the component writes `slot`.**

The kit follows shadcn/ui's components and AI Elements' under their
original part names, with three differences ([kit/more.md](kit/more.md),
"How parts are written in Mote"):

- **No triggers.** What floats (a menu, a popover, a tooltip) is written
  under the element that opens it.
- **`of=` is the shared value.** A part cannot see the part around it, so
  where the original shares a value through context, each part takes it:
  `TabsTrigger value="a" of=tab`, `TabsContent value="a" of=tab`.
- **Short forms.** The common components work in one line:
  `Select value= options=`, `Tabs value= options=`, `Dialog open= title=`.

## The everyday components

`kit/kit.mote`. Each line compiles as written (with the state it names).

```text
Button "Save" press=save variant="outline" size="sm"
    variant: default secondary outline ghost destructive link
    size: xs sm md lg icon (icon-xs icon-sm icon-lg) · disabled
Badge "New" variant=…              Card title="…" description="…"
Label "Name"                       Kbd "⌘K"
Separator                          Separator vertical=true
Input value=name placeholder="…" type="email"
Textarea value=bio rows=4
Checkbox "I agree" checked=ok      Switch on=wifi      (driven: value= change=)
Slider value=n min=0 max=100 step=1
Select value=fruit options=["Apple", "Pear"]
Radio value=plan options=[…]
Tabs value=tab options=[…]         (the page shows what belongs to `tab`)
Toggle "B" on=bold
Accordion title="…"                Alert title="…" variant="destructive"
Progress value=40                  (of 100; `max=` changes that)
Avatar name="Ada Lovelace" src=…   Skeleton width="60%"      Spinner
Feed stuck=atEnd                   a column that stays at its end as it grows
Combobox value=city options=[…] placeholder="…"      (type to narrow)
Pagination page=p pages=9
```

### Driven switches

`Switch on=wifi` writes `wifi` as soon as it is pressed. When the value is
not the page's to write (a row the back end keeps, a setting that must be
agreed first), pass it as `value=` and take the press as `change=`:

```mote
// docs: whole
shape Channel
  id: string
  muted: bool

state channels: Channel[] = [{id: "general", muted: false}]
state wifi = true

action mute(id: string, on: bool)
  // (Ask the back end here; the switch moves when the row does.)
  let c = channels.find(c => c.id == id)
  if c != none c.muted = on

column gap=8 padding=16
  Switch on=wifi
  for c in channels key=c.id
    Switch value=c.muted change={mute(c.id, it)}
    Checkbox "Muted" value=c.muted change={mute(c.id, it)}
    Toggle "M" value=c.muted change={mute(c.id, it)}
```

| | Two-way | Driven |
|---|---|---|
| `Switch` | `on=flag` | `value=row.on change=handler` |
| `Toggle` | `on=flag` | `value=row.on change=handler` |
| `Checkbox` | `checked=flag` | `value=row.on change=handler` |

- With `value=`, the component shows `value` and nothing else: a press does
  not move it. `change` is handed the bool it would become: `it` in a block
  (`change={mute(c.id, it)}`), or the parameter of an action named alone
  (`change=mute` with `action mute(on: bool)`). The switch moves when the
  page changes what it passes.
- `change=` may be given with the two-way form too: it is told after the
  write.

Under what opens them:

```mote
Button "Options"
  Menu
    MenuLabel "Actions"
    MenuItem "Rename" press=rename
    MenuSeparator
    MenuItem "Delete" press=remove
Button "Info"
  Popover
    text "Anything"
```

Over the page:

```mote
Dialog open=asking title="…" description="…"
Sheet open=menu title="…" side="left"
```

Both are the browser's modal dialog: Escape and a click outside close them,
and `open` is written back. A plain `dialog open=flag` does the same.

Writing mail and documents ([kit/richtext.md](kit/richtext.md)): the
document is the page's value, and mail is made of it.

```mote
richtext value=doc placeholder="Write…" aria-label="Message" height=280
text richtext.html(doc)
```

Code and documents ([text.md](text.md#highlight-codeblock-and-markdown)):

```mote
Highlight text=code lang="ts" lines start=12 wrap marks=[{line: 14, kind: "added"}]
CodeBlock code=… language="ts" title="a.ts"
Markdown text=answer streaming=busy
```

**The screen's edges.** In a program that says
[`app … edges=cover`](markup.md#the-screens-edges), the components that
touch them leave a phone's notch and home bar free. One that does is
written with the length in an attribute of its own
(`padding=24 padding-bottom="max(24px, env(safe-area-inset-bottom))"`): in
any other program the kit's attributes that name `env(safe-area-inset-…)`
are left out, so they cost it nothing.

`toast("Saved", "A second line", "error")` is a built-in call, not a
component ([language.md](language.md#built-in)).

```mote
// docs: whole
state tab = "Profile"
state wifi = true
state volume = 40
state city = ""
state page = 1

action rename
  toast("Renamed")

column gap=12 padding=24 max-width=480
  Tabs value=tab options=["Profile", "Sound"]
  if tab == "Profile"
    Combobox value=city options=["Oslo", "Rome", "Lima"] placeholder="City"
    Button "Options" variant="outline"
      Menu
        MenuItem "Rename" press=rename
  else
    Switch on=wifi
    Slider value=volume min=0 max=100 step=1
    Progress value=volume
  Separator
  Pagination page=page pages=9
```

## The index

Seven files, about 770 components. Each page lists every component with its
props, and names its gallery and its test.

| Page | Source | Holds |
|---|---|---|
| (this page) | `kit/kit.mote` | The 31 everyday components above. |
| [kit/base.md](kit/base.md) | `kit/base.mote` | shadcn/ui, Accordion to Dropdown Menu: AlertDialog, AspectRatio, Avatar's parts, Breadcrumb, ButtonGroup, Calendar, DatePicker, Card's parts, Carousel, charts (Bar, Line, Area, Pie, Radar, Radial), Collapsible, Command, ContextMenu, DataTable, Dialog's parts, Drawer, DropdownMenu. |
| [kit/more.md](kit/more.md) | `kit/more.mote` | shadcn/ui, Empty to Typography: Empty, Field, HoverCard, InputGroup, InputOTP, Item, Menubar, NativeSelect, NavigationMenu, Pagination's parts, RadioGroup, Resizable, ScrollArea, Select's parts, Sheet's parts, Sidebar, Sonner (Toaster), Table, Tabs' parts, ToggleGroup, Tooltip, Typography. Also how parts are written in Mote. |
| [kit/chat.md](kit/chat.md) | `kit/chat.mote` | Chat building blocks and AI patterns: Message, Bubble, MessageScroller, Attachment, PromptInput, Reasoning, Sources, Tool, Task, Plan, ChainOfThought, Confirmation, Queue, Suggestions, Context, InlineCitation, FileTree, ModelSelector, StackTrace, Terminal, TestResults, Questionnaire, Trace. |
| [kit/ai.md](kit/ai.md) | `kit/ai.mote` | The rest of AI Elements: Conversation, Artifact, Commit, EnvironmentVariables, Sandbox, SchemaDisplay, Snippet, WebPreview, HtmlPreview, PackageInfo, AudioPlayer, MicSelector, VoiceSelector, SpeechInput, Transcription, Persona, Image, OpenIn, Shimmer. |
| [kit/code.md](kit/code.md) | `kit/code.mote` | Highlight, CodeBlock and its parts. |
| [kit/markdown.md](kit/markdown.md) | `kit/code.mote` | Markdown: what it reads, how it is laid out, streaming. |
| [kit/flow.md](kit/flow.md) | `kit/flow.mote`, `runtime/page/flow.mote` | The `flow` element, and Canvas, Node, Controls, Panel, Toolbar over it. |
| [kit/editor.md](kit/editor.md) | `runtime/page/editor.mote` | The `editor` element: attributes, events, keys, find. |
| [kit/richtext.md](kit/richtext.md) | `runtime/page/richtext.mote`, `runtime/shared/richtext.mote` | The `richtext` element, a rich text editor: attributes, events, keys, the document and its operations, mail of it (`richtext.html`, `richtext.text`), input methods, paste, what a screen reader is told. |
| [kit/terminal.md](kit/terminal.md) | `runtime/page/terminal.mote` | The `terminal` element: output, events, what keys send, escape sequences. |

To find a component: `grep -n "^component Name(" kit/*.mote` gives its
props, with a comment above it that says its variants.

Galleries are `examples/kit/<name>.mote` (run one with
`mote dev examples/kit/base.mote`); `examples/kit/app.mote` is a small tour.
`examples/playground` shows the whole of Mote, kit included, a page per
subject.

## Pitfalls

- **A lone word straight after a component's name** is a `bool` prop of
  that name, true, where the component has one and nothing in scope has
  the name (`Separator vertical`, `AdminPanel trusted`); otherwise it is the
  component's text, as a name (`Badge label`). After a text or another prop
  a lone word is a boolean: `Highlight text=code lines wrap`, `Button "Go"
  disabled`.
- **A missing prop is named.** `Dialog title="x"` with no `open` is refused
  (`H072`). An unknown one too (`H046`), with the list of props it takes.
- **`Tabs` shows no panels of its own in its short form.** It writes `tab`;
  the page shows what belongs to it with `if` or `match`.
- **Your own component stands in for the kit's everywhere.** A page that
  declares its own `Button` and also uses `Pagination` (which draws
  `Button`s) gets its own `Button` inside the Pagination. It must then take
  what the kit gives it: a prop it lacks is refused (`H046`, at the kit's
  line: `Button` has no prop `variant`). Give yours those props, or
  another name.

## Where a new component goes

A new everyday component: a line in "The everyday components" here. A new
family: its own section in the page for its source file (`kit/base.md`,
`kit/more.md`, …), and its name in the index row. A new source file: a new
page under `kit/`, a row in the index, and its name in `KIT` in
`compiler/src/build.rs`.

## In the source

| What | Where |
|---|---|
| The components | `kit/kit.mote`, `base.mote`, `more.mote`, `chat.mote`, `ai.mote`, `flow.mote`, `code.mote` |
| Embedding the kit, taking only what is used | `compiler/src/build.rs`: `KIT`, `with_kit` |
| A kit component sees only its own names | `compiler/src/types.rs` (`Checker::body`, `built_in`); `compiler/src/emit.rs` (`Gen::items`, `wall`) |
| Components, props, slots | `compiler/src/view.rs`: `Gen::instance` |
| Recorded behaviour | `tests/kit.real.mjs`, `tests/kit_*.real.mjs` (run by `tests/all.sh real_kit real_kitall`) |
