-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: New switcher layout primitive A new layout component at `atoms/switcher`, that lays out its children in a horizontal row with consistent spacing between children. The layout switches to a vertical stack once the width of the component passes below a threshold, or the number of children goes over a limit.. `content-padding` design tokens are now using the same naming scheme as the base size tokens (`l1`, `s1` etc) #779
- Loading branch information
1 parent
1870def
commit 45bc652
Showing
15 changed files
with
336 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { generateLabel } from '../../../../.storybook/helpers'; | ||
|
||
const SwitcherItem = ({ | ||
children, | ||
backgroundColor = 'var(--bs-color-grayscale-light-2)', | ||
}) => { | ||
const switcherItem = document.createElement('div'); | ||
switcherItem.style.backgroundColor = backgroundColor; | ||
switcherItem.style.paddingBlock = 'var(--bs-size-s2)'; | ||
switcherItem.style.paddingInline = 'var(--bs-content-padding-base)'; | ||
switcherItem.style.borderRadius = 'var(--bs-size-s4)'; | ||
switcherItem.innerHTML = children; | ||
return switcherItem; | ||
}; | ||
|
||
const Switcher = ({ length = 3, classname = [], children = [] }) => { | ||
const switcher = document.createElement('div'); | ||
switcher.classList.add('a-switcher'); | ||
classname.forEach((cls) => switcher.classList.add(cls)); | ||
|
||
if (children.length) { | ||
children.forEach((child) => switcher.append(child)); | ||
} else { | ||
for (let child = 0; child < length; child += 1) { | ||
switcher.append( | ||
SwitcherItem({ | ||
children: generateLabel(['switcher', 'child', child + 1]), | ||
}) | ||
); | ||
} | ||
} | ||
return switcher; | ||
}; | ||
|
||
export { Switcher, SwitcherItem }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
@forward 'settings'; | ||
@use './settings'; | ||
@use '../../tools/classname'; | ||
@use '../../tools/design-token'; | ||
@use '../../tools/media-query'; | ||
@use 'sass:map'; | ||
|
||
/* stylelint-disable scss/dollar-variable-default */ | ||
$breakpoint-property-name: design-token.get('switcher', 'breakpoint'); | ||
$spacing-property-name: design-token.get('switcher', 'spacing'); | ||
|
||
#{classname.get($classname-items: 'switcher', $layer: 'atom')} { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: var(#{$spacing-property-name}); | ||
|
||
> * { | ||
flex-basis: calc((var(#{$breakpoint-property-name}) - 100%) * 999); | ||
flex-grow: 1; | ||
} | ||
} | ||
/* stylelint-enable scss/dollar-variable-default */ | ||
|
||
@each $breakpoint, $size-variants in settings.$size-variants { | ||
@include media-query.get($breakpoint) { | ||
@each $size-variant-name, $size-variant in ($size-variants) { | ||
$class: ''; | ||
@if $size-variant-name == '' { | ||
$class: 'switcher'; | ||
} @else { | ||
$class: 'switcher--#{$size-variant-name}'; | ||
} | ||
|
||
/* stylelint-disable max-nesting-depth */ | ||
#{classname.get($classname-items: $class, $layer: 'atom')} { | ||
@if map.has-key($size-variant, 'breakpoint') { | ||
#{$breakpoint-property-name}: #{map.get($size-variant, 'breakpoint')}; | ||
} | ||
|
||
@if map.has-key($size-variant, 'spacing') { | ||
#{$spacing-property-name}: #{map.get($size-variant, 'spacing')}; | ||
} | ||
|
||
@if map.has-key($size-variant, 'limit') { | ||
> :nth-last-child(n + #{map.get($size-variant, 'limit') + 1}), | ||
> :nth-last-child(n + #{map.get($size-variant, 'limit') + 1}) ~ * { | ||
flex-basis: 100%; | ||
} | ||
} | ||
} | ||
/* stylelint-enable max-nesting-depth */ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@use '../../settings/setup'; | ||
@use '../../tools/design-token'; | ||
|
||
$size-variants: ( | ||
'#{setup.$no-media-query}': ( | ||
'': ( | ||
'spacing': var(design-token.get('size', 's5')), | ||
'breakpoint': 30rem, | ||
'limit': 4, | ||
), | ||
), | ||
'l': ( | ||
'': ( | ||
'spacing': var(design-token.get('size', 's3')), | ||
), | ||
), | ||
) !default; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Switcher } from './Switcher'; | ||
|
||
export default { | ||
title: 'Atoms/Switcher', | ||
component: Switcher, | ||
argTypes: {}, | ||
}; | ||
|
||
const Template = (args) => Switcher(args); | ||
|
||
// ***** Size variants ****************** // | ||
|
||
export const One = Template.bind({}); | ||
One.args = { length: 1 }; | ||
|
||
export const Two = Template.bind({}); | ||
Two.args = { length: 2 }; | ||
|
||
export const Three = Template.bind({}); | ||
Three.args = { length: 3 }; | ||
|
||
export const Four = Template.bind({}); | ||
Four.args = { length: 4 }; | ||
|
||
export const Five = Template.bind({}); | ||
Five.args = { length: 5 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs'; | ||
|
||
<Meta title="Atoms/Switcher/Overview" /> | ||
|
||
<Canvas> | ||
<Story id="atoms-switcher--three" /> | ||
</Canvas> | ||
|
||
# Switcher | ||
|
||
A layout atom that places its children in a single horizontal row and ensures consistent space between each. The layout switches to a vertical stack when it is less than `30rem` wide, or when there are more than 4 children (with default configuration). All children will be the same width in both layouts. This component is responsive, applying larger spacing between children when rendered on larger viewports. | ||
|
||
The layout is best suited to small blocks of content such as a list of related buttons or stats. | ||
|
||
The spacing and the breakpoints the component responds to can be [customized](#customization). You can also add extra size variants of the switcher that apply different spacing; the default configuration provides one size variant that applies a larger spacing at the `l` breakpoint. | ||
|
||
<Canvas> | ||
<Story id="atoms-switcher--one" /> | ||
</Canvas> | ||
|
||
<Canvas> | ||
<Story id="atoms-switcher--two" /> | ||
</Canvas> | ||
|
||
<Canvas> | ||
<Story id="atoms-switcher--three" /> | ||
</Canvas> | ||
|
||
<Canvas> | ||
<Story id="atoms-switcher--four" /> | ||
</Canvas> | ||
|
||
When a switcher has more children than the specified limit (4 in the default configuration), it will switch to the vertical layout, to avoid the children being squashed. | ||
|
||
<Canvas> | ||
<Story id="atoms-switcher--five" /> | ||
</Canvas> | ||
|
||
## Customization | ||
|
||
The component expects a Sass list of Sass maps, with the keys being the name of the breakpoint (use `setup.$no-media-query` for the base mobile-first styles) and the values being map with the key being the name of the switcher variant, and the value a list of properties. You can change the breakpoint names or add new breakpoints if you want the component to apply different spacing at extra breakpoints (in which case you probably also want to [edit the available `content` padding design tokens](/docs/design-tokens-content--page) available to you, though you can also pass `size` design tokens directly) | ||
|
||
```scss | ||
@use '~bitstyles/scss/bitstyles/atoms/switcher' with ( | ||
$size-variants: ( | ||
'#{setup.$no-media-query}': ( | ||
'': ( | ||
'spacing': var(design-token.get('size', 's5')), | ||
'breakpoint': 30rem, | ||
'limit': 4, | ||
), | ||
), | ||
'l': ( | ||
'': ( | ||
'spacing': var(design-token.get('size', 's3')), | ||
), | ||
), | ||
) | ||
); | ||
``` | ||
|
||
### Available properties | ||
|
||
| Property | Description | | ||
| ------------ | ----------------------------------------------------------------------------------------------------------------------------- | | ||
| `spacing` | Spacing between each child. This value is used in both horizontal and vertical layouts. | | ||
| `breakpoint` | Minimum inline-size of the row layout, below which it switches to the stack layout. | | ||
| `limit` | The maximum number of children that will fit in the row. If there are more children, the switcher will render a stack layout. | | ||
|
||
### New size variants | ||
|
||
The keys of the variants values above are deliberately left blank — that results in those properties being applied to the base `a-stack` component. If you provide a key, that will be used to create a stack variant. See the example below, where a `tight` variant is created with no spacing | ||
|
||
```scss | ||
@use '~bitstyles/scss/bitstyles/atoms/switcher' with ( | ||
$size-variants: ( | ||
'#{setup.$no-media-query}': ( | ||
'': ( | ||
'spacing': var(design-token.get('size', 's5')), | ||
), | ||
'tight': ( | ||
'spacing': 0, | ||
), | ||
), | ||
) | ||
); | ||
``` | ||
|
||
Produces CSS similar to the following: | ||
|
||
```css | ||
.a-switcher { | ||
gap: var(--bs-size-s5); | ||
} | ||
|
||
.a-switcher--tight { | ||
gap: 0; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.