Flex

A layout container using flexbox. Provides Spectrum dimension values, and supports the gap property to define consistent spacing between items.

installyarn add @adobe/react-spectrum
version3.1.2
usageimport {Flex} from '@adobe/react-spectrum'

Introduction#


The Flex component can be used to layout its children in one dimension with flexbox. Any React Spectrum component can be used as a child, and Flex components can be nested to create more complex layouts.

In addition to the properties widely supported by CSS, React Spectrum also shims the gap property, along with rowGap and columnGap. These properties make it much easier to build layouts with consistent space between each item. The gap can be defined with Spectrum dimension variables to ensure consistency across applications, and allow the layout to adapt to different devices automatically. In addition, these values can be autocompleted in many IDEs for convenience.

Examples#


Vertical stack#

This example shows a simple vertical stack, with a gap between each item defined using a Spectrum dimension variable.

<Flex direction="column" width="size-2000" gap="size-100">
  <View backgroundColor="celery-600" height="size-800" />
  <View backgroundColor="blue-600" height="size-800" />
  <View backgroundColor="magenta-600" height="size-800" />
</Flex>
<Flex direction="column" width="size-2000" gap="size-100">
  <View backgroundColor="celery-600" height="size-800" />
  <View backgroundColor="blue-600" height="size-800" />
  <View backgroundColor="magenta-600" height="size-800" />
</Flex>
<Flex
  direction="column"
  width="size-2000"
  gap="size-100">
  <View
    backgroundColor="celery-600"
    height="size-800"
  />
  <View
    backgroundColor="blue-600"
    height="size-800"
  />
  <View
    backgroundColor="magenta-600"
    height="size-800"
  />
</Flex>

Horizontal stack#

This example shows a horizontal stack.

<Flex direction="row" height="size-800" gap="size-100">
  <View backgroundColor="celery-600" width="size-800" />
  <View backgroundColor="blue-600" width="size-800" />
  <View backgroundColor="magenta-600" width="size-800" />
</Flex>
<Flex direction="row" height="size-800" gap="size-100">
  <View backgroundColor="celery-600" width="size-800" />
  <View backgroundColor="blue-600" width="size-800" />
  <View backgroundColor="magenta-600" width="size-800" />
</Flex>
<Flex
  direction="row"
  height="size-800"
  gap="size-100">
  <View
    backgroundColor="celery-600"
    width="size-800"
  />
  <View
    backgroundColor="blue-600"
    width="size-800"
  />
  <View
    backgroundColor="magenta-600"
    width="size-800"
  />
</Flex>

Nesting#

This example shows how you can nest flexboxes to create more complicated layouts. It also uses the flex prop on one of the children to expand it to fill the repaining space.

<Flex direction="column" gap="size-100">
  <View backgroundColor="celery-600" height="size-800" />
  <Flex direction="row" height="size-3000" gap="size-100">
    <View backgroundColor="indigo-600" width="size-2000" />
    <View backgroundColor="seafoam-600" flex />
  </Flex>
  <View backgroundColor="magenta-600" height="size-800" />
</Flex>
<Flex direction="column" gap="size-100">
  <View backgroundColor="celery-600" height="size-800" />
  <Flex direction="row" height="size-3000" gap="size-100">
    <View
      backgroundColor="indigo-600"
      width="size-2000"
    />
    <View backgroundColor="seafoam-600" flex />
  </Flex>
  <View backgroundColor="magenta-600" height="size-800" />
</Flex>
<Flex
  direction="column"
  gap="size-100">
  <View
    backgroundColor="celery-600"
    height="size-800"
  />
  <Flex
    direction="row"
    height="size-3000"
    gap="size-100">
    <View
      backgroundColor="indigo-600"
      width="size-2000"
    />
    <View
      backgroundColor="seafoam-600"
      flex
    />
  </Flex>
  <View
    backgroundColor="magenta-600"
    height="size-800"
  />
</Flex>

Wrapping#

This example shows how to enable wrapping of items into multiple rows. Resize your browser window to see the items reflow.

<Flex direction="row" gap="size-100" wrap>
  {colors.map((color) => (
    <View
      key={color}
      backgroundColor={color}
      width="size-800"
      height="size-800"
    />
  ))}
</Flex>
<Flex direction="row" gap="size-100" wrap>
  {colors.map((color) => (
    <View
      key={color}
      backgroundColor={color}
      width="size-800"
      height="size-800"
    />
  ))}
</Flex>
<Flex
  direction="row"
  gap="size-100"
  wrap>
  {colors.map(
    (color) => (
      <View
        key={color}
        backgroundColor={
          color
        }
        width="size-800"
        height="size-800"
      />
    )
  )}
</Flex>

Alignment#

The alignItems prop can be used to align items along the cross axis. When direction is "column", this refers to horizontal alignment, and when direction is "row" it refers to vertical alignment.

This example horizontally centers items in a vertical stack.

<Flex direction="column" gap="size-100" alignItems="center">
  <View backgroundColor="celery-600" width="size-800" height="size-800" />
  <View backgroundColor="blue-600" width="size-2000" height="size-800" />
  <View backgroundColor="magenta-600" width="size-800" height="size-800" />
</Flex>
<Flex
  direction="column"
  gap="size-100"
  alignItems="center">
  <View
    backgroundColor="celery-600"
    width="size-800"
    height="size-800"
  />
  <View
    backgroundColor="blue-600"
    width="size-2000"
    height="size-800"
  />
  <View
    backgroundColor="magenta-600"
    width="size-800"
    height="size-800"
  />
