GitHub

Stack

A flexible layout component for arranging children vertically or horizontally with consistent spacing.

Loading...

Installation

pnpm add @corew500/ui

Usage

import { Stack } from "@corew500/ui/stack"

<Stack>
  <Card>First</Card>
  <Card>Second</Card>
  <Card>Third</Card>
</Stack>

Direction

Control the layout direction (vertical or horizontal).

{/* Vertical stack (default) */}
<Stack direction="vertical">
  <Card>Card 1</Card>
  <Card>Card 2</Card>
  <Card>Card 3</Card>
</Stack>

{/* Horizontal stack */}
<Stack direction="horizontal">
  <Button>Cancel</Button>
  <Button>Save</Button>
</Stack>

Spacing

Control the gap between children using Tailwind's spacing scale.

<Stack spacing={0}>
  {/* No gap */}
</Stack>

<Stack spacing={2}>
  {/* 0.5rem gap */}
</Stack>

<Stack spacing={4}>
  {/* 1rem gap (default) */}
</Stack>

<Stack spacing={8}>
  {/* 2rem gap */}
</Stack>

Available spacing values: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 8 | 10 | 12

Alignment

Control cross-axis alignment (items-*).

<Stack align="start">
  {/* Align items to start */}
</Stack>

<Stack align="center">
  {/* Center items */}
</Stack>

<Stack align="end">
  {/* Align items to end */}
</Stack>

<Stack align="stretch">
  {/* Stretch items to fill */}
</Stack>

<Stack align="baseline">
  {/* Align to text baseline */}
</Stack>

Justification

Control main-axis distribution (justify-*).

<Stack direction="horizontal" justify="between">
  <Logo />
  <Navigation />
  <UserMenu />
</Stack>

<Stack justify="center">
  {/* Center content */}
</Stack>

<Stack justify="evenly">
  {/* Distribute evenly */}
</Stack>

Available values: start | center | end | between | around | evenly

Wrapping

Allow items to wrap to the next line.

<Stack direction="horizontal" wrap>
  {tags.map(tag => (
    <Badge key={tag}>{tag}</Badge>
  ))}
</Stack>

Full Width

Make the stack expand to fill its container.

<Stack fullWidth>
  <Button fullWidth>Full Width Button</Button>
  <Button fullWidth>Another Button</Button>
</Stack>

Common Patterns

Header Layout

<Stack direction="horizontal" align="center" justify="between" fullWidth>
  <Logo />
  <Navigation />
  <UserMenu />
</Stack>

Form Buttons

<Stack direction="horizontal" spacing={2} justify="end">
  <Button variant="outline">Cancel</Button>
  <Button>Submit</Button>
</Stack>

Card Grid

<Stack direction="horizontal" spacing={4} wrap>
  {products.map(product => (
    <Card key={product.id}>{product.name}</Card>
  ))}
</Stack>

With Container

Stack works well with Container for max-width layouts.

import { Container, Stack } from "@corew500/ui"

<Container size="lg">
  <Stack spacing={8}>
    <Heading level={1}>Page Title</Heading>
    <Text>Page content...</Text>
  </Stack>
</Container>

API Reference

Props

PropTypeDefaultDescription
directionenum"vertical"The flex direction of the stack. - `vertical`: Stacks children in a column (flex-col) - `horizontal`: Stacks children in a row (flex-row) - `responsive`: Vertical on mobile, horizontal on tablet+ (flex-col md:flex-row)
spacingenum4The gap between stack children, using Tailwind's spacing scale. - `0`: No gap - `1`: 0.25rem (4px) - `2`: 0.5rem (8px) - `3`: 0.75rem (12px) - `4`: 1rem (16px) - `5`: 1.25rem (20px) - `6`: 1.5rem (24px) - `8`: 2rem (32px) - `10`: 2.5rem (40px) - `12`: 3rem (48px)
alignenum"stretch"Cross-axis alignment of children (align-items). - `start`: Align to flex-start - `center`: Center alignment - `end`: Align to flex-end - `stretch`: Stretch to fill container - `baseline`: Align to text baseline
justifyenum"start"Main-axis distribution of children (justify-content). - `start`: Pack items to the start - `center`: Center items - `end`: Pack items to the end - `between`: Distribute with space between - `around`: Distribute with space around - `evenly`: Distribute with equal space
wrapenumfalseWhether children should wrap to new lines when they overflow.
fullWidthenumfalseWhether the stack should take full width of its parent.

Accessibility

Keyboard Navigation

  • Does not affect tab order - children maintain their natural focus sequence

Screen Readers

  • Uses a plain `<div>` element; add appropriate roles if needed for landmark regions

Additional

  • - Stack is a purely visual layout component with no semantic meaning

Localization

Translatable Content

  • - Stack has no text content and requires no localization

RTL Support

  • For RTL layouts, horizontal stacks will automatically reverse due to flexbox behavior

Related

Related Components