Skip to main content

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 was created by Facebook (now Meta) and is the same layout engine used in React Native.
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

Override alignItems 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:
Multiple children with 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:
Margins support:
  • Fixed numbers: margin: 5
  • Auto centering: marginLeft: "auto"
  • Percentages: margin: "10%"

Padding

Add space inside an element:
Padding supports:
  • 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:
Absolute positioning:
  • Doesn’t affect sibling layout
  • Positioned relative to parent
  • Requires explicit size and position

Overflow

Control how content beyond bounds is handled:
When 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
Access computed layout after recalculation:

Layout Performance

Minimize layout thrashing - Batch property changes when possible rather than updating one at a time.
Use absolute positioning sparingly - Absolute elements still participate in layout calculations but don’t affect siblings.
Yoga layout calculation is fast, but deeply nested trees with many children can impact performance. Profile if you notice slowness.

Layout Options Reference