Skip to content

Commit a2e36c0

Browse files
authored
Merge pull request #218 from caseywilliams/PDS-442/breakpoints
(PDS-442) Add breakpoint tools to sass-variables
2 parents 6f7e7be + 0d68b4c commit a2e36c0

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Overview
2+
3+
This system remains mostly unopinionated on responsive tools, but the `sass-variables` package contains a few mixins to help you customize your own styles based on the viewport width:
4+
5+
* `respond-above` applies styles only above a certain width. For example, the following constrains the width of `.container` elements, but only on larger screens:
6+
```scss
7+
.container {
8+
width: 100%;
9+
10+
@include respond-above(large) {
11+
width: 1180px;
12+
}
13+
}
14+
```
15+
* `respond-below` applies styles only below a certain width. Here's an equivalent example using `respond-below` instead:
16+
```scss
17+
.container {
18+
width: 1180px;
19+
20+
@include respond-below(large) {
21+
width: 100%;
22+
}
23+
}
24+
```
25+
26+
### Breakpoint widths
27+
28+
The mixins respond to three sizes by default:
29+
30+
- `small` (most phone screens)
31+
- `medium` (most tablets, small browser windows)
32+
- `large` (browser windows)
33+
34+
You can customize these values or add your own by redefining the `$breakpoints` map variable in your SCSS before you include the design system styles. Here's an example:
35+
36+
```scss
37+
// Define your breakpoint map:
38+
$breakpoints: (
39+
'small': 576px,
40+
'medium': 768px,
41+
'large': 992px,
42+
'xlarge': 1180px
43+
);
44+
45+
// Import design system stylesheets after your breakpoint definitions:
46+
@import '~@puppet/sass-variables/index';
47+
48+
.container {
49+
@include respond-above(xlarge) {
50+
// Now responds above 1180px
51+
}
52+
}
53+
```
54+
55+
### Which mixin should I use?
56+
57+
The conventional wisdom of "mobile-first" design encourages designing at the smallest widths first, then progressively enhancing the design for larger sizes. This tends to improve the responsiveness of the design overall, since it forces a focus on multiple sizes from the beginning.
58+
59+
We suggest writing your initial styles for smaller screen widths, then using `respond-above` to adapt them to larger screens. `respond-below` is included as a convenience.
60+

packages/design-system-website/styleguide.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ module.exports = {
8787
name: 'Iconography',
8888
content: 'foundations/Iconography.md',
8989
},
90+
{
91+
name: 'Responsiveness',
92+
content: 'foundations/Responsiveness.md',
93+
},
9094
{
9195
name: 'Typography',
9296
content: 'foundations/Typography.md',

packages/sass-variables/_index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
@import './common';
33
@import './fonts';
44
@import './typography';
5+
@import './responsive';
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
$breakpoints: (
2+
'small': 576px,
3+
'medium': 768px,
4+
'large': 992px,
5+
) !default;
6+
7+
@mixin respond-above($breakpoint) {
8+
@if map-has-key($breakpoints, $breakpoint) {
9+
@media (min-width: map-get($breakpoints, $breakpoint)) {
10+
@content;
11+
}
12+
} @else {
13+
@warn "Unrecognized breakpoint '#{$breakpoint}'. Available breakpoints are: #{map-keys($breakpoints)}.";
14+
}
15+
}
16+
17+
@mixin respond-below($breakpoint) {
18+
@if map-has-key($breakpoints, $breakpoint) {
19+
@media (max-width: map-get($breakpoints, $breakpoint) - 1) {
20+
@content;
21+
}
22+
} @else {
23+
@warn "Unrecognized breakpoint '#{$breakpoint}'. Available breakpoints are: #{map-keys($breakpoints)}.";
24+
}
25+
}

0 commit comments

Comments
 (0)