Skip to content

Using Layout

Use Layout to arrange Boxes within an available width or height. Each example below is standalone and matches its terminal preview. Start with Using Box for text, borders, and alignment.

GoalUse
Divide one row or column among childrenFlex.row or Flex.col
Pass dimensions after padding to a builderContainer.make
Arrange repeated items in equal-width columnsGrid.make or Grid.auto
Position and clip content inside an exact areaPlace.make

For overlapping layers, use frame rendering.

Flex

Flex subtracts fixed children and gaps before distributing remaining space. Flex.row allocates columns, and Flex.col allocates rows.

  • Flex.fixed(box) keeps the Box's dimensions.
  • Flex.grow(box, factor) aligns or crops the Box to its allocated size. The default factor is 1.
  • Flex.fill(builder, factor) passes an allocated size to your builder. Your builder must return a Box of that size. Flex does not resize its result.
  • Flex.spacer(factor) consumes allocated space without visible content.

If fixed children and gaps exceed the requested size, Flex keeps them. Use Place when the final dimensions must be exact.

Push items apart

The row below is 63 columns wide. Its outer border adds two columns, so the complete navbar is 65 columns wide.

╭───────────────────────────────────────────────────────────────╮
│ ◆ App Logo           Home  Reports  Settings          ▸ Admin │
╰───────────────────────────────────────────────────────────────╯
import * as Box from "effect-boxes/Box";
import { Flex } from "effect-boxes/Layout";
 
const navbar = Flex.row(
  [
    Flex.fixed(Box.text("◆ App Logo").pipe(Box.pad(0, 1))),
    Flex.spacer(),
    Flex.fixed(Box.text("Home  Reports  Settings").pipe(Box.pad(0, 1))),
    Flex.spacer(),
    Flex.fixed(Box.text("▸ Admin").pipe(Box.pad(0, 1))),
  ],
  63
).pipe(Box.border("rounded"));
const output = Box.renderPrettySync(navbar);
console.log(output);

Split width by factor

After reserving the one-column gap, the row allocates 21 and 41 columns to the two panels. Each builder reserves two columns for its border before sizing the content. The outer border brings the final width to 65 columns.

┌───────────────────────────────────────────────────────────────┐
│╭───────────────────╮ ╭───────────────────────────────────────╮│
││ 1/3 width         │ │ 2/3 width                             ││
│╰───────────────────╯ ╰───────────────────────────────────────╯│
└───────────────────────────────────────────────────────────────┘
import * as Box from "effect-boxes/Box";
import { Flex, Place } from "effect-boxes/Layout";
 
const split = Flex.row(
  [
    Flex.fill(
      (width) =>
        Box.text("1/3 width").pipe(
          Box.pad(0, 1),
          Place.horizontal(width - 2, "left"),
          Box.border("rounded")
        ),
      1
    ),
    Flex.fill(
      (width) =>
        Box.text("2/3 width").pipe(
          Box.pad(0, 1),
          Place.horizontal(width - 2, "left"),
          Box.border("rounded")
        ),
      2
    ),
  ],
  63,
  { gap: 1 }
).pipe(Box.border());
const output = Box.renderPrettySync(split);
console.log(output);

Build content from allocated width

The progress builder returns exactly the allocated number of columns. It also handles a zero-width allocation without adding a marker.

╭───────────────────────────────────────────────────────────────╮
│ Progress ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸────────────── 70% │
╰───────────────────────────────────────────────────────────────╯
import * as Box from "effect-boxes/Box";
import { Flex } from "effect-boxes/Layout";
 
const progress = Flex.row(
  [
    Flex.fixed(Box.text(" Progress ")),
    Flex.fill((width) => {
      const complete = Math.floor(width * 0.7);
      const marker = complete < width ? "╸" : "";
      return Box.text(
        "━".repeat(complete) +
          marker +
          "─".repeat(width - complete - marker.length)
      );
    }),
    Flex.fixed(Box.text(" 70% ")),
  ],
  63
).pipe(Box.border("rounded"));
const output = Box.renderPrettySync(progress);
console.log(output);

