Skip to content

Layout

Layout — higher-level layout combinators built on top of Box.

Provides container-aware composition primitives inspired by CSS Flexbox:

  • Flex.row / Flex.col distribute space among children proportionally
  • Container.make passes available dimensions to child builders
  • Grid.make / Grid.auto arrange items in fixed-column grids
  • Place.make places and clips content within an exact bounded region

Flex, Container, Grid, and Place return standard Box values. Layer and Stack describe overlapping Boxes for Renderer.frame at the frame boundary.


combinators

Container

Provides container dimensions to a child builder function. Automatically computes inner dimensions after padding, enforces width on the output, and applies padding.

Signature

declare const Container: {
  readonly make: <A>(
    options: Container.Options,
    builder: (ctx: Container.Context) => Box.Box<A>,
  ) => Box.Box<A>;
};

Source

Flex

Distributes space among children using a flexbox-style algorithm.

Fixed children keep their intrinsic size. Grow and Fill children share remaining space proportionally based on their factor. Remainder columns are distributed one each to the first N grow children to avoid rounding gaps.

If fixed children exceed the container size, grow children receive 0 space and the result may be wider/taller than the container. Compose with Box.maxWidth to enforce hard limits.

Example

import { pipe } from "effect";
import * as Box from "effect-boxes/Box";
import { Flex } from "effect-boxes/Layout";
 
// Data-first
const row = Flex.row(
  [Flex.fixed(Box.text("Name:")), Flex.spacer(), Flex.fixed(Box.text("[ok]"))],
  80,
);
 
// Data-last (pipe)
const row2 = pipe(
  [
    Flex.fixed(Box.text("Name:")),
    Flex.grow(Box.text("value"), 2),
    Flex.fixed(Box.text("[ok]")),
  ],
  Flex.row(80, { gap: 1 }),
);

Signature

declare const Flex: {
  readonly fixed: <A>(box: Box.Box<A>) => Flex.Child<A>;
  readonly grow: <A>(box: Box.Box<A>, factor?: number) => Flex.Child<A>;
  readonly fill: <A>(
    builder: (size: number) => Box.Box<A>,
    factor?: number,
  ) => Flex.Child<A>;
  readonly spacer: (factor?: number) => Flex.Child<never>;
  readonly row: ((
    containerWidth: number,
    options?: Flex.Options,
  ) => <A>(self: ReadonlyArray<Flex.Child<A>>) => Box.Box<A>) &
    (<A>(
      self: ReadonlyArray<Flex.Child<A>>,
      containerWidth: number,
      options?: Flex.Options,
    ) => Box.Box<A>);
  readonly col: ((
    containerHeight: number,
    options?: Flex.Options,
  ) => <A>(self: ReadonlyArray<Flex.Child<A>>) => Box.Box<A>) &
    (<A>(
      self: ReadonlyArray<Flex.Child<A>>,
      containerHeight: number,
      options?: Flex.Options,
    ) => Box.Box<A>);
};

Source

Grid

Arranges items in rows and columns with uniform column width.

Signature

declare const Grid: {
  readonly make: ((
    options: Grid.Options,
  ) => <A>(self: ReadonlyArray<Box.Box<A>>) => Box.Box<A>) &
    (<A>(self: ReadonlyArray<Box.Box<A>>, options: Grid.Options) => Box.Box<A>);
  readonly auto: ((
    containerWidth: number,
    options: Grid.AutoOptions,
  ) => <A>(self: ReadonlyArray<Box.Box<A>>) => Box.Box<A>) &
    (<A>(
      self: ReadonlyArray<Box.Box<A>>,
      containerWidth: number,
      options: Grid.AutoOptions,
    ) => Box.Box<A>);
};

Source

Stack

Combines layers for resolution and encoding through Renderer.frame. Higher z wins; later input wins ties. Spaces and padding are opaque. Partial wide glyphs become blanks when clipped or covered.

Example

