Stack API
Stack is a layout component that arranges its children in a vertical or horizontal stack with consistent spacing between them. It provides a convenient way to create common layouts while maintaining proper spacing between elements.
The Stack component is particularly useful when you need to:
- Create vertical lists of elements with consistent spacing
- Arrange form fields
- Layout groups of buttons or controls
- Create responsive layouts that adapt to different screen sizes
Import
Section titled “Import”import { Stack } from '@cimpress-ui/react';import { Stack, Select, SelectItem, TextField } from '@cimpress-ui/react';
export default function Example() { return ( <Stack gap={16}> <TextField label="Name" /> <Select label="Favorite fruit"> <SelectItem>Apple</SelectItem> <SelectItem>Banana</SelectItem> <SelectItem>Cherry</SelectItem> </Select> </Stack> );}The Stack component requires two props:
children: The elements to be stackedgap: The amount of space between each child element
By default, Stack arranges items vertically in a single column. You can customize the layout using props like direction, align, justify, and wrap.
Horizontal direction
Section titled “Horizontal direction”import { Button, Stack } from '@cimpress-ui/react';
export default function Example() { return ( <Stack gap={16} direction="horizontal"> <Button>First button</Button> <Button>Second button</Button> <Button>Third button</Button> </Stack> );}Stack can arrange items horizontally by setting the direction prop to "horizontal".
Wrapping
Section titled “Wrapping”import { Stack } from '@cimpress-ui/react';import { WireframeGridItem } from '../../components/wireframes/wireframe';
export default function Example() { return ( <Stack gap={64}> <Stack gap={4} direction="horizontal" wrap> <WireframeGridItem width="300px" height="60px"> Wrapping enabled </WireframeGridItem> <WireframeGridItem width="400px" height="50px" /> <WireframeGridItem width="500px" height="40px" /> <WireframeGridItem width="600px" height="40px" /> </Stack>
<Stack gap={4} direction="horizontal"> <WireframeGridItem width="300px" height="60px"> Wrapping disabled </WireframeGridItem> <WireframeGridItem width="400px" height="50px" /> <WireframeGridItem width="500px" height="40px" /> <WireframeGridItem width="600px" height="40px" /> </Stack> </Stack> );}By default, Stack will prevent items from wrapping if there’s not enough space available. If you want items to wrap, set the wrap prop to true.
Responsiveness
Section titled “Responsiveness”import { Stack } from '@cimpress-ui/react';import { WireframeGridItem } from '../../components/wireframes/wireframe.js';
export default function Example() { return ( <Stack gap={16} direction={{ sm: 'vertical', md: 'horizontal' }}> <WireframeGridItem height="100px" /> <WireframeGridItem height="100px" /> <WireframeGridItem height="100px" /> </Stack> );}Stack supports responsive layouts through object syntax. You can specify different directions for different breakpoints, allowing the layout to adapt based on screen size. In this example, items are stacked vertically on mobile devices (sm breakpoint) and horizontally on tablets and larger devices (md breakpoint).
API reference
Section titled “API reference”Stacks its children with a specified amount of space between them.
- Ref<HTMLDivElement>
-
The
reftype for this component.
StackProps
- ReactNode
children *
Section titled “ children * ” -
The items to stack.
- Responsive<Spacing>
gap *
Section titled “ gap * ” -
The amount of space between items.
- Responsive<Direction>
direction
Section titled “ direction ” - Defaults to 'vertical' .
Determines the stacking direction (ie. which direction is the primary axis)
- Responsive<Justify>
justify
Section titled “ justify ” - Defaults to 'start' .
Determines how items are distributed along the primary axis.
- Responsive<Alignment>
align
Section titled “ align ” - Defaults to 'stretch' .
Determines how each item is aligned along the cross axis.
- Responsive<boolean>
- Defaults to false .
Determines whether items will wrap to a new line if stack size is too small.
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.