Skip to content

Commit

Permalink
feat(documentation): add docs page for our grid-system (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschuerch authored Oct 17, 2023
1 parent fda671c commit d2cff34
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 302 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-cycles-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-documentation': minor
---

Added a docs page for our grid-system.
9 changes: 8 additions & 1 deletion packages/documentation/.storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ const preview: Preview = {
'Getting Started',
['Styles', 'Components'],
'Foundations',
['Typography', 'Color', 'Layout', 'Elevation', 'Accessibility'],
[
'Typography',
'Color',
'Layout',
['Breakpoints', 'Grid', 'TODOS'],
'Elevation',
'Accessibility',
],
'Components',
'Internet Header',
['Getting Started', 'Migration Guide', 'Header', 'Breadcrumbs', 'Footer'],
Expand Down
1 change: 1 addition & 0 deletions packages/documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@storybook/theming": "7.4.5",
"@storybook/web-components": "7.4.5",
"@storybook/web-components-vite": "7.4.5",
"@types/css-modules": "1.0.3",
"@types/mdx": "2.0.7",
"@types/react": "18.2.22",
"@types/react-syntax-highlighter": "15.5.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
@use 'sass:list';
@use 'sass:map';
@use '@swisspost/design-system-styles/core' as post;

$breakpointNames: (
xs: Extra small,
sm: Small,
rg: Regular,
md: Medium,
lg: Large,
xl: Extra large,
xxl: Extra extra large,
);
@use '../layout.shared' as shared;

:export {
@each $breakpoint, $value in post.$grid-breakpoints {
$i: list.index(post.$grid-breakpoints, $breakpoint $value);
$name: map.get($breakpointNames, $breakpoint) or $breakpoint;
$name: map.get(shared.$breakpointNames, $breakpoint) or $breakpoint;

breakpoint_#{$breakpoint}_name: $name;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<body>
<header>...</header>
<main>
<div class="container">...</div>
</main>
<footer>...</footer>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Creates a wrapper for a series of columns
@include make-row();

// Make the column element grid-ready (applying everything but the width)
@include make-col-ready();

// Without optional size values, the mixin will create equal columns (similar to using .col)
@include make-col();
@include make-col($size, $columns: $grid-columns);

// Offset with margins
@include make-col-offset($size, $columns: $grid-columns);
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import LinkTo from '@storybook/addon-links/react';
import { parse } from '../../../../utils/sass-export';
import { forEach } from '../../../../utils/react';
import scss from './grid.module.scss';

export const SCSS_VARIABLES = parse(scss);

export const GridBreakpoints = () => (
<div className="table-responsive">
<table className="table">
<thead>
<tr>
<th></th>
{forEach(
SCSS_VARIABLES.breakpoint,
(data: { key: string; value: { dimensions: string } }) => (
<th key={data.key}>
{data.key}
<br />
<small className="fw-normal">{data.value.dimensions}</small>
</th>
),
)}
</tr>
</thead>
<tbody>
<tr>
<th>Name</th>
{forEach(SCSS_VARIABLES.breakpoint, (data: { key: string; value: { name: string } }) => (
<td key={data.key}>{data.value.name}</td>
))}
</tr>
<tr>
<th>
Container <code>max-width</code>
</th>
{forEach(
SCSS_VARIABLES.breakpoint,
(data: { key: string; value: { maxWidth: string } }) => (
<td key={data.key}>{data.value.maxWidth}</td>
),
)}
</tr>
<tr>
<th>
Container <code>padding</code>
</th>
{forEach(
SCSS_VARIABLES.breakpoint,
(data: { key: string; value: { containerPadding: string } }) => (
<td key={data.key}>{data.value.containerPadding}</td>
),
)}
</tr>

<tr>
<th>Class prefix</th>
{forEach(SCSS_VARIABLES.breakpoint, (data: { key: string; value: { infix: string } }) => (
<td key={data.key}>
<code>.col-{data.value.infix !== 'none' && `${data.key}-`}</code>
</td>
))}
</tr>

<tr>
<th>Amount of columns</th>
<td colSpan={Object.keys(SCSS_VARIABLES.breakpoint).length}>
{SCSS_VARIABLES.variables['grid-columns']}
</td>
</tr>

<tr>
<th>Gutter width</th>
<td colSpan={Object.keys(SCSS_VARIABLES.breakpoint).length}>
{SCSS_VARIABLES.variables['grid-gutter-width']}
</td>
</tr>

<tr>
<th>Custom gutters</th>
<td colSpan={Object.keys(SCSS_VARIABLES.breakpoint).length}>
<LinkTo kind="foundations-layout-gutters" story="docs">
yes
</LinkTo>
</td>
</tr>
<tr>
<th>Nestable</th>
<td colSpan={Object.keys(SCSS_VARIABLES.breakpoint).length}>
<a href="#nesting" target="_self">
yes
</a>
</td>
</tr>
<tr>
<th>Column ordering</th>
<td colSpan={Object.keys(SCSS_VARIABLES.breakpoint).length}>
<LinkTo kind="foundations-layout-columns" story="docs">
yes
</LinkTo>
</td>
</tr>
</tbody>
</table>
</div>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Meta, Canvas, Source } from '@storybook/blocks';
import { formatAsMap } from '../../../../utils/sass-export';
import { GridBreakpoints, SCSS_VARIABLES } from './grid.blocks';
import * as GridStories from './grid.stories';
import './grid.styles.scss';
import SampleContainer from './grid-container.sample.html?raw';
import SampleScssMixins from './grid-scss-mixins.sample.scss?raw';

<Meta of={GridStories} />

# Grid

<div className="lead">
Use our powerful mobile-first flexbox grid to build layouts of all shapes and sizes thanks to a
twelve column system, {Object.keys(SCSS_VARIABLES.breakpoint).length} default responsive tiers,
Sass variables and mixins, and dozens of predefined classes.
</div>

## Example

Our grid system uses a series of containers, rows, and columns to layout and align content. It’s built with flexbox and is fully responsive. Below is an example and an in-depth explanation for how the grid system comes together.

<Canvas of={GridStories.Basis} />

The above example creates three equal-width columns across all devices and viewports using our predefined grid classes. Those columns are centered in the page with the parent `.container`.

{/* prettier-ignore */}
<div className="alert alert-warning">
<h4 className="alert-heading">Don't put a single column in a grid</h4>
<p>If you only use one column across all breakpoints, there is no need to wrap your content in a grid.</p>
</div>

<Canvas of={GridStories.SingleColumnOnly} />

## How it works

Breaking it down, here’s how the grid system comes together:

- **Our grid supports {Object.keys(SCSS_VARIABLES.breakpoint).length} responsive breakpoints.** Breakpoints are based on `min-width` media queries, meaning they affect that breakpoint and all those above it. This means you can control container and column sizing and behavior by each breakpoint.
- **Container (e.g. `.container`) center and horizontally pad your content.**<br/>
Don't nest `.containers`, but use them (for example) as a wrapper for your content area:
<Source language="html" dark code={SampleContainer}></Source>
- **Rows are wrappers for columns.** Each column has horizontal `padding` (called a gutter) for controlling the space between them. This `padding` is then counteracted on the rows with negative margins to ensure the content in your columns is visually aligned down the left side. Rows also support modifier classes to uniformly apply column sizing and gutter classes to change the spacing of your content.
- **Columns are incredibly flexible.** There are 12 template columns available per row, allowing you to create different combinations of elements that span any number of columns. Column classes indicate the number of template columns to span (e.g., `.col-4` spans four). Widths are set in percentages so you always have the same relative sizing.
- **Gutters are also responsive and customizable.** Gutter classes are available across all breakpoints, with all the same sizes as our `margin` and `padding` spacing. Change horizontal gutters with `.gx-*` classes, vertical gutters with `.gy-*`, or all gutters with `.g-\*` classes. `.g-0` is also available to remove gutters.
- **Sass variables, maps, and mixins power the grid.** If you don’t want to use the predefined grid classes, you can use the grid’s source Sass to create your own with more semantic markup. We also include some CSS custom properties to consume these Sass variables for even greater flexibility for you.

## Grid options

Our grid system can adapt across all {Object.keys(SCSS_VARIABLES.breakpoint).length} breakpoints.
Each of these breakpoints have their own specifications, a unique class prefix, and some modifiers.

<GridBreakpoints />

## Auto-layout columns

Utilize breakpoint-specific column classes for easy column sizing without an explicit numbered class like `.col-sm-6`.

### Equal-width

For example, here are two grid layouts that apply to every device and viewport, from the smallest to the biggest. Add any number of unit-less classes for each breakpoint you need and every column will be the same width.

<Canvas of={GridStories.EqualWidth} />

### Setting one column width

Auto-layout for flexbox grid columns also means you can set the width of one column and have the sibling columns automatically resize around it. You may use predefined grid classes (as shown below), grid mixins, or inline widths. Note that the other columns will resize no matter the width of the center column.

<Canvas of={GridStories.SettingOneColumnWidth} />

### Variable width content

Use `.col-{breakpoint}-auto` classes to size columns based on the natural width of their content.

<Canvas of={GridStories.VariableWidthContent} />

## Responsive classes

Our grid includes {Object.keys(SCSS_VARIABLES.breakpoint).length} tiers of predefined classes for building complex responsive layouts.

### All breakpoints

For grids that are the same from the smallest of devices to the largest, use the `.col` and `.col-*` classes. Specify a numbered class when you need a particularly sized column.

<Canvas of={GridStories.AllBreakpoints} />

### Stacked to horizontal

Using a single set of `.col-sm-*` classes, you can create a basic grid system that starts out stacked and becomes horizontal at a smaller breakpoint.

<Canvas of={GridStories.StackedToHorizontal} />

### Mix and match

Don’t want your columns to simply stack in some grid tiers? Use a combination of different classes for each tier as needed.

<Canvas of={GridStories.MixAndMatch} />

## Row columns

Use the responsive `.row-cols-*` classes to quickly set the number of columns that best render your content and layout. Whereas normal `.col-*` classes apply to the individual columns (e.g., `.col-md-4`), the row columns classes are set on the parent `.row` as a shortcut. With `.row-cols-auto` you can give the columns their natural width.

Use these row columns classes to quickly create basic grid layouts or to control your card layouts.

<Canvas of={GridStories.RowColumns} />

## Nesting

To nest your content with the default grid, add a new `.row` and set of `.col-*` columns within an existing `.col-*` column. Nested rows should include a set of columns that add up to 12 or fewer.

<Canvas of={GridStories.Nested} />

## CSS

When using our source Sass files, you have the option of using Sass variables and mixins to create custom, semantic, and responsive page layouts. Our predefined grid classes use these same variables and mixins to provide a whole suite of ready-to-use classes for fast responsive layouts.

### Sass variables

Variables and maps determine the number of columns, the gutter width, and the media query point at which to begin floating columns. We use these to generate the predefined grid classes documented above, as well as for the custom mixins listed below.

<Source
language="scss"
dark
code={Object.entries(SCSS_VARIABLES.variables)
.map(([key, value]) => `$${key}: ${formatAsMap(value)};`)
.join('\n')}
></Source>

### Sass mixins

Mixins are used in conjunction with the grid variables to generate semantic CSS for individual grid columns.

<Source language="scss" dark code={SampleScssMixins}></Source>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@use 'sass:list';
@use 'sass:map';
@use '@swisspost/design-system-styles/core' as post;
@use '../layout.shared' as shared;

:export {
@each $breakpoint, $value in post.$grid-breakpoints {
$i: list.index(post.$grid-breakpoints, $breakpoint $value);
$name: map.get(shared.$breakpointNames, $breakpoint) or $breakpoint;

breakpoint_#{$breakpoint}_name: $name;
breakpoint_#{$breakpoint}_maxWidth: map.get(post.$container-max-widths, $breakpoint);
breakpoint_#{$breakpoint}_containerPadding: map.get(post.$grid-container-padding, $breakpoint);

@if $i == 1 {
$keys: map.keys(post.$grid-breakpoints);
$nextValue: map.get(post.$grid-breakpoints, list.nth($keys, $i + 1));

breakpoint_#{$breakpoint}_infix: none;
breakpoint_#{$breakpoint}_dimensions: #{'<' + $nextValue};
} @else {
breakpoint_#{$breakpoint}_infix: #{'<code>' + $breakpoint + '</code>'};
breakpoint_#{$breakpoint}_dimensions: #{'' + $value};
}
}

variables_grid-columns: post.$grid-columns;
variables_grid-gutter-width: post.$grid-gutter-width;
variables_grid-row-columns: post.$grid-row-columns;

@each $breakpoint, $value in post.$grid-breakpoints {
variables_grid-breakpoints_#{$breakpoint}: $value;
variables_grid-container-max-width_#{$breakpoint}: map.get(
post.$container-max-widths,
$breakpoint
);
variables_grid-container-padding_#{$breakpoint}: map.get(
post.$grid-container-padding,
$breakpoint
);
}
}
Loading

0 comments on commit d2cff34

Please sign in to comment.