Skip to content

Stateful Widgets ​

Stateless widgets only borrow configuration each frame. Stateful widgets store selection indices, scroll positions, expanded node sets, and editable buffers cleanly in caller-owned state structs.

Core Stateful Pairs ​

WidgetState TypeCleanup Needed?
Listtui.ListStateNo (value struct)
Tabletui.TableStateNo (value struct)
Scrollbartui.ScrollbarStateNo (value struct)
ScrollViewtui.ScrollViewStateNo (value struct)
RadioGrouptui.RadioStateNo (value struct)
Selecttui.SelectStateNo (value struct)
Slidertui.SliderStateNo (value struct)
Calendartui.CalendarStateNo (value struct)
ConfirmDialogtui.ConfirmStateNo (value struct)
TextInputtui.TextInputStateYes (defer state.deinit())
TextAreatui.TextAreaStateYes (defer state.deinit())
Treetui.TreeStateYes (defer state.deinit())
CommandPalettetui.CommandPaletteStateYes (defer state.deinit())
TerminalViewtui.VtScreenYes (defer screen.deinit())

Expand / Collapse Controls ​

  • TreeState: Call try state.expandAll(&roots) / state.collapseAll(), or toggle individual branches via Enter, Space, Left/Right, or mouse double-click (handleEvent).
  • Select / Dropdown: Expand/collapse via Enter, Space, Down (when closed), Esc (to collapse), or clicking the header/outside (SelectState.handleEventIn).
  • Accordion: Toggle .expanded per section on click, Enter, or Space (Accordion.handleEvent).

Scroll-Synchronized Rendering ​

When rendering a List, Table, or Tree, the widget inspects state.selected alongside the inner viewport height and updates state.offset automatically so the active item stays visible across resizes:

zig
try ui.list(.{
    .raw_items = &items,
    .block = .{ .title = "Packages", .focused = true },
}, &state.list, list_rect);

Focus Coordination (FocusGroup) ​

Coordinate multi-control screens cleanly with tui.FocusGroup:

zig
var focus = tui.FocusGroup{ .count = 3 };
if (focus.handleEvent(event)) return true; // Consumes Tab / BackTab

Released under the MIT License.