Stack

Use to lay out a horizontal row of components or to achieve no-fuss vertical centering. A stack is made of flexible items that wrap each of the stack’s children. Options provide control of the wrapping, spacing, and relative size of the items in the stack.

Use to quickly lay out a horizontal row of components and maintain their relative sizes. On small screens, children rows wrap down to additional rows as needed.

import {Stack, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack>
      <Badge>Paid</Badge>
      <Badge>Processing</Badge>
      <Badge>Fulfilled</Badge>
      <Badge>Completed</Badge>
    </Stack>
  );
}

Use to create a stack where the children will not wrap to new rows on small screens. As noted above, the wrap option defaults to true. This means you must explicitly set it to false to turn it off.

import {Stack, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack wrap={false}>
      <Badge>Paid</Badge>
      <Badge>Processing</Badge>
      <Badge>Fulfilled</Badge>
      <Badge>Completed</Badge>
    </Stack>
  );
}

Use to control spacing of items in a stack in standard increments. Use tight for less spacing, loose for more spacing, or none to remove normal spacing altogether.

import {Stack, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack spacing="loose">
      <Badge>Paid</Badge>
      <Badge>Fulfilled</Badge>
    </Stack>
  );
}

Use to vertically center a set of items that have different heights.

import {Stack, Heading, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack alignment="center">
      <Heading>
        Order
        <br />
        #1136
        <br />
        was paid
      </Heading>
      <Badge>Paid</Badge>
      <Badge>Fulfilled</Badge>
    </Stack>
  );
}

Use to have the stack’s items fill the horizontal space in the container but maintain their relative proportions.

import {Stack, Heading, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack distribution="fill">
      <Heading>Order #1136</Heading>
      <Badge>Paid</Badge>
      <Badge>Fulfilled</Badge>
    </Stack>
  );
}

Use to have the stack’s items fill the horizontal space in the container and be equal widths, regardless of their content.

import {Stack, Heading, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack distribution="fillEvenly">
      <Heading>Order #1136</Heading>
      <Badge>Paid</Badge>
      <Badge>Fulfilled</Badge>
    </Stack>
  );
}

Use for aligning buttons or secondary content to the right edge of another element, allowing it to wrap below on small screens.

import {Stack, Heading, Badge} from '@shopify/polaris';
import React from 'react';

function StackExample() {
  return (
    <Stack>
      <Stack.Item fill>
        <Heading>Order #1136</Heading>
      </Stack.Item>
      <Stack.Item>
        <Badge>Paid</Badge>
      </Stack.Item>
      <Stack.Item>
        <Badge>Fulfilled</Badge>
      </Stack.Item>
    </Stack>
  );
}

Props

Want to help make this feature better? Please share your feedback.

interface StackProps
children?React.ReactNode

Elements to display inside stack.

wrap?boolean

Wrap stack elements to additional rows as needed on small screens (Defaults to true).

vertical?boolean

Stack the elements vertically.

spacing?'extraTight' | 'tight' | 'baseTight' | 'loose' | 'extraLoose' | 'none'

Adjust spacing between elements.

alignment?'leading' | 'trailing' | 'center' | 'fill' | 'baseline'

Adjust vertical alignment of elements.

distribution?'equalSpacing' | 'leading' | 'trailing' | 'center' | 'fill' | 'fillEvenly'

Adjust horizontal alignment of elements.

Best practices

Stacks should:

  • Be used for small-scale layout tasks when you want a row of components that should wrap on small screen widths
  • Be used to vertically center two elements
  • Not be used for complex or unique arrangements of components
  • Not be used for large-scale page layout

Stack item

The stack component will treat multiple elements wrapped in a stack item component as one item. By default, each individual element is treated as one stack item. Use the fill prop on a single stack item component to make it fill the rest of the available horizontal space. See the “Stack where a single item fills the remaining space” example.

Stack item properties

PropTypeDescriptionDefault
fillbooleanFill the available horizontal space in the stack with the itemfalse
childrenanyElements to display inside stack item


Accessibility

The stack component is for alignment only and doesn’t provide any structural information for assistive technologies. To convey relationships between specific items, consider using the list component.