Skip to content

Box

Flexbox container component -- the primary layout primitive. Supports all standard flexbox properties, dimensions, spacing, borders, outlines, and theming. Provides NodeContext to children, enabling useContentRect/useScreenRect hooks.

Import

tsx
import { Box } from "silvery"

Props

BoxProps extends FlexboxProps, StyleProps, TestProps, MouseEventProps, and FocusEventProps.

Layout (FlexboxProps)

PropTypeDefaultDescription
widthnumber | string--Box width
heightnumber | string--Box height
minWidthnumber | string--Minimum width
minHeightnumber | string--Minimum height
maxWidthnumber | string--Maximum width
maxHeightnumber | string--Maximum height
flexGrownumber--Flex grow factor
flexShrinknumber--Flex shrink factor
flexBasisnumber | string--Flex basis
flexDirection"row" | "column" | "row-reverse" | "column-reverse""column"Main axis direction
flexWrap"nowrap" | "wrap" | "wrap-reverse"--Wrap behavior
alignItems"flex-start" | "flex-end" | "center" | "stretch" | "baseline"--Cross-axis alignment of children
alignSelf"auto" | "flex-start" | "flex-end" | "center" | "stretch" | "baseline"--Cross-axis alignment of self
alignContent"flex-start" | "flex-end" | "center" | "stretch" | "space-between" | "space-around" | "space-evenly"--Multi-line cross-axis alignment
justifyContent"flex-start" | "flex-end" | "center" | "space-between" | "space-around" | "space-evenly"--Main-axis alignment
paddingnumber--Padding on all sides
paddingXnumber--Horizontal padding
paddingYnumber--Vertical padding
paddingTopnumber--Top padding
paddingBottomnumber--Bottom padding
paddingLeftnumber--Left padding
paddingRightnumber--Right padding
marginnumber--Margin on all sides
marginXnumber--Horizontal margin
marginYnumber--Vertical margin
marginTopnumber--Top margin
marginBottomnumber--Bottom margin
marginLeftnumber--Left margin
marginRightnumber--Right margin
gapnumber--Gap between children
columnGapnumber--Gap between columns
rowGapnumber--Gap between rows
position"relative" | "absolute" | "sticky" | "static"--Positioning mode
topnumber | string--Top offset (for absolute/relative)
leftnumber | string--Left offset
bottomnumber | string--Bottom offset
rightnumber | string--Right offset
stickyTopnumber--Sticky top offset
stickyBottomnumber--Sticky bottom offset
aspectRationumber--Aspect ratio
display"flex" | "none""flex"Display mode
overflow"visible" | "hidden" | "scroll"--Overflow behavior
overflowX"visible" | "hidden"--Horizontal overflow
overflowY"visible" | "hidden"--Vertical overflow
scrollTonumber--Child index to ensure visible (when overflow="scroll")
scrollOffsetnumber--Explicit scroll offset in rows

Style (StyleProps)

PropTypeDefaultDescription
colorstring--Foreground color (name, hex, or $token)
backgroundColorstring--Background color
boldboolean--Bold text
dimboolean--Dim text
dimColorboolean--Dim text (alias for dim, Ink compatibility)
italicboolean--Italic text
underlineboolean--Enable underline
underlineStyle"single" | "double" | "curly" | "dotted" | "dashed" | false--Underline style variant
underlineColorstring--Underline color (independent of text color)
strikethroughboolean--Strikethrough text
inverseboolean--Inverse (swap fg/bg)

Border & Outline

PropTypeDefaultDescription
borderStyle"single" | "double" | "round" | "bold" | "singleDouble" | "doubleSingle" | "classic"--Border style
borderColorstring--Border color
borderTopboolean--Show top border
borderBottomboolean--Show bottom border
borderLeftboolean--Show left border
borderRightboolean--Show right border
outlineStyle"single" | "double" | "round" | "bold" | "singleDouble" | "doubleSingle" | "classic"--Outline style (no layout impact)
outlineColorstring--Outline color
outlineDimColorboolean--Dim outline
outlineTopbooleantrueShow top outline
outlineBottombooleantrueShow bottom outline
outlineLeftbooleantrueShow left outline
outlineRightbooleantrueShow right outline

Other

PropTypeDefaultDescription
childrenReactNode--Child elements
themeTheme--Override theme for this subtree
pointerEvents"auto" | "none""auto"CSS pointer-events equivalent
onLayout(layout: Rect) => void--Called when layout changes
overflowIndicatorboolean--Show scroll overflow indicators
idstring--Element ID for DOM queries
testIDstring--Test ID for querying nodes
focusableboolean--Whether this node participates in focus tree
focusScopeboolean--Whether this node is a focus scope boundary

Ref: BoxHandle

ts
interface BoxHandle {
  getNode(): AgNode | null
  getContentRect(): Rect | null
  getScreenRect(): Rect | null
}

Usage

tsx
// Basic vertical layout (default)
<Box>
  <Text>Line 1</Text>
  <Text>Line 2</Text>
</Box>

// Horizontal layout with spacing
<Box flexDirection="row" gap={2}>
  <Box width={10}><Text>Left</Text></Box>
  <Box flexGrow={1}><Text>Center</Text></Box>
  <Box width={10}><Text>Right</Text></Box>
</Box>

// With border
<Box borderStyle="single" borderColor="green" padding={1}>
  <Text>Boxed content</Text>
</Box>

// With ref and onLayout
const boxRef = useRef<BoxHandle>(null)
<Box
  ref={boxRef}
  onLayout={(layout) => console.log('Size:', layout.width, layout.height)}
>
  <Text>Content</Text>
</Box>

See Also

  • Text -- text rendering primitive
  • Spacer -- fills available space
  • Fill -- repeats content to fill width

Released under the MIT License.