Overview
TheBoxRenderable 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
The width of the box. Can be a fixed pixel value, percentage, or ‘auto’ to size based on content.
The height of the box. Can be a fixed pixel value, percentage, or ‘auto’ to size based on content.
Sets the main axis direction for child elements.
How much the box should grow relative to siblings when there’s extra space.
How much the box should shrink relative to siblings when space is constrained.
Alignment of children along the cross axis.
justifyContent
'flex-start' | 'flex-end' | 'center' | 'space-between' | 'space-around'
default:"'flex-start'"
Alignment of children along the main axis.
Spacing between all children (both row and column gaps).
Spacing between rows of children.
Spacing between columns of children.
Spacing Props
Padding on all sides.
Horizontal padding (left and right).
Vertical padding (top and bottom).
Padding on the top side.
Padding on the right side.
Padding on the bottom side.
Padding on the left side.
Margin on all sides.
Horizontal margin (left and right).
Vertical margin (top and bottom).
Visual Props
Background color as a hex string (e.g., ‘#1e293b’) or RGBA object.
Enable borders on all sides (true) or specify individual sides as an array:
['top', 'right', 'bottom', 'left'].borderStyle
'single' | 'double' | 'rounded' | 'bold' | 'singleDouble' | 'doubleSingle' | 'classic'
default:"'single'"
The style of the border characters.
Color of the border.
Border color when the box is focused (requires
focusable: true).Custom characters for border rendering. Object with keys:
topLeft, top, topRight, right, bottomRight, bottom, bottomLeft, left.Whether to fill the box with the background color.
Optional title text to display in the top border.
Alignment of the title text in the border.
Behavior Props
Whether the box can receive keyboard focus.
Whether the box is visible.
Stacking order of the box. Higher values appear on top.
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 (requiresfocusable: 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
shouldFillprop 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.