Overview
OpenTUI uses the Yoga layout engine, which implements a subset of CSS Flexbox. This provides:- Responsive layouts that adapt to terminal size
- Flexible sizing with grow/shrink behavior
- Alignment and justification controls
- Row and column layouts
- Nested layout containers
Yoga is implemented in native code (Zig) for high performance, but you interact with it through TypeScript.
Flexbox Basics
Flex Direction
Control whether children stack horizontally or vertically:Justify Content
Align children along the main axis (direction of flex flow):Align Items
Align children along the cross axis (perpendicular to flex flow):Align Self
OverridealignItems for a specific child:
Sizing
Fixed Sizes
Auto Sizing
Percentage Sizing
Min/Max Constraints
Flex Properties
Flex Grow
Control how much a child grows to fill available space:flexGrow share space proportionally:
Flex Shrink
Control how much a child shrinks when space is limited:By default, elements with explicit width/height have
flexShrink: 0, while auto-sized elements have flexShrink: 1.Flex Basis
Set the initial size before growing/shrinking:Flex Wrap
Control whether children wrap to new lines:Spacing
Margin
Add space outside an element:- Fixed numbers:
margin: 5 - Auto centering:
marginLeft: "auto" - Percentages:
margin: "10%"
Padding
Add space inside an element:- Fixed numbers:
padding: 5 - Percentages:
padding: "5%"
Common Layouts
Horizontal Split
Two columns with fixed sidebar:Vertical Split
Header, content, footer:Centered Content
Space Between
Evenly distribute children:Responsive Grid
Position Types
Relative Positioning
Default - element participates in flexbox layout:Absolute Positioning
Remove element from flex flow and position manually:- Doesn’t affect sibling layout
- Positioned relative to parent
- Requires explicit size and position
Overflow
Control how content beyond bounds is handled:overflow: "scroll" or overflow: "hidden", children are clipped to the parent’s bounds.
Dynamic Layout Updates
Layout is automatically recalculated when:- Children are added/removed
- Size properties change
- Flex properties change
- Terminal is resized
Layout Performance
Yoga layout calculation is fast, but deeply nested trees with many children can impact performance. Profile if you notice slowness.