Flex
A layout container using flexbox. Provides Spectrum dimension values, and supports the gap property to define consistent spacing between items.
install | yarn add @adobe/react-spectrum |
---|---|
version | 3.21.2 |
usage | import {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#
Name | Type | Default | Description |
children | ReactNode | — | Children of the flex container. |
direction | Responsive<'row'
| 'column'
| 'row-reverse'
| 'column-reverse'> | 'row' | The direction in which to layout children. See MDN. |
wrap | Responsive<boolean
| 'wrap'
| 'nowrap'
| 'wrap-reverse'> | false | Whether to wrap items onto multiple lines. See MDN. |
justifyContent | Responsive<'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 | Responsive<'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 | Responsive<'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. |
gap | Responsive<DimensionValue> | — | The space to display between both rows and columns. See MDN. |
columnGap | Responsive<DimensionValue> | — | The space to display between columns. See MDN. |
rowGap | Responsive<DimensionValue> | — | The space to display between rows. See MDN. |
Layout
Name | Type | Default | Description |
flex | Responsive<string
| number
| boolean> | — | When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN. |
flexGrow | Responsive<number> | — | When used in a flex layout, specifies how the element will grow to fit the space available. See MDN. |
flexShrink | Responsive<number> | — | When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN. |
flexBasis | Responsive<number | string> | — | When used in a flex layout, specifies the initial main size of the element. See MDN. |
alignSelf | Responsive<'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 | Responsive<'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. |
order | Responsive<number> | — | The layout order for the element within a flex or grid container. See MDN. |
gridArea | Responsive<string> | — | When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN. |
gridColumn | Responsive<string> | — | When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN. |
gridRow | Responsive<string> | — | When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN. |
gridColumnStart | Responsive<string> | — | When used in a grid layout, specifies the starting column to span within the grid. See MDN. |
gridColumnEnd | Responsive<string> | — | When used in a grid layout, specifies the ending column to span within the grid. See MDN. |
gridRowStart | Responsive<string> | — | When used in a grid layout, specifies the starting row to span within the grid. See MDN. |
gridRowEnd | Responsive<string> | — | When used in a grid layout, specifies the ending row to span within the grid. See MDN. |
Spacing
Name | Type | Default | Description |
margin | Responsive<DimensionValue> | — | The margin for all four sides of the element. See MDN. |
marginTop | Responsive<DimensionValue> | — | The margin for the top side of the element. See MDN. |
marginBottom | Responsive<DimensionValue> | — | The margin for the bottom side of the element. See MDN. |
marginStart | Responsive<DimensionValue> | — | The margin for the logical start side of the element, depending on layout direction. See MDN. |
marginEnd | Responsive<DimensionValue> | — | The margin for the logical end side of an element, depending on layout direction. See MDN. |
marginX | Responsive<DimensionValue> | — | The margin for both the left and right sides of the element. See MDN. |
marginY | Responsive<DimensionValue> | — | The margin for both the top and bottom sides of the element. See MDN. |
Sizing
Name | Type | Default | Description |
width | Responsive<DimensionValue> | — | The width of the element. See MDN. |
minWidth | Responsive<DimensionValue> | — | The minimum width of the element. See MDN. |
maxWidth | Responsive<DimensionValue> | — | The maximum width of the element. See MDN. |
height | Responsive<DimensionValue> | — | The height of the element. See MDN. |
minHeight | Responsive<DimensionValue> | — | The minimum height of the element. See MDN. |
maxHeight | Responsive<DimensionValue> | — | The maximum height of the element. See MDN. |
Positioning
Name | Type | Default | Description |
position | Responsive<'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky'> | — | Specifies how the element is positioned. See MDN. |
top | Responsive<DimensionValue> | — | The top position for the element. See MDN. |
bottom | Responsive<DimensionValue> | — | The bottom position for the element. See MDN. |
left | Responsive<DimensionValue> | — | The left position for the element. See MDN. Consider using start instead for RTL support. |
right | Responsive<DimensionValue> | — | The right position for the element. See MDN. Consider using start instead for RTL support. |
start | Responsive<DimensionValue> | — | The logical start position for the element, depending on layout direction. See MDN. |
end | Responsive<DimensionValue> | — | The logical end position for the element, depending on layout direction. See MDN. |
zIndex | Responsive<number> | — | The stacking order for the element. See MDN. |
isHidden | Responsive<boolean> | — | Hides the element. |
Advanced
Name | Type | Default | Description |
UNSAFE_className | string | — | Sets the CSS className for the element. Only use as a last resort. Use style props instead. |
UNSAFE_style | CSSProperties | — | Sets inline style for the element. Only use as a last resort. Use style props instead. |