Skip to main content

GridContainer

The DhlGridContainer component is used to create a container with a specified number of columns. This component can be used in conjunction with the DhlGridCell to easily position elements on the grid.

Import

// with @dhl-official/react-library:
import { DhlGridContainer } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlGridContainer } from "@dhl-official/ui-libraries/react-library"

Code

<DhlGridContainer>
/* Grid Children... */
</DhlGridContainer>

Usage

Defining Responsive Behaviour

The number of columns can be specified for 4 viewports, at the following respective viewports:

To specify the number of columns for each viewport, the columns prop is used.

The columns prop can take either an Array type or a Number type.

Passing a single number value to columns

Supplying the columns prop with a single Number will result in an equal amount of columns across ALL of the previously mentioned viewports.

By default, the DhlGridContainer creates 12 columns.

Default Grid Container

Passing an array of values to columns

You can specify exactly how many columns DhlGridContainer should have for each viewport by supplying the columns prop with an Array.

The index of the array maps to the previously defined viewport breakpoints.

Responsive Grid Container

const responsiveColumns = [3, 6, 8, 12];

<DhlGridContainer columns={responsiveColumns}>
{/* Grid Children... */}
</DhlGridContainer>;

This will result in a DhlGridContainer with the following number of columns at each breakpoint:

  • small: 3 columns
  • medium: 6 columns
  • large: 8 columns
  • xlarge: 12 columns

If an array is specified, but values are not provided for all viewports, then the number of columns will be inherited from the last specified viewport.

Inheriting Grid Container

const responsiveColumns = [3, 6];

<DhlGridContainer columns={responsiveColumns}>
{/* Grid Children... */}
</DhlGridContainer>;

This will result in a DhlGridContainer with the following number of columns at each breakpoint:

  • small: 3 columns
  • medium: 6 columns
  • large: 6 columns
  • xlarge: 6 columns

To prevent this behaviour, you can pass unset as a value in the array for any viewport.

Handling off-grid components

To handle off-grid components, you can set the fullWidth prop to true. This removes all horizontal padding but ensures that the off-grid components still adhere to the max-width defined in DSM.

Example use-cases

The example use-case below creates a DhlGridContainer that takes the full width of its parent container. The gap between rows has been set to var(--dui-size-space-20x). The DhlGridContainer holds DhlGridCell.

<DhlGridContainer
columns={[6, 12]}
isFullWidth
rowGap="var(--dui-size-space-20x)"
>
<DhlGridCell>
<!-- dhl-grid-cell content -->
</DhlGridCell>
<DhlGridCell>
<!-- dhl-grid-cell content -->
</DhlGridCell>
<DhlGridCell>
<!-- dhl-grid-cell content -->
</DhlGridCell>
<DhlGridCell>
<!-- dhl-grid-cell content -->
</DhlGridCell>
</DhlGridContainer>

Readme

Properties

PropertyAttributeDescriptionTypeDefault
columnscolumnsAn optional prop that sets the number of grid columns Please note: Value can be of type number or array If specified as number, the value is used for all viewports If specified as array, the values are used for the viewports in the following order: 0 = small. 1 = medium, 2 = large viewport, 3 = xlarge viewport Use "unset" in larger viewports to ignore settings from smaller viewportsnumber \| number[] \| string \| string[]12
dataClassNamedata-class-nameAn optional class name prop for the component.stringundefined
dataIddata-idAn optional prop. Gives a valid HTML ID attribute value for the component.stringdhl-grid-container-${getRandomString()}
dataTestiddata-testidAn optional prop. The test id attached to the component as a data-testid attribute.stringundefined
isFullWidthis-full-widthAn optional prop that sets the component to 100% width of it's parent container.booleanfalse
rowGaprow-gapAn optional prop that defines the gap between rowsstringundefined

Slots

SlotDescription
"unnamed"unnamed children slot intended for DhlGridCell component(s)

Dependencies

Used by

Graph


Built by DHL User Interface Library Team!