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
added3.0.0
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.

All Flex props also support object syntax to define responsive layouts that change at certain breakpoints. See the layout docs for more details.

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 "row" 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
childrenReactNodeChildren of the flex container.
directionResponsive<'row''column''row-reverse''column-reverse'>'row'The direction in which to layout children. See MDN.
wrapResponsive<boolean'wrap''nowrap''wrap-reverse'>falseWhether to wrap items onto multiple lines. See MDN.
justifyContentResponsive<'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.
alignContentResponsive<'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.
alignItemsResponsive<'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.
gapResponsive<DimensionValue>The space to display between both rows and columns. See MDN.
columnGapResponsive<DimensionValue>The space to display between columns. See MDN.
rowGapResponsive<DimensionValue>The space to display between rows. See MDN.
Layout
NameTypeDefaultDescription
flexResponsive<stringnumberboolean>When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrowResponsive<number>When used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinkResponsive<number>When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisResponsive<numberstring>When used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelfResponsive<'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.
justifySelfResponsive<'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.
orderResponsive<number>The layout order for the element within a flex or grid container. See MDN.
gridAreaResponsive<string>When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnResponsive<string>When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowResponsive<string>When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartResponsive<string>When used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndResponsive<string>When used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartResponsive<string>When used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndResponsive<string>When used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginResponsive<DimensionValue>The margin for all four sides of the element. See MDN.
marginTopResponsive<DimensionValue>The margin for the top side of the element. See MDN.
marginBottomResponsive<DimensionValue>The margin for the bottom side of the element. See MDN.
marginStartResponsive<DimensionValue>The margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndResponsive<DimensionValue>The margin for the logical end side of an element, depending on layout direction. See MDN.
marginXResponsive<DimensionValue>The margin for both the left and right sides of the element. See MDN.
marginYResponsive<DimensionValue>The margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthResponsive<DimensionValue>The width of the element. See MDN.
minWidthResponsive<DimensionValue>The minimum width of the element. See MDN.
maxWidthResponsive<DimensionValue>The maximum width of the element. See MDN.
heightResponsive<DimensionValue>The height of the element. See MDN.
minHeightResponsive<DimensionValue>The minimum height of the element. See MDN.
maxHeightResponsive<DimensionValue>The maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
positionResponsive<'static''relative''absolute''fixed''sticky'>Specifies how the element is positioned. See MDN.
topResponsive<DimensionValue>The top position for the element. See MDN.
bottomResponsive<DimensionValue>The bottom position for the element. See MDN.
leftResponsive<DimensionValue>The left position for the element. See MDN. Consider using start instead for RTL support.
rightResponsive<DimensionValue>The right position for the element. See MDN. Consider using start instead for RTL support.
startResponsive<DimensionValue>The logical start position for the element, depending on layout direction. See MDN.
endResponsive<DimensionValue>The logical end position for the element, depending on layout direction. See MDN.
zIndexResponsive<number>The stacking order for the element. See MDN.
isHiddenResponsive<boolean>Hides 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.