Overview

Base container for structuring terminal layouts.

Installation

npm i nikcli-tui

Import

import { Box } from 'nikcli-tui';

Quick Start

new Box({ parent: screen, width: 40, height: 8, borderStyle: 'line', label: ' Box ' });

Live Preview

Run Locally

npm run tsx:core-gallery

Production Examples

import blessed from 'blessed';
import { Box, Text } from 'nikcli-tui';
const screen = blessed.screen({ smartCSR: true });
const container = new Box({ parent: screen, top: 1, left: 2, width: 50, height: 12, borderStyle: 'line', label: 'Container' });
new Text({ parent: container.el, top: 1, left: 2, content: 'Hello Box' });
screen.key(['q','C-c'], () => process.exit(0));
screen.render();

Props

Box-specific Props

PropertyTypeRequiredDefaultDescription
contentstringNo-Initial content text for the box
labelstringNo-Label text displayed in border (from component-schemas)
scrollablebooleanNo-Enable scrolling functionality
alwaysScrollbooleanNo-Always show scroll indicators (from component-schemas)
scrollableOptionsScrollableOptionsNo-Advanced scrolling configuration

ScrollableOptions Interface

interface ScrollableOptions {
  scrollbar?: {
    style?: Record<string, any>;
    ch?: string; // Character used for scrollbar
  };
  track?: {
    style?: Record<string, any>;
    ch?: string; // Character used for track
  };
}

Inherited Base Props

The Box component extends BasePropsSchema and includes all standard TUI component properties:
PropertyTypeDefaultDescription
parentblessed.Widgets.Node-Parent blessed element
classNamestring-CSS-like class name for styling
idstring-Unique identifier
variantComponentVariant"default"Component variant (primary, secondary, etc.)
sizeComponentSize-Component size (xs, sm, md, lg, xl)
disabledbooleanfalseWhether component is disabled
hiddenbooleanfalseWhether component is hidden
focusablebooleantrueWhether component can receive focus
scrollablebooleanfalseWhether component is scrollable

Layout & Positioning Props

PropertyTypeDescription
topTerminalUnitTop position (number, percentage, or alignment)
leftTerminalUnitLeft position
rightTerminalUnitRight position
bottomTerminalUnitBottom position
widthTerminalUnitComponent width
heightTerminalUnitComponent height
paddingPaddingConfigInternal spacing
marginPaddingConfigExternal spacing

Style Props

PropertyTypeDescription
bgColorValueBackground color
fgColorValueForeground color
borderBorderStyleBorder configuration
borderColorColorValueBorder color
borderStyle"line" | "double" | "round" | "bold" | "classic" | "none"Border style helper
styleTextStyleText styling (bold, underline, etc.)
animationAnimationTypeAnimation type

Zod Schema Validation

The Box component uses Zod for runtime type validation:
import { BoxSchema } from 'nikcli-tui';

const BoxSchema = BasePropsSchema.extend({
  // Content
  content: z.string().optional(),
  
  // Box-specific styling
  scrollable: z.boolean().optional(),
  scrollableOptions: z.object({
    scrollbar: z.object({
      style: z.record(z.any()).optional(),
      ch: z.string().optional(),
    }).optional(),
    track: z.object({
      style: z.record(z.any()).optional(),
      ch: z.string().optional(),
    }).optional(),
  }).optional(),
}).strict();

Methods

Instance Methods

MethodParametersReturn TypeDescription
setContentcontent: stringvoidSets content with stable width management (streaming-friendly)
destroy-voidCleanup method from base component

Inherited Methods (from BaseComponent)

MethodParametersReturn TypeDescription
setVariantvariant: ComponentVariantvoidUpdates component variant
setSizesize: ComponentSizevoidUpdates component size
setStatestate: ComponentStatevoidUpdates component state
getConfig-ComponentConfigReturns current configuration
updateprops: Partial<BoxProps>voidUpdates component properties

Static Methods

MethodParametersReturn TypeDescription
Box.createprops: BoxPropsBoxFactory method to create new box instance

Streaming-friendly Features

The Box component includes special handling for streaming content:
  • Width stabilization: Tracks maximum content width to prevent layout shifts
  • Safe rendering: Uses safe render methods to avoid screen corruption
  • Minimum width: Sets minimum width automatically based on content tracking

Best Practices

  • Prefer Box as a container; use Panel/Card for more opinionated UI.
  • Use label for semantic section titles; combine with Heading for in-content titles.