Divide vertical space

The header and footer each use one row. The body receives the other eight rows. The border adds two rows and two columns to the 63-column, 10-row page.

╭───────────────────────────────────────────────────────────────╮
│                           ┄ Header ┄                          │
│                                                               │
│  Body content goes here...                                    │
│                                                               │
│                                                               │
│                                                               │
│                                                               │
│                                                               │
│                                                               │
│                           ┄ Footer ┄                          │
╰───────────────────────────────────────────────────────────────╯
import * as Box from "effect-boxes/Box";
import { Flex } from "effect-boxes/Layout";
 
const page = Flex.col(
  [
    Flex.fixed(Box.text("┄ Header ┄").pipe(Box.alignHoriz(Box.center1, 63))),
    Flex.grow(Box.text("Body content goes here...").pipe(Box.pad(1, 2))),
    Flex.fixed(Box.text("┄ Footer ┄").pipe(Box.alignHoriz(Box.center1, 63))),
  ],
  10
).pipe(Box.border("rounded"));
const output = Box.renderPrettySync(page);
console.log(output);

Choose gaps and alignment

Both Flex functions accept gap and align. A row's gap counts columns, and its alignment controls vertical placement. A column's gap counts rows, and its alignment controls horizontal placement.

Use Box.top, Box.center1, or Box.bottom for a row. Use Box.left, Box.center1, or Box.right for a column.

Container

Container.make calculates dimensions after padding and passes them to your builder. Padding can be a number for both axes or [vertical, horizontal]. The result enforces width, but it does not enforce height.

When you omit height, both ctx.height and ctx.innerHeight are 0. The content still determines the output height. Supply a height before using ctx.innerHeight to allocate vertical space.

┌───────────────────────────────────────────────────────────────┐
│                                                               │
│                                                               │
│  outer width:  63                                             │
│  inner width:  59                                             │
│  inner height: 0                                              │
│  padding:      2 cells each side                              │
│                                                               │
│                                                               │
└───────────────────────────────────────────────────────────────┘
import * as Box from "effect-boxes/Box";
import { Container } from "effect-boxes/Layout";
 
const panel = Container.make({ width: 63, padding: 2 }, (ctx) =>
  Box.vcat(
    [
      Box.text(`outer width:  ${ctx.width}`),
      Box.text(`inner width:  ${ctx.innerWidth}`),
      Box.text(`inner height: ${ctx.innerHeight}`),
      Box.text("padding:      2 cells each side"),
    ],
    Box.left
  )
).pipe(Box.border());
const output = Box.renderPrettySync(panel);
console.log(output);

Fit Flex inside a container

Pass ctx.innerWidth to each row so padding remains outside its allocation. This status panel is 65 columns wide including its border.

╭───────────────────────────────────────────────────────────────╮
│                                                               │
│ ◆ Status                                             ● Online │
│ ───────────────────────────────────────────────────────────── │
│ Users: 1,247 Uptime: 99.9%                             v2.4.1 │
│                                                               │
╰───────────────────────────────────────────────────────────────╯
import * as Box from "effect-boxes/Box";
import { Flex, Container } from "effect-boxes/Layout";
 
const statusPanel = Container.make({ width: 63, padding: 1 }, (ctx) =>
  Box.vcat(
    [
      Flex.row(
        [
          Flex.fixed(Box.text("◆ Status")),
          Flex.spacer(),
          Flex.fixed(Box.text("● Online")),
        ],
        ctx.innerWidth
      ),
      Box.text("─".repeat(ctx.innerWidth)),
      Flex.row(
        [
          Flex.fixed(Box.text("Users: 1,247")),
          Flex.fixed(Box.text("Uptime: 99.9%")),
          Flex.spacer(),
          Flex.fixed(Box.text("v2.4.1")),
        ],
        ctx.innerWidth,
        { gap: 1 }
      ),
    ],
    Box.left
  )
).pipe(Box.border("rounded"));
const output = Box.renderPrettySync(statusPanel);
console.log(output);