import * as Box from "effect-boxes/Box";
import { Layer, Stack } from "effect-boxes/Layout";
const stack = Stack.make(
  [
    Layer.make(Box.text("Background")),
    Layer.make(Box.text("Popup"), { x: 2, z: 1 }),
  ],
  {
    width: 20,
    height: 4,
  },
);

Signature

declare const Stack: {
  readonly make: <const T extends ReadonlyArray<Layer<unknown>>>(
    layers: T,
    options: Stack.Options,
  ) => Stack<
    [T[number]] extends [never]
      ? never
      : T[number] extends Layer<infer A>
        ? A
        : never
  >;
  readonly makeResult: <const T extends ReadonlyArray<Layer<unknown>>>(
    layers: T,
    options: Stack.Options,
  ) => Result.Result<
    Stack<
      [T[number]] extends [never]
        ? never
        : T[number] extends Layer<infer A>
          ? A
          : never
    >,
    LayoutValidationError
  >;
};

Source

constructors

Layer

Creates positioned layers for frame-boundary composition.

Example

import * as Box from "effect-boxes/Box";
import { Layer } from "effect-boxes/Layout";
const popup = Layer.make(Box.text("Popup"), { x: 2, y: 1, z: 1 });
const piped = Box.text("Popup").pipe(Layer.make({ x: 2, y: 1 }));

Signature

declare const Layer: {
  readonly make: {
    (options?: Layer.Options): <A>(self: Box.Box<A>) => Layer<A>;
    <A>(self: Box.Box<A>, options?: Layer.Options): Layer<A>;
  };
  readonly makeResult: {
    (
      options?: Layer.Options,
    ): <A>(self: Box.Box<A>) => Result.Result<Layer<A>, LayoutValidationError>;
    <A>(
      self: Box.Box<A>,
      options?: Layer.Options,
    ): Result.Result<Layer<A>, LayoutValidationError>;
  };
};

Source

errors

LayoutValidationError

Reports invalid layout input or unsupported command annotations.

Example

import { Result } from "effect";
import * as Box from "effect-boxes/Box";
import { Place } from "effect-boxes/Layout";
const result = Place.makeResult(Box.text("A"), { width: -1, height: 1 });
if (Result.isFailure(result)) console.log(result.failure.reason); // "InvalidInput"

Signature

declare const LayoutValidationError: typeof internalValidation.LayoutValidationError;

Source

LayoutValidationError (type alias)

Signature

type LayoutValidationError = internalValidation.LayoutValidationError;

Source

models

Container (namespace)

Source

Context (interface)

Context passed to a container builder function with computed inner dimensions after padding.

Signature

export interface Context {
  readonly width: number;
  readonly height: number;
  readonly innerWidth: number;
  readonly innerHeight: number;
}

Source

Options (interface)

Options for creating a container.

Signature

export interface Options {
  readonly width: number;
  readonly height?: number;
  readonly padding?: number | readonly [number, number];
}

Source

Flex (namespace)

Source

Fixed (interface)

A flex child that occupies its intrinsic width/height.

Signature

export interface Fixed<A = never> {
  readonly _tag: "Fixed";
  readonly box: Box.Box<A>;
}

Source

Grow (interface)

A flex child that grows to fill remaining space proportionally.

Signature

export interface Grow<A = never> {
  readonly _tag: "Grow";
  readonly box: Box.Box<A>;
  readonly factor: number;
}

Source

Fill (interface)

A flex child that fills remaining space via a builder function.

Signature

export interface Fill<A = never> {
  readonly _tag: "Fill";
  readonly builder: (size: number) => Box.Box<A>;
  readonly factor: number;
  readonly isSpacer?: true;
}

Source

Options (interface)

Options for flex row/col layout.

Signature

export interface Options {
  readonly align?: Box.Alignment;
  readonly gap?: number;
}

Source

Child (type alias)

A child element in a flex layout.

Signature

type Child<A = never> = Fixed<A> | Grow<A> | Fill<A>;

Source

Grid (namespace)

Source

Options (interface)

