FlexGrid
- Library
- UDS Base
- Repository
- github.com/telus/universal-design-system
- Package
- npmjs.com/package/@telus-uds/components-base
- Package version
- 1.6.1
Introduction
import { FlexGrid } from '@telus-uds/components-base'
A mobile-first flexbox grid based on the "12-column grid" pattern.
Guidance
Use FlexGrid to build grid layouts based on divisions of 12: imagine the screen width as being divided into 12 slices: each FlexGrid row contains one or more columns which occupy a given number of those 12 slices.
A FlexGrid is built using a <FlexGrid>
outer container which contains one or more <FlexGrid.Row>
s, then
each row contains one or more <FlexGrid.Col>
columns, each of which claims a number of the 12 slices
available for the overall row. The number of slices claimed may vary by viewport, allowing grids to be created
that change shape based on the viewport.
None of the FlexGrid components depend on being direct children of each other and all may contain non-FlexGrid children, so FlexGrid may be used with other layout and spacing components such as Divider, Spacer, Box and StackView.
See below for detailed documentation on each of the subcomponents.
Alternatives
For simpler or more flexible grid layouts, consider using StackView.
Many grid layouts can be achieved by using <StackView>
columns inside <StackView direction="row">
rows, and StackView directions may be made conditional on viewport (for example,
<StackView direction={{ xs: 'column', md: 'row' }} />
).
Accessibility
Reversing the order of content using *Reverse
props should be done sparingly as it creates a disconnect
between the order of items seen visually on the screen and the order in which items are navigated by
keyboard navigation and by screen readers. This may make pages harder to navigate or more confusing
for sighted users using keyboard navigation or partially-sighted users who use screen readers as an
assistance rather than a replacement to scanning the screen visually.
Tag
prop for semantic HTML
By default, FlexGrid renders a <div>
on web and a View
with no accessibility role on native apps.
To render a HTML attribute with a semantic meaning on web, pass a string naming a HTML semantic layout tag
to the tag
prop. An accessibilityRole
may also be passed if a role is needed in native apps.
Heading tags (h1
, h2
, h3
, h4
, h5
, h6
) are by default mapped to "heading" role in native apps.
See the tag
entry in the props table for the full list of supported tags.
Platform considerations
FlexGrid may be used in both web and native apps.
The prop horizontalAlign
behaves somewhat differently between platforms:
- On web, text elements inside a column or row with the
horizontalAlign
prop applied will inherit left, right or center text alignment corresponding to thathorizontalAlign
prop, unless they either have explicitly settextAlign
styles themselves or they have an ancestor below the FlexGrid that setstextAlign
. - In native apps, text elements do not inherit
textAlign
from thehorizontalAlign
FlexGrid element.
Subcomponents
A FlexGrid consists of:
- One FlexGrid container
- One or more FlexGrid.Row rows
- One or more FlexGrid.Col columns
FlexGrid
All instances of FlexGrid should be within one FlexGrid container. This sets the grid's outer container
and provides all columns and rows with the gutter
prop value.
gutter
By default, 16px of spacing is provided between all columns in all rows. The amount of spacing here is
fixed and cannot be overridden; however, passing false
to the gutter
prop removes this default spacing
from all columns in all rows.
outsideGutter
The prop outsideGutter
controls whether the 16px gutter should also be applied either side of the
FlexGrid row's content. Passing outsideGutter={false}
while keeping gutter={true}
applies the
spacing only between FlexGrid rows.
Do not set both outsideGutter={false}
and gutter={false}
; this will overcompensate and result
in the FlexGrid's rows being wider than its parent.
limitWidth
By default, the FlexGrid sets the maximum width of the overall container to the maximum width of the
current viewport, at viewports sm
or above. To disable this behaviour and set no max width on the
outer container at any viewport, set limitWidth
to false.
Reverse props (xsReverse
, smReverse
...)
"Reverse" props may be set on the outer container for each viewport (xsReverse
, smReverse
,
mdReverse
, lgReverse
, xlReverse
). These control the order of the FlexGrid's rows.
Pass one of these props as true
to reverse the rows at this viewport and above, and pass one
as false
to stop reversing at this viewport or above.
FlexGrid.Row
Within the FlexGrid
container, there should be one or more FlexGrid.Row
rows.
horizontalAlign, verticalAlign and distribute props
These props set the flex alignment behaviours of the immediate children of the row - i.e.
each FlexGrid.Col
wrapper, and any elements placed in the row not inside a FlexGrid.Col
.
These props take a string and are not responsive - it is not expected that flex rows change their alignment styles based on the current viewport.
horizontalAlign
and distribute
apply horizontally and usually only have an effect when
there is leftover space (i.e. when the columns total does not add up to 12
). verticalAlign
applies vertically and usually only has an effect when columns in a row have different heights.
horizontalAlign
values map to justifyContent
and textAlign
styles:
horizontalAlign="start"
maps to{ justifyContent: 'flex-start', textAlign: 'left' }
horizontalAlign="center"
maps to{ justifyContent: 'center', textAlign: 'center' }
horizontalAlign="end"
maps to{ justifyContent: 'flex-end', textAlign: 'right' }
Note that on native apps, due to React Native's limited text style inheritance, text within a
FlexGrid.Row will not inherit these textAlign
values and alignment should still be set on
text components inside the row such as Typography.
distribute
values set the distribution of any leftover space and also map to justifyContent
:
distribute="around"
distributes space evenly before, after and between each item. It maps to{ justifyContent: 'space-around' }
distribute="between"
distributes space evenly between items, leaving the first and last items tight against the left and right edges of the Row. It maps to{ justifyContent: 'space-between' }
If both horizontalAlign
and distribute
props are set, distribute
has priority and
the final justifyContent
style of the rendered will be based on the distribute
value
not the horizontalAlign
value.
verticalAlign
values map to alignItems
styles:
verticalAlign="top"
maps to{ alignItems: 'flex-start' }
verticalAlign="middle"
maps to{ alignItems: 'center' }
verticalAlign="bottom"
maps to{ alignItems: 'flex-end' }
Reverse props (xsReverse
, smReverse
...)
"Reverse" props may be set on the outer container for each viewport (xsReverse
, smReverse
,
mdReverse
, lgReverse
, xlReverse
). These control the order of the columns within this row.
Pass one of these props as true
to reverse the columns at this viewport and above, and pass one
as false
to stop reversing at this viewport or above.
FlexGrid.Col
Within each FlexGrid.Row
row, there should be one or more FlexGrid.Col
columns.
xs
, sm
, md
, lg
and xl
These props take number values, where the number indicates how many twelths of the available width this
column tries to fill. For example, 3
means one quarter of the available width, 4
means a third, 6
means half, 9
means three quarters and 12
means the column tries to fill the whole width.
This applies at and above the viewport specified, until another prop is specified. For example,
<FlexGrid.Col xs={12} md={6} />
occupies the full width at viewports xs
and sm
, and occupies
half the available width at viewports md
, lg
and xl
.
If the values for all the columns in a row exceed 12
, the content of that row will
wrap onto a new line. A common use of this prop is to create layouts that take up one
row on wide screens and which collapse down to multiple rows on narrower screens.
Offset props (xsOffset
, xsOffset
...)
Offset props may be used to leave an amount of space empty before a column at and above the given
viewport (xsOffset
, smOffset
, mdOffset
, lgOffset
, xlOffset
).
Giving a column an offset prop gives similar visual output to preceding a column with an empty
column that has the equivalent responsive prop. For example, <FlexGrid.Col xsOffset={3} mdOffset={6}>
is similar to preceding a column with an empty column <FlexGrid.Col xs={3} md={3} />
.
horizontalAlign
prop
FlexGrid.Col
's horizontalAlign
prop is quite different to FlexGrid.Row
's. It sets the text alignment
of the wrapper (not justifyContent
), and it is a responsive prop: it may take a string ('left'
, 'center'
or 'right'
) or an object keyed by viewports (e.g. { xs: 'left', lg: 'right' }
).
Note that because this works using HTML text inheritance rules, it has no effect on child elements that already have a text alignment specified in their own styles, and it has no effect in native apps.
Props
FlexGrid
props
Name | Type | Default | Description |
---|---|---|---|
limitWidth | bool | true | Whether or not to give the grid a fixed width. This also centres the grid horizontally. |
gutter | bool | true | Whether or not to include gutters in between columns. |
outsideGutter | bool | true | Whether or not to include gutter at the ends to the left and right of the FlexGrid |
xsReverse | bool | Choose if the item order should be reversed from the 'xs' breakpoint. When you pass in false, the order will be normal from the xs breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
smReverse | bool | Choose if the item order should be reversed from the 'sm' breakpoint. When you pass in false, the order will be normal from the sm breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
mdReverse | bool | Choose if the item order should be reversed from the 'md' breakpoint. When you pass in false, the order will be normal from the md breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
lgReverse | bool | Choose if the item order should be reversed from the 'lg' breakpoint. When you pass in false, the order will be normal from the lg breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
xlReverse | bool | Choose if the item order should be reversed from the 'xl' breakpoint. When you pass in false, the order will be normal from the xl breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
tag | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'article' | 'aside' | 'blockquote' | 'footer' | 'figure' | 'form' | 'header' | 'ul' | 'li' | 'main' | 'nav' | 'section' | 'label' | Optional semantic HTML tag, to apply to the container on web. Does not change styling. In native apps, if a header tag is provided ("h1", "h2" etc) and accessibilityRole prop is not defined, the accessibilityRole will default to "heading". | |
children* | node | The rows and columns of the Grid. Will typically be `FlexGrid.Row` and `FlexGrid.Col` components. |
FlexGrid.Col
props
Name | Type | Default | Description |
---|---|---|---|
xs | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | true | false | Specify number of columns within the 'xs' breakpoint range. `0` hides the column. `true` sets the column width automatically; `false` disables the prop | |
sm | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | true | false | Specify number of columns within the 'sm' breakpoint range. `0` hides the column. `true` sets the column width automatically; `false` disables the prop | |
md | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | true | false | Specify number of columns within the 'md' breakpoint range. `0` hides the column. `true` sets the column width automatically; `false` disables the prop | |
lg | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | true | false | Specify number of columns within the 'lg' breakpoint range. `0` hides the column. `true` sets the column width automatically; `false` disables the prop | |
xl | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | true | false | Specify number of columns after the 'xl' breakpoint. `0` hides the column. `true` sets the column width automatically; `false` disables the prop | |
xsOffset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Offset the specified number of columns within the 'xs' breakpoint range. `0` hides the column. | |
smOffset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Offset the specified number of columns within the 'sm' breakpoint range. | |
mdOffset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Offset the specified number of columns within the 'md' breakpoint range. | |
lgOffset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Offset the specified number of columns within the 'lg' breakpoint range. | |
xlOffset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Offset the specified number of columns within the 'xl' breakpoint range. | |
children* | node | The columns of the Grid. Will typically be `FlexGrid.Col` components. | |
horizontalAlign | custom | Align content horizontally within the column. Accepts a `PropType.string` following the [responsive prop](#/Layout?id=responsive) structure. | |
flex | bool | (web only) Stretches the column to fill vertical space using `display: flex`. In native apps, FlexGrid.Col behaves as if this is always true (as do all `View`s). |
FlexGrid.Row
props
Name | Type | Default | Description |
---|---|---|---|
horizontalAlign | 'start' | 'center' | 'end' | Align columns horizontally within their row. | |
verticalAlign | 'top' | 'middle' | 'bottom' | Align columns vertically within their row. | |
distribute | 'around' | 'between' | Distribute empty space around columns. | |
xsReverse | bool | Choose if the item order should be reversed from the 'xs' breakpoint. When you pass in false, the order will be normal from the xs breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
smReverse | bool | Choose if the item order should be reversed from the 'sm' breakpoint. When you pass in false, the order will be normal from the sm breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
mdReverse | bool | Choose if the item order should be reversed from the 'md' breakpoint. When you pass in false, the order will be normal from the md breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
lgReverse | bool | Choose if the item order should be reversed from the 'lg' breakpoint. When you pass in false, the order will be normal from the lg breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
xlReverse | bool | Choose if the item order should be reversed from the 'xl' breakpoint. When you pass in false, the order will be normal from the xl breakpoint. By default, it inherits the behaviour set by the preceding prop. | |
children* | node |