</Flex>
<Flex
  direction="column"
  gap="size-100"
  alignItems="center">
  <View
    backgroundColor="celery-600"
    width="size-800"
    height="size-800"
  />
  <View
    backgroundColor="blue-600"
    width="size-2000"
    height="size-800"
  />
  <View
    backgroundColor="magenta-600"
    width="size-800"
    height="size-800"
  />
</Flex>

NOTE: the default value for alignItems is "stretch" according to the CSS spec. This means that if your items do not have a size defined in the cross direction, then they will be stretched to fill the height of the container. This may be undesirable in some cases, e.g. a group of buttons with varying heights. Ensure you set an alignItems value to account for this.

Justification#

The justifyContent prop can be used to align items along the main axis. When direction is "column", this refers to vertical alignment, and when direction is "column" it refers to horizontal alignment.

This example vertically centers the stack of items within the available space defined by a container.

<View height="size-3000" borderWidth="thin" borderColor="dark">
  <Flex
    direction="column"
    gap="size-100"
    justifyContent="center"
    height="100%">
    <View backgroundColor="celery-600" width="size-800" height="size-800" />
    <View backgroundColor="blue-600" width="size-2000" height="size-800" />
    <View backgroundColor="magenta-600" width="size-800" height="size-800" />
  </Flex>
</View>
<View
  height="size-3000"
  borderWidth="thin"
  borderColor="dark">
  <Flex
    direction="column"
    gap="size-100"
    justifyContent="center"
    height="100%">
    <View
      backgroundColor="celery-600"
      width="size-800"
      height="size-800"
    />
    <View
      backgroundColor="blue-600"
      width="size-2000"
      height="size-800"
    />
    <View
      backgroundColor="magenta-600"
      width="size-800"
      height="size-800"
    />
  </Flex>
</View>
<View
  height="size-3000"
  borderWidth="thin"
  borderColor="dark">
  <Flex
    direction="column"
    gap="size-100"
    justifyContent="center"
    height="100%">
    <View
      backgroundColor="celery-600"
      width="size-800"
      height="size-800"
    />
    <View
      backgroundColor="blue-600"
      width="size-2000"
      height="size-800"
    />
    <View
      backgroundColor="magenta-600"
      width="size-800"
      height="size-800"
    />
  </Flex>
</View>

Props#


NameTypeDefaultDescription
childrenReactElementReactElement[]Children of the flex container.
direction'row''column''row-reverse''column-reverse''row'The direction in which to layout children. See MDN.
wrapboolean'wrap''nowrap''wrap-reverse'falseWhether to wrap items onto multiple lines. See MDN.
justifyContent'start''end''center''left''right''space-between''space-around''space-evenly''stretch''baseline''first baseline''last baseline''safe center''unsafe center''stretch'The distribution of space around items along the main axis. See MDN.
alignContent'start''end''center''space-between''space-around''space-evenly''stretch''baseline''first baseline''last baseline''safe center''unsafe center''start'The distribution of space around child items along the cross axis. See MDN.
alignItems'start''end''center''stretch''self-start''self-end''baseline''first baseline''last baseline''safe center''unsafe center''stretch'The alignment of children within their container. See MDN.
gapDimensionValueThe space to display between both rows and columns. See MDN.
columnGapDimensionValueThe space to display between columns. See MDN.
rowGapDimensionValueThe space to display between rows. See MDN.
Layout
NameTypeDefaultDescription
flexstringnumberbooleanWhen used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrownumberWhen used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinknumberWhen used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisnumberstringWhen used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelf'auto''normal''start''end''center''flex-start''flex-end''self-start''self-end''stretch'Overrides the alignItems property of a flex or grid container. See MDN.
justifySelf'auto''normal''start''end''flex-start''flex-end''self-start''self-end''center''left''right''stretch'Specifies how the element is justified inside a flex or grid container. See MDN.
ordernumberThe layout order for the element within a flex or grid container. See MDN.
gridAreastringWhen used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnstringWhen used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowstringWhen used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartstringWhen used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndstringWhen used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartstringWhen used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndstringWhen used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginDimensionValueThe margin for all four sides of the element. See MDN.
marginTopDimensionValueThe margin for the top side of the element. See MDN.
marginBottomDimensionValueThe margin for the bottom side of the element. See MDN.
marginStartDimensionValueThe margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndDimensionValueThe margin for the logical end side of an element, depending on layout direction. See MDN.
marginXDimensionValueThe margin for both the left and right sides of the element. See MDN.
marginYDimensionValueThe margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthDimensionValueThe width of the element. See MDN.
minWidthDimensionValueThe minimum width of the element. See MDN.
maxWidthDimensionValueThe maximum width of the element. See MDN.
heightDimensionValueThe height of the element. See MDN.
minHeightDimensionValueThe minimum height of the element. See MDN.
maxHeightDimensionValueThe maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
position'static''relative''absolute''fixed''sticky'Specifies how the element is positioned. See MDN.
topDimensionValueThe top position for the element. See MDN.
bottomDimensionValueThe bottom position for the element. See MDN.
leftDimensionValueThe left position for the element. See MDN. Consider using start instead for RTL support.
rightDimensionValueThe right position for the element. See MDN. Consider using start instead for RTL support.
startDimensionValueThe logical start position for the element, depending on layout direction. See MDN.
endDimensionValueThe logical end position for the element, depending on layout direction. See MDN.
zIndexnumberThe stacking order for the element. See MDN.
isHiddenbooleanHides the element.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
Advanced
NameTypeDefaultDescription
UNSAFE_classNamestringSets the CSS className for the element. Only use as a last resort. Use style props instead.
UNSAFE_styleCSSPropertiesSets inline style for the element. Only use as a last resort. Use style props instead.