| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- ---
- title: Box
- description: Container component with borders and layout capabilities
- order: 2
- ---
- # Box
- A container component with borders, background colors, and layout capabilities. Use it to create panels, frames, and organized sections.
- ## Basic usage
- ### Renderable API
- ```typescript
- import { BoxRenderable, createCliRenderer } from "@opentui/core"
- const renderer = await createCliRenderer()
- const panel = new BoxRenderable(renderer, {
- id: "panel",
- width: 30,
- height: 10,
- backgroundColor: "#333366",
- borderStyle: "double",
- borderColor: "#FFFFFF",
- })
- renderer.root.add(panel)
- ```
- ### Construct API
- ```typescript
- import { Box, Text, createCliRenderer } from "@opentui/core"
- const renderer = await createCliRenderer()
- renderer.root.add(
- Box(
- {
- width: 30,
- height: 10,
- backgroundColor: "#333366",
- borderStyle: "rounded",
- },
- Text({ content: "Inside the box!" }),
- ),
- )
- ```
- ## Border styles
- ```typescript
- // No border
- {
- border: false
- }
- // Simple border (default style)
- {
- border: true
- }
- // Specific border styles
- {
- borderStyle: "single"
- } // Single line: ┌─┐│└─┘
- {
- borderStyle: "double"
- } // Double line: ╔═╗║╚═╝
- {
- borderStyle: "rounded"
- } // Rounded corners: ╭─╮│╰─╯
- {
- borderStyle: "heavy"
- } // Heavy lines: ┏━┓┃┗━┛
- ```
- ## Titles
- Add a title and bottom title to the box border:
- ```typescript
- const panel = new BoxRenderable(renderer, {
- id: "settings",
- width: 40,
- height: 15,
- borderStyle: "rounded",
- title: "Settings",
- titleAlignment: "center",
- bottomTitle: "Footer",
- bottomTitleAlignment: "center",
- })
- ```
- ### Title alignment (both top and bottom titles)
- ```typescript
- {
- titleAlignment: "left"
- } // ┌─ Title ────────┐
- {
- titleAlignment: "center"
- } // ┌──── Title ─────┐
- {
- titleAlignment: "right"
- } // ┌────────── Title ┐
- {
- bottomTitleAlignment: "left"
- } // └─ Title ────────┘
- {
- bottomTitleAlignment: "center"
- } // └──── Title ─────┘
- {
- bottomTitleAlignment: "right"
- } // └────────── Title ┘
- ```
- ## Layout container
- Box works as a flex container for child elements:
- ```typescript
- const container = Box(
- {
- flexDirection: "column",
- justifyContent: "space-between",
- alignItems: "stretch",
- width: 50,
- height: 20,
- padding: 1,
- gap: 1,
- },
- Text({ content: "Header" }),
- Box({ flexGrow: 1, backgroundColor: "#222" }, Text({ content: "Content area" })),
- Text({ content: "Footer" }),
- )
- ```
- ## Mouse events
- Handle mouse interactions on the box:
- ```typescript
- const button = new BoxRenderable(renderer, {
- id: "button",
- width: 12,
- height: 3,
- border: true,
- backgroundColor: "#444",
- onMouseDown: () => {
- console.log("Button clicked!")
- },
- onMouseOver: () => {
- button.backgroundColor = "#666"
- },
- onMouseOut: () => {
- button.backgroundColor = "#444"
- },
- })
- ```
- ## Properties
- | Property | Type | Default | Description |
- | ---------------------- | ------------------ | -------------- | --------------------------------- |
- | `width` | `number \| string` | - | Width in characters or percentage |
- | `height` | `number \| string` | - | Height in rows or percentage |
- | `backgroundColor` | `string \| RGBA` | - | Background fill color |
- | `border` | `boolean` | `false` | Show border |
- | `borderStyle` | `string` | `"single"` | Border style |
- | `borderColor` | `string \| RGBA` | - | Border color |
- | `title` | `string` | - | Title text in border |
- | `titleAlignment` | `string` | `"left"` | Title position |
- | `bottomTitle` | `string` | - | Bottom title text in border |
- | `bottomTitleAlignment` | `string` | `"left"` | Bottom title position |
- | `padding` | `number` | `0` | Internal padding |
- | `gap` | `number \| string` | - | Gap between children |
- | `flexDirection` | `string` | `"column"` | Child layout direction |
- | `justifyContent` | `string` | `"flex-start"` | Main axis alignment |
- | `alignItems` | `string` | `"stretch"` | Cross axis alignment |
- ## Example: Card component
- ```typescript
- import { Box, Text, t, bold, fg } from "@opentui/core"
- function Card(props: { title: string; description: string }) {
- return Box(
- {
- width: 40,
- borderStyle: "rounded",
- borderColor: "#666",
- padding: 1,
- margin: 1,
- },
- Text({
- content: t`${bold(fg("#00FFFF")(props.title))}`,
- }),
- Text({
- content: props.description,
- fg: "#AAAAAA",
- }),
- )
- }
- renderer.root.add(
- Box(
- { flexDirection: "row", flexWrap: "wrap" },
- Card({ title: "Feature 1", description: "Description of feature 1" }),
- Card({ title: "Feature 2", description: "Description of feature 2" }),
- Card({ title: "Feature 3", description: "Description of feature 3" }),
- ),
- )
- ```
|