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

Every component takes the root props `id` (tells two instances apart and keeps their state), `label` (what it is called for agents), and `agent` (`"secret"` shows its values as `"[redacted]"` to agents; `"hidden"` leaves it out).

A part is drawn as `Part(parent, rect, body?, item?)`. A part's body draws inside it instead of its own content.

## Listbox

Picks one item from a list.

| Prop                                | Description                                                                                       |
| ----------------------------------- | ------------------------------------------------------------------------------------------------- |
| `items`                             | `{ value, label }[]`, drawn by `Content` when it is given no body.                                |
| `value`, `defaultValue`             | The chosen item's value.                                                                          |
| `onValueChange(value, details)`     | Reasons: `item-press` or `imperative`.                                                            |
| `highlighted`, `defaultHighlighted` | The item the keys are on.                                                                         |
| `onHighlight(value, details)`       | Reasons: `list-navigation`, `item-press`, or `imperative`.                                        |
| `loop`                              | Whether moving past either end goes around. True unless set.                                      |
| `activation`                        | `"automatic"` picks as the keys move; `"manual"` waits for Enter or Space. `"manual"` unless set. |

Parts: `Root`, `Content`, `Item`. `Content` takes the focus; Up and Down, Home and End, the page keys, Ctrl+P and Ctrl+N, and typeahead move the highlight; Enter or Space picks.

## CheckboxGroup

Checks any number of items.

| Prop                                | Description                                                                                                |
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `items`                             | `{ value, label }[]`.                                                                                      |
| `value`, `defaultValue`             | The checked values, in the items' order.                                                                   |
| `onValueChange(value, details)`     | Reasons: `item-press` or `imperative`.                                                                     |
| `highlighted`, `defaultHighlighted` | The item the keys are on.                                                                                  |
| `onHighlight(value, details)`       | Reasons: `list-navigation` or `imperative`.                                                                |
| `loop`                              | Whether moving past either end goes around. True unless set.                                               |
| `onSubmit(values, key)`             | Enter, typed on its own; `key` is null when the program or an agent submits. Without it, Enter checks too. |

Parts: `Root`, `Content`. Space checks or unchecks the highlighted item.

## Tabs

A row of tabs, one open at a time.

| Prop                            | Description                                                          |
| ------------------------------- | -------------------------------------------------------------------- |
| `items`                         | `{ value, label }[]`, drawn by `List` in a row.                      |
| `value`, `defaultValue`         | The open tab. The first tab's when it is empty.                      |
| `onValueChange(value, details)` | Reasons: `list-navigation`, `item-press` (a click), or `imperative`. |
| `loop`                          | Whether Left and Right wrap. True unless set.                        |

Parts: `Root`, `List`. Left and Right open the tab beside it, Home and End the first and last. The view draws what `root.api.value()` says is open.

## Table

Rows and columns, with the highlighted row picked.

| Prop                                | Description                                                            |
| ----------------------------------- | ---------------------------------------------------------------------- |
| `columns`                           | `{ label, width?, align? }[]`, where `align` is `"left"` or `"right"`. |
| `rows`                              | `{ value, cells }[]`, a cell for each column.                          |
| `value`, `defaultValue`             | The chosen row's value.                                                |
| `onValueChange(value, details)`     | Reasons: `item-press` or `imperative`.                                 |
| `highlighted`, `defaultHighlighted` | The row the keys are on.                                               |
| `onHighlight(value, details)`       | Reasons: `list-navigation` or `imperative`.                            |
| `loop`                              | Whether moving past either end goes around. True unless set.           |

Parts: `Root`, `Content`. `Content` draws the header row, then the rows and columns.

## Input

One line of text, edited the way a shell edits.

| Prop                            | Description                                                                  |
| ------------------------------- | ---------------------------------------------------------------------------- |
| `value`, `defaultValue`         | The text.                                                                    |
| `onValueChange(value, details)` | Reasons: `input-change`, `clear-press`, or `imperative`.                     |
| `placeholder`                   | Drawn dimmed while the input is empty.                                       |
| `onSubmit(value, key)`          | Enter, typed on its own; `key` is null when the program or an agent submits. |

Parts: `Root`, `Field`. `Field` takes the focus and sets `frame.cursor` while focused. `root.api.setValue(value)` replaces the text and `root.api.clear()` empties it.

## Textarea

Text with a cursor, wrapped to its width.

| Prop                            | Description                                                                                                       |
| ------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `value`, `defaultValue`         | The text, with lines separated by `\n`.                                                                           |
| `onValueChange(value, details)` | Reasons: `input-change`, `clear-press`, or `imperative`.                                                          |
| `placeholder`                   | Drawn dimmed while the textarea is empty.                                                                         |
| `onSubmit(value, key)`          | Enter, typed on its own; `key` is null when the program or an agent submits. Without it, Enter starts a new line. |
| `submitShortcut`                | `"enter"`, with Shift+Enter for a new line, or `"mod+enter"`, with Enter for a new line. `"enter"` unless set.    |

Parts: `Root`, `Field`. A paste keeps its lines, and `root.api.rows(columns)` says how many rows the text needs. `root.api.clear()` empties the text.

