Flex
Flex component is a Box with display: flex and abilities to apply other flexbox CSS properties via properties of the component.
The Flex component is useful because it helps with several common cases:
- Create a one direction layout (vertical or horizontal) with uses Forma 36 spacing system.
- Compose new component and allow to override element using the asproperty.
Import
import { Flex } from '@contentful/f36-components';// orimport { Flex } from '@contentful/f36-core';
Examples
Margins & Paddings
Flex component inherits behaviour of Box component and allows to pass margins and paddings that are based on our spacing system.
To control the behaviour of the Flex component you can use specific properties that match all main Flexbox related CSS attributes.
Nesting
This example shows how you can nest flexboxes to create more complicated layouts. It also uses the flexGrow prop on one of the children to expand it to fill the repaining space.
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.
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.
Using Flex to align other Forma components
Props (API reference)
- NamealignContentDescriptionDefines how each line is aligned within a flexbox/grid container. "end""baseline""inherit""initial""start""center""-moz-initial""revert""unset"string & {}"space-around""space-between""space-evenly""stretch""flex-end""flex-start""normal"
- NamealignItemsDescriptionDefines how flexbox items are aligned according to the cross axis, within a line of a flexbox container. "end""baseline""inherit""initial""start""center""-moz-initial""revert""unset"string & {}"stretch""flex-end""flex-start""normal""self-end""self-start"
- NamealignSelfDescriptionWorks like align-items, but applies only to a single flexbox item, instead of all of them. "end""baseline""inherit""initial""start""center""-moz-initial""revert""unset"string & {}"stretch""flex-end""flex-start""normal""self-end""self-start"
- NameasHTML Tag or React Component (e.g. div, span, etc)
- NamechildrenDescriptionChild nodes to be rendered in the component ReactNode
- NameclassNameDescriptionCSS class to be appended to the root element string
- NameflexDescriptionA shorthand property for flex-grow, flex-shrink, flex-basis 0"none""auto""inherit""initial""content""-moz-initial""revert""unset""fit-content""max-content""min-content"string & {}number & {}
- NameflexBasisDescriptionDefines the initial size of a flexbox item. 0"auto""inherit""initial""content""-moz-initial""revert""unset""fit-content""max-content""min-content"string & {}"-moz-max-content""-moz-min-content""-webkit-auto"
- NameflexDirectionDescriptionDefines how flexbox items are ordered within a flexbox container. "inherit""initial""-moz-initial""revert""unset""column""column-reverse""row""row-reverse"
- NameflexGrowDescriptionDefines how much a flexbox item should grow if there's space available. "inherit""initial""-moz-initial""revert""unset"number & {}
- NameflexShrinkDescriptionDefines how much a flexbox item should shrink if there's not enough space available. "inherit""initial""-moz-initial""revert""unset"number & {}
- NameflexWrapDescriptionDefines if flexbox items appear on a single line or on multiple lines within a flexbox container. "inherit""initial""wrap""-moz-initial""revert""unset""nowrap""wrap-reverse"
- NamefullHeightDescriptionSets height: 100% falsetrue
- NamefullWidthDescriptionSets width: 100% falsetrue
- NamegapDescriptionDefines a gap between flexbox items. string
- NameisInlineDescriptionSets display: inline-flex falsetrue
- NamejustifyContentDescriptionDefines how flexbox/grid items are aligned according to the main axis, within a flexbox/grid container. "end""inherit""initial""start""left""center""right""-moz-initial""revert""unset"string & {}"space-around""space-between""space-evenly""stretch""flex-end""flex-start""normal"
- NamejustifyItemsDescriptionDefines the default justify-self for all items of the box, giving them all a default way of justifying each box along the appropriate axis. "end""inherit""initial""start""left""center""right""-moz-initial""revert""unset"string & {}"space-around""space-between""space-evenly""stretch""flex-end""flex-start""normal"
- NamejustifySelfDescriptionSets the way a box is justified inside its alignment container along the appropriate axis. "end""auto""baseline""inherit""initial""start""left""center""right""-moz-initial""revert""unset"string & {}"stretch""flex-end""flex-start""normal""self-end""self-start"
- NamemarginDescriptionsets margin to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamemarginBottomDescriptionsets margin-bottom to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamemarginLeftDescriptionsets margin-left to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamemarginRightDescriptionsets margin-right to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamemarginTopDescriptionsets margin-top to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NameorderDescriptionDefines the order of a flexbox item "inherit""initial""-moz-initial""revert""unset"number & {}
- NamepaddingDescriptionsets padding to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamepaddingBottomDescriptionsets padding-bottom to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamepaddingLeftDescriptionsets padding-left to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamepaddingRightDescriptionsets padding-right to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NamepaddingTopDescriptionsets padding-top to one of the corresponding spacing tokens "none""spacing2Xs""spacingXs""spacingS""spacingM""spacingL""spacingXl""spacing2Xl""spacing3Xl""spacing4Xl"
- NametestIdDescriptionA [data-test-id] attribute used for testing purposes string