Grid

Grid.make aligns each item inside a column of exactly colWidth columns. Wider items are cropped. A final incomplete row gets empty cells. Three 21-column cells and two one-column gaps occupy 65 columns before the outer border.

┌─────────────────────────────────────────────────────────────────┐
│╭───────────────────╮ ╭───────────────────╮ ╭───────────────────╮│
││ ■ Alpha           │ │ ■ Beta            │ │ ■ Gamma           ││
││   Item card       │ │   Item card       │ │   Item card       ││
│╰───────────────────╯ ╰───────────────────╯ ╰───────────────────╯│
│                                                                 │
│╭───────────────────╮ ╭───────────────────╮ ╭───────────────────╮│
││ ■ Delta           │ │ ■ Epsilon         │ │ ■ Zeta            ││
││   Item card       │ │   Item card       │ │   Item card       ││
│╰───────────────────╯ ╰───────────────────╯ ╰───────────────────╯│
└─────────────────────────────────────────────────────────────────┘
import * as Box from "effect-boxes/Box";
import { Grid } from "effect-boxes/Layout";
 
const cards = ["Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta"].map(
  (name) =>
    Box.vcat([Box.text(`■ ${name}`), Box.text("  Item card")], Box.left).pipe(
      Box.pad(0, 1),
      Box.minWidth(19),
      Box.border("rounded")
    )
);
const grid = Grid.make(cards, { cols: 3, colWidth: 21, gap: [1, 1] }).pipe(
  Box.border()
);
const output = Box.renderPrettySync(grid);
console.log(output);

Adapt the number of columns

Grid.auto calculates column count from the available width, minColWidth, and the horizontal gap. At 50 columns, these cards use two 24-column cells and one gap, leaving one unused column. The outer border adds two columns.

┌─────────────────────────────────────────────────┐
│╭───────────────────╮    ╭───────────────────╮   │
││ ■ Alpha           │    │ ■ Beta            │   │
││   Item card       │    │   Item card       │   │
│╰───────────────────╯    ╰───────────────────╯   │
│╭───────────────────╮    ╭───────────────────╮   │
││ ■ Gamma           │    │ ■ Delta           │   │
││   Item card       │    │   Item card       │   │
│╰───────────────────╯    ╰───────────────────╯   │
│╭───────────────────╮    ╭───────────────────╮   │
││ ■ Epsilon         │    │ ■ Zeta            │   │
││   Item card       │    │   Item card       │   │
│╰───────────────────╯    ╰───────────────────╯   │
└─────────────────────────────────────────────────┘
import * as Box from "effect-boxes/Box";
import { Grid } from "effect-boxes/Layout";
 
const cards = ["Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta"].map(
  (name) =>
    Box.vcat([Box.text(`■ ${name}`), Box.text("  Item card")], Box.left).pipe(
      Box.pad(0, 1),
      Box.minWidth(19),
      Box.border("rounded")
    )
);
const grid = Grid.auto(cards, 50, { minColWidth: 20 }).pipe(Box.border());
const output = Box.renderPrettySync(grid);
console.log(output);

Grid options

OptionMeaning
colsColumn count for Grid.make
colWidthExact cell width for Grid.make
minColWidthMinimum width used to calculate Grid.auto column count
maxColWidthOptional cap on each automatic column's width
gap[horizontal, vertical] for Grid.make; horizontal spacing for Grid.auto
alignHorizontal alignment inside cells, default Box.left
stretchPads items before alignment; cell widths remain exact with either setting

Place content inside a fixed area

Place.make produces exactly the requested width and height. Content outside that area is clipped, and empty cells contain spaces. The border below is outside the 20-column, five-row area.