## Dialog

A box over everything else.

| Prop                          | Description                                |
| ----------------------------- | ------------------------------------------ |
| `open`, `defaultOpen`         | Whether it is open.                        |
| `onOpenChange(open, details)` | Reasons: `escape-key` or `imperative`.     |
| `title`                       | Drawn in its border.                       |
| `dismissible`                 | Whether Escape closes it. True unless set. |

Parts: `Root`, `Content`. While it is open, Tab moves only between its own parts, keys reach nothing behind it, and Escape closes it. `root.api.open()` and `root.api.close()` change it imperatively.

## ScrollArea

Content taller than its view.

| Prop                              | Description                                                                      |
| --------------------------------- | -------------------------------------------------------------------------------- |
| `lines`                           | Lines drawn by `Viewport` when it is given no body.                              |
| `total`                           | How many rows the content has when a body draws it. The lines' count by default. |
| `offset`, `defaultOffset`         | The first row in view.                                                           |
| `onOffsetChange(offset, details)` | Reasons: `keyboard` or `imperative`.                                             |
| `stickToEnd`                      | Keeps the end in view as more arrives, while it is scrolled to the end.          |

Parts: `Root`, `Viewport`. `Viewport` takes the focus and the arrows scroll by a row, the page keys and Space by a page, Home and End to either end. A bar on the right shows where the view is. `root.api.scrollToEnd()`, `root.api.scrollToStart()`, `root.api.isAtEnd()`, and `root.api.isAtStart()` read and move it.

## ChangeDetails

Every change callback receives `details` after the new value:

| Field      | Description                                                                       |
| ---------- | --------------------------------------------------------------------------------- |
| `reason`   | Why it changed, such as `"list-navigation"`, `"input-change"`, or `"escape-key"`. |
| `source`   | `"keyboard"`, `"mouse"`, `"paste"`, `"programmatic"`, or `"agent"`.               |
| `key`      | The key behind it, or null.                                                       |
| `cancel()` | Stops the change.                                                                 |
| `canceled` | Whether it was canceled.                                                          |

## definePrimitive

`definePrimitive(definition)` builds a component on the same state, focus, and change contract as the built-in components. The definition has `name`, `state`, `api`, optional `keys` and `agent`, and `slots`. Each slot is made with `slot(name, options)`, where the options are `focusable`, `keys`, `style`, `present`, `press`, `mouse`, `trap`, `decorate`, and `draw`.

```ts
import assert from "node:assert/strict"
import {
  type ChangeDetails,
  controllable,
  createTestApp,
  definePrimitive,
  type PrimitiveContext,
  type RootProps,
  text,
} from "tui"

interface CounterProps extends RootProps {
  value?: number
  defaultValue?: number
  onValueChange?: (value: number, details: ChangeDetails) => void
}

const Counter = definePrimitive({
  name: "counter",
  state: (context: PrimitiveContext<CounterProps>) => ({
    value: controllable(
      context,
      "value",
      (props) => props.value,
      context.props().defaultValue ?? 0,
      (props, value, details) => props.onValueChange?.(value, details),
    ),
  }),
  api: () => ({}),
  slots: (slot, Root) => {
    const View = slot("view", {
      focusable: true,
      keys: (key, view) => {
        if (key.name !== "char" || key.text !== " ") {
          return false
        }
        view.state.value.set(view.state.value.get() + 1, "item-press", key)
        return true
      },
      draw: (view) => {
        text(view.frame, view.rect.x, view.rect.y, String(view.state.value.get()))
      },
    })
    return { Root, View }
  },
})

const app = createTestApp({
  columns: 10,
  rows: 1,
  view: (frame) => {
    Counter.Root(frame, { id: "count", defaultValue: 0 }, (root) => Counter.View(root, frame.rect))
  },
})

app.press(" ")
assert.equal(app.screen()[0], "1")
console.log(app.screen().join("\n"))
```

`controllable(context, name, read, initial, notify)` implements the `value`, `defaultValue`, and `onValueChange` contract for one value.

## Behaviors

These are the behaviors the built-in components use; they are useful in a component of your own.

- `createListNavigation({ loop?, page? })` returns `{ move(values, current, key), moveIndex(count, at, key) }`. It handles Up, Down, Home, End, PageUp, PageDown, Ctrl+P, and Ctrl+N.
- `scrollByKey(offset, key, rows, total)` returns the next offset, or null when the key does not scroll.
- `scrollIntoView(offset, index, rows, total)` returns the offset that shows `index`, moving as little as it can.
- `createTextEditing()` returns `{ edit(state, key) }`, where `state` is `{ value, cursor }`. It returns the next state, or null when the key is not an edit. It handles arrows, Home and End, word moves, Ctrl+A, Ctrl+E, Ctrl+U, Ctrl+K, Ctrl+W, Alt+Backspace, Alt+D, and Ctrl+D.
- `createTypeahead({ timeout?, now? })` returns `{ find(values, labelOf, current, key), reset() }`. Keys typed within `timeout` milliseconds add up; typing one letter again cycles through the items it starts.
