Skip to main content

Overview

The BoxRenderable is a fundamental building block for creating layouts in OpenTUI. It provides a container with customizable borders, backgrounds, and full flexbox layout support through Yoga layout engine.

Basic Usage

Props

Layout Props

number | 'auto' | `${number}%`
default:"'auto'"
The width of the box. Can be a fixed pixel value, percentage, or ‘auto’ to size based on content.
number | 'auto' | `${number}%`
default:"'auto'"
The height of the box. Can be a fixed pixel value, percentage, or ‘auto’ to size based on content.
'row' | 'column' | 'row-reverse' | 'column-reverse'
default:"'column'"
Sets the main axis direction for child elements.
number
default:"0"
How much the box should grow relative to siblings when there’s extra space.
number
default:"1"
How much the box should shrink relative to siblings when space is constrained.
'flex-start' | 'flex-end' | 'center' | 'stretch'
default:"'flex-start'"
Alignment of children along the cross axis.
'flex-start' | 'flex-end' | 'center' | 'space-between' | 'space-around'
default:"'flex-start'"
Alignment of children along the main axis.
number | `${number}%`
Spacing between all children (both row and column gaps).
number | `${number}%`
Spacing between rows of children.
number | `${number}%`
Spacing between columns of children.

Spacing Props

number | `${number}%`
Padding on all sides.
number | `${number}%`
Horizontal padding (left and right).
number | `${number}%`
Vertical padding (top and bottom).
number | `${number}%`
Padding on the top side.
number | `${number}%`
Padding on the right side.
number | `${number}%`
Padding on the bottom side.
number | `${number}%`
Padding on the left side.
number | `${number}%`
Margin on all sides.
number | `${number}%`
Horizontal margin (left and right).
number | `${number}%`
Vertical margin (top and bottom).

Visual Props

string | RGBA
default:"'transparent'"
Background color as a hex string (e.g., ‘#1e293b’) or RGBA object.
boolean | BorderSides[]
default:"false"
Enable borders on all sides (true) or specify individual sides as an array: ['top', 'right', 'bottom', 'left'].
'single' | 'double' | 'rounded' | 'bold' | 'singleDouble' | 'doubleSingle' | 'classic'
default:"'single'"
The style of the border characters.
string | RGBA
default:"'#FFFFFF'"
Color of the border.
string | RGBA
default:"'#00AAFF'"
Border color when the box is focused (requires focusable: true).
BorderCharacters
Custom characters for border rendering. Object with keys: topLeft, top, topRight, right, bottomRight, bottom, bottomLeft, left.
boolean
default:"true"
Whether to fill the box with the background color.
string
Optional title text to display in the top border.
'left' | 'center' | 'right'
default:"'left'"
Alignment of the title text in the border.

Behavior Props

boolean
default:"false"
Whether the box can receive keyboard focus.
boolean
default:"true"
Whether the box is visible.
number
default:"0"
Stacking order of the box. Higher values appear on top.
'relative' | 'absolute'
default:"'relative'"
Positioning mode. Use ‘absolute’ with left, right, top, bottom props.

Examples

Three-Column Layout

Box with Title and Custom Border

Absolute Positioned Box

Centered Content

Focusable Box with Highlight

Gap and Spacing

Methods

add(child: Renderable, index?: number): number

Adds a child renderable to the box.

remove(id: string): void

Removes a child by its ID.

focus(): void

Sets focus to the box (requires focusable: true).

requestRender(): void

Manually requests a re-render of the box.

Notes

  • Box uses the Yoga layout engine, which provides flexbox-like layout capabilities
  • Borders take up 1 cell on each enabled side, affecting the inner content area
  • When using percentage-based sizing, the value is relative to the parent container
  • The shouldFill prop controls whether the background color fills the entire box area
  • Absolute positioning takes the box out of the normal layout flow
For scrollable content areas, use ScrollBox which wraps Box with scrolling functionality.