╭────────────────────╮
│                    │
│                    │
│       Ready        │
│                    │
│                    │
╰────────────────────╯
import * as Box from "effect-boxes/Box";
import { Place } from "effect-boxes/Layout";
 
const panel = Box.text("Ready").pipe(
  Place.make({ width: 20, height: 5, x: "center", y: "center" }),
  Box.border("rounded")
);
const output = Box.renderPrettySync(panel);
console.log(output);

x counts columns from the left edge, and y counts rows from the top edge. Both default to 0. Negative offsets clip the start of the content. For horizontal anchors, use "left", "center", or "right". For vertical anchors, use "top", "center", or "bottom". Centering uses Math.floor((areaSize - contentSize) / 2) on each axis, including when content is larger than the area.

Clip one dimension at a time

Place.horizontal changes only width, and Place.vertical changes only height. Their combined result below contains HIJK and NOPQ.

┌────┐
│HIJK│
│NOPQ│
└────┘
import * as Box from "effect-boxes/Box";
import { Place } from "effect-boxes/Layout";
 
const clipped = Box.text("ABCDEF\nGHIJKL\nMNOPQR").pipe(
  Place.horizontal(4, -1),
  Place.vertical(2, "bottom"),
  Box.border()
);
const output = Box.renderPrettySync(clipped);
console.log(output);

Place retains nested annotations on visible source content, including styles and reactive regions. Added padding has no source annotations. Apply an annotation to the resulting Box if it should cover the whole area.

Place uses the existing logical Reactive tracking API. It does not correct pre-existing differences between rendered and tracked coordinates for centered SubBoxes; see the tracking alignment issue.

Dimensions must be nonnegative safe integers. Numeric offsets must be safe integers. Place throws RangeError for invalid numbers or any Cmd annotation inside the source Box, even if the command would be clipped. Compose terminal commands separately from placed content.

Compose a dashboard

The container reserves two columns for padding. The three card widths account for their borders before Grid arranges them. The dashboard is 65 columns wide including its outer border.

╭───────────────────────────────────────────────────────────────╮
│                                                               │
│ ◆ Dashboard                                        Settings ▸ │
│ ───────────────────────────────────────────────────────────── │
│ ╭─────────────────╮ ╭─────────────────╮ ╭─────────────────╮   │
│ │ Users           │ │ Revenue         │ │ Errors          │   │
│ │ 1,247           │ │ $48,290         │ │ 23              │   │
│ ╰─────────────────╯ ╰─────────────────╯ ╰─────────────────╯   │
│                                                               │
╰───────────────────────────────────────────────────────────────╯
import * as Box from "effect-boxes/Box";
import { Flex, Container, Grid, Place } from "effect-boxes/Layout";
 
const dashboard = Container.make({ width: 63, padding: 1 }, (ctx) => {
  const cols = 3;
  const gap = 1;
  const colWidth = Math.floor((ctx.innerWidth - (cols - 1) * gap) / cols);
  const kpis = Grid.make(
    [
      { label: "Users", value: "1,247" },
      { label: "Revenue", value: "$48,290" },
      { label: "Errors", value: "23" },
    ].map(({ label, value }) =>
      Box.vcat([Box.text(label), Box.text(value)], Box.left).pipe(
        Box.pad(0, 1),
        Place.horizontal(colWidth - 2, "left"),
        Box.border("rounded")
      )
    ),
    { cols, colWidth, gap: [gap, 0] }
  );
  const header = Flex.row(
    [
      Flex.fixed(Box.text("◆ Dashboard")),
      Flex.spacer(),
      Flex.fixed(Box.text("Settings ▸")),
    ],
    ctx.innerWidth
  );
  return Box.vcat(
    [header, Box.text("─".repeat(ctx.innerWidth)), kpis],
    Box.left
  );
}).pipe(Box.border("rounded"));
const output = Box.renderPrettySync(dashboard);
console.log(output);