Skip to main content

Stack

Stacks children in a row or column with one consistent gap.

import { Stack } from "@/fragments/ui";Source

Preview

import { Box } from '@/components/Box';
import { Stack } from '@/components/Stack';

<Stack gap="sm">
  <Box background="secondary" rounded="sm" paddingX="md" paddingY="sm">One</Box>
  <Box background="secondary" rounded="sm" paddingX="md" paddingY="sm">Two</Box>

Installation

npx @usefragments/cli add stack

Writes src/fragments/ui/components/Stack plus what it imports, the globals stylesheet, and .fragments/registry-lock.json, then prints the packages to add. No account, no key.

Your own registry: publish from Fragments Cloud and add --registry org/name. Connect a workspace.

Examples

Horizontal Stack

Children flow left to right.

import { Box } from '@/components/Box';
import { Stack } from '@/components/Stack';

<Stack direction="row" gap="sm">
  <Box background="secondary" rounded="sm" paddingX="md" paddingY="sm">One</Box>
  <Box background="secondary" rounded="sm" paddingX="md" paddingY="sm">Two</Box>

Gap Sizes

One spacing token sets every gap in the stack.

import { Box } from '@/components/Box';
import { Stack } from '@/components/Stack';

<Stack gap="lg">
  <Stack direction="row" gap="xs">
    <Box background="secondary" rounded="sm" paddingX="md" paddingY="sm">xs</Box>

Alignment

justify positions children along the main axis.

import { Box } from '@/components/Box';
import { Stack } from '@/components/Stack';

<Stack gap="md">
  <Box width="240px" border rounded="sm" padding="sm">
    <Stack direction="row" gap="sm" justify="between">

Responsive

Swap direction and gap per breakpoint with an object value.

<Stack
  direction={{ base: 'column', md: 'row' }}
  gap={{ base: 'sm', md: 'lg' }}
>
  <Button variant="soft">First</Button>
  <Button variant="soft">Second</Button>

With Separator

Draws a divider between each child.

import { Stack } from '@/components/Stack';
import { Text } from '@/components/Text';

<Stack gap="md" separator>
  <Text>Section one</Text>
  <Text>Section two</Text>

Semantic Element

The as prop renders a real nav, section, or list element.

<Stack as="nav" direction="row" gap="md">
  <Button variant="ghost" size="sm">Home</Button>
  <Button variant="ghost" size="sm">About</Button>
  <Button variant="ghost" size="sm">Contact</Button>
</Stack>

API

Component props
Prop
Type
Default
Description
childrenRequirednodeNot setElements to arrange
directionunioncolumnStack direction: "row", "column", or responsive object
gapunionmdSpacing between items: "none"|"xs"|"sm"|"md"|"lg"|"xl", a number (1-8) for space scale, or responsive object
alignenumstartcenterendstretchbaselineNot setCross-axis alignment
justifyenumstartcenterendbetweenNot setMain-axis alignment
wrapbooleanfalseAllow items to wrap
separatornodeNot setRender a separator between children. true = default 1px line, or pass a ReactNode for custom separators.
asenumdivsectionnavarticleasideheaderfootermainuloldivHTML element to render
classNamestringNot set
styleobjectNot set

Accessibility

  • Use semantic elements (nav, section, etc.) via "as" prop
  • Maintains source order for screen readers
  • No accessibility concerns with visual arrangement

Guidance

Use when

  • Spacing a short row or column of elements
  • Any one-directional flexbox arrangement
  • Layouts that flip direction at a breakpoint

Avoid when

  • Two-dimensional layouts (use Grid)
  • Grouping buttons into one control (use ButtonGroup)
  • Page shell regions (use AppShell)
  • GridalternativeUse Grid for complex 2D layouts
  • ButtonGroupsiblingButtonGroup is specialized for buttons
  • BoxsiblingBox for single-element styling

Next steps