---
title: "Components"
description: "Terminal apps, written in TypeScript and compiled to native executables with scriptc."
canonical_url: "https://tui-sdk.dev/docs/concepts/components"
---
# Components

Every component is drawn from your state on every frame. A component that is left out of a frame starts over the next time it is drawn. Components take a rect to draw in, so you place them with `split`, `inset`, and `center`.

The HTML page runs [`examples/assistant.ts`](https://github.com/vercel-labs/tui/blob/main/examples/assistant.ts) here, in a terminal in the page.

## Parts

A component is a `Root` plus one or more parts. Draw the root with the frame, its props, and a body that draws the parts, for example `Listbox.Root(frame, { id: "fruits", items }, (root) => Listbox.Content(root, list))`. A part takes its parent, a rect, and an optional body and item. A part with a body draws that instead of its own content. The `id` tells two instances of the same component apart and keeps each one's state between frames.

| Component       | Parts                     | What it does                                                                                        |
| --------------- | ------------------------- | --------------------------------------------------------------------------------------------------- |
| `Listbox`       | `Root`, `Content`, `Item` | Picks one item. Up and Down, Home and End, the page keys, and typeahead move; Enter or Space picks. |
| `CheckboxGroup` | `Root`, `Content`         | Checks any number of items. Space checks or unchecks; Enter submits.                                |
| `Tabs`          | `Root`, `List`            | Opens one tab at a time. Left and Right move.                                                       |
| `Table`         | `Root`, `Content`         | Shows rows and columns and picks a row. Header row is drawn by `Content`.                           |
| `Input`         | `Root`, `Field`           | One line of text, edited like a shell.                                                              |
| `Textarea`      | `Root`, `Field`           | Wrapped text with a cursor. Enter starts a new line unless `onSubmit` is given.                     |
| `Dialog`        | `Root`, `Content`         | A box over everything else. While open, keys and focus stay inside it.                              |
| `ScrollArea`    | `Root`, `Viewport`        | Content taller than its view, with a scroll bar.                                                    |

`Listbox.Content` scrolls to the highlight and draws `items` when it is given no body. `ScrollArea.Viewport` draws `lines`, or tells a body how many rows are in view with `total`. `Dialog.Content` draws a border and traps the keys.

## The state contract

Each piece of state follows one contract: a controlled prop, an uncontrolled default, and a change callback.

| State     | Props                                                              |
| --------- | ------------------------------------------------------------------ |
| Value     | `value`, `defaultValue`, `onValueChange(value, details)`           |
| Highlight | `highlighted`, `defaultHighlighted`, `onHighlight(value, details)` |
| Open      | `open`, `defaultOpen`, `onOpenChange(open, details)`               |
| Offset    | `offset`, `defaultOffset`, `onOffsetChange(offset, details)`       |
| Checked   | `value`, `defaultValue`, `onValueChange(value, details)`           |

`details` says why the value changed, how it was caused, and how to stop it. In `onValueChange`, call `details.cancel()` before returning to stop a change; the change callback sees `details.canceled` after it returns. A controlled prop wins over the component's own state. If the prop does not change, drawing the component again shows the old value. `details.reason` is a short string such as `"list-navigation"`, `"item-press"`, `"input-change"`, `"clear-press"`, `"escape-key"`, or `"imperative"`. `details.source` is `"keyboard"`, `"mouse"`, `"paste"`, `"programmatic"`, or `"agent"`.

## Filter and pick

```ts
import assert from "node:assert/strict"
import { createTestApp, type Frame, Input, Listbox, split } from "tui"

const fruits = ["Apple", "Banana", "Cherry"]
const state = { query: "", picked: "" }

function view(frame: Frame) {
  const [search, results] = split(frame.rect, "rows", [1, -1])
  Input.Root(
    frame,
    {
      id: "search",
      label: "Filter",
      value: state.query,
      placeholder: "Filter",
      onValueChange: (value) => {
        state.query = value
      },
    },
    (root) => Input.Field(root, search),
  )
  const items = fruits
    .filter((fruit) => fruit.toLowerCase().includes(state.query.toLowerCase()))
    .map((fruit) => ({ value: fruit, label: fruit }))
  Listbox.Root(
    frame,
    {
      id: "fruits",
      label: "Fruit",
      items,
      onValueChange: (value) => {
        state.picked = value
      },
    },
    (root) => Listbox.Content(root, results),
  )
}

const app = createTestApp({ columns: 30, rows: 5, view })
app.type("ban")
app.press("tab")
app.press("enter")
assert.equal(state.picked, "Banana")
console.log(state.picked)
```

Tab moved the focus from the input to the list. Enter then picked the highlighted item, and the `onValueChange` callback recorded it.

## Theme

Components, prompts, and Markdown take their styles from a `Theme`. Its roles are `primary`, `success`, `destructive`, `muted`, `highlight`, `selected`, `code`, `link`, `heading`, `border`, and `match`. Each role is a style, so it can be a color, bold, or both. `defaultTheme` uses the terminal's palette and follows its light or dark theme. Spread it to change a role:

```ts
import { defaultTheme, runApp, text } from "tui"

runApp({
  theme: { ...defaultTheme, primary: { fg: "#ff5f00", bold: true } },
  view: (frame) => {
    text(frame, 0, 0, "Hello", frame.theme.primary)
  },
})
```