Options for fixed-column grid layout.

Signature

export interface Options {
  readonly cols: number;
  readonly colWidth: number;
  readonly gap?: readonly [number, number];
  readonly align?: Box.Alignment;
  readonly stretch?: boolean;
}

Source

AutoOptions (interface)

Options for auto-column grid layout.

Signature

export interface AutoOptions {
  readonly minColWidth: number;
  readonly maxColWidth?: number;
  readonly gap?: number;
  readonly align?: Box.Alignment;
  readonly stretch?: boolean;
}

Source

Layer (interface)

Describes one opaque Box positioned within a Stack viewport.

Signature

export interface Layer<out A = never> {
  readonly box: Box.Box<A>;
  readonly x: number;
  readonly y: number;
  readonly z: number;
}

Source

Layer (namespace)

Source

Options (interface)

Describes signed safe integer coordinates and ordering.

Signature

export interface Options {
  readonly x?: number;
  readonly y?: number;
  readonly z?: number;
}

Source

Place (namespace)

Source

Options (interface)

Bounded placement options in terminal cells.

Signature

export interface Options {
  readonly width: number;
  readonly height: number;
  readonly x?: Horizontal;
  readonly y?: Vertical;
}

Source

Horizontal (type alias)

Horizontal coordinate or anchor.

Signature

type Horizontal = number | "left" | "center" | "right";

Source

Vertical (type alias)

Vertical coordinate or anchor.

Signature

type Vertical = number | "top" | "center" | "bottom";

Source

Stack (interface)

Describes flat opaque layers within an exact viewport. A Stack is not a Box.

Signature

export interface Stack<out A = never> {
  readonly _tag: "Stack";
  readonly width: number;
  readonly height: number;
  readonly layers: ReadonlyArray<Layer<A>>;
}

Source

Stack (namespace)

Source

Options (interface)

Describes exact viewport dimensions in terminal cells.

Signature

export interface Options {
  readonly width: number;
  readonly height: number;
}

Source

transformations

Place

Places content within an exact bounded region using terminal cell coordinates.

The make helper sets both dimensions; horizontal and vertical preserve the other source dimension. Numeric offsets default to zero. Center rounding uses floor((outer - inner) / 2), including oversized content. Visible annotations survive clipping, and padding outside the source is unannotated.

Dimensions must be non-negative safe integers and numeric offsets must be safe integers. Invalid values and Cmd annotations anywhere in the source throw RangeError, including when the source is wholly outside the viewport.

Example

import * as Box from "effect-boxes/Box";
import { Place } from "effect-boxes/Layout";
 
const placed = Place.make(Box.text("Hello"), {
  width: 20,
  height: 4,
  x: "center",
  y: "bottom",
});
console.log(placed.cols, placed.rows); // 20, 4

Signature

declare const Place: {
  readonly make: ((
    options: Place.Options,
  ) => <A>(self: Box.Box<A>) => Box.Box<A>) &
    (<A>(self: Box.Box<A>, options: Place.Options) => Box.Box<A>);
  readonly makeResult: ((
    options: Place.Options,
  ) => <A>(
    self: Box.Box<A>,
  ) => Result.Result<Box.Box<A>, internalValidation.LayoutValidationError>) &
    (<A>(
      self: Box.Box<A>,
      options: Place.Options,
    ) => Result.Result<Box.Box<A>, internalValidation.LayoutValidationError>);
  readonly horizontal: ((
    width: number,
    position: Place.Horizontal,
  ) => <A>(self: Box.Box<A>) => Box.Box<A>) &
    (<A>(
      self: Box.Box<A>,
      width: number,
      position: Place.Horizontal,
    ) => Box.Box<A>);
  readonly vertical: ((
    height: number,
    position: Place.Vertical,
  ) => <A>(self: Box.Box<A>) => Box.Box<A>) &
    (<A>(
      self: Box.Box<A>,
      height: number,
      position: Place.Vertical,
    ) => Box.Box<A>);
};

Source