Skip to content

Commit b1a6c3d

Browse files
authored
Add responsive inline size utilities (#1854)
1 parent d946c7a commit b1a6c3d

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': minor
3+
---
4+
5+
Add responsive inline size utilities
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div class="demo-u-size-inline">
2+
{% for columns in 2..6 %}
3+
<div>
4+
<div class="u-size-inline-1/{{columns}} u-pad-n1 u-rounded t-dark t-alternate u-text-center">
5+
1/{{columns}}
6+
</div>
7+
<div class="u-size-inline-{{columns - 1}}/{{columns}} u-pad-n1 u-rounded t-dark t-alternate u-text-center">
8+
{{columns - 1}}/{{columns}}
9+
</div>
10+
</div>
11+
{% endfor %}
12+
<div>
13+
<div class="u-size-inline-full u-pad-n1 u-rounded t-dark t-alternate u-text-center">
14+
full
15+
</div>
16+
</div>
17+
<div>
18+
<div class="u-size-inline-auto u-pad-n1 u-rounded t-dark t-alternate u-text-center">
19+
auto
20+
</div>
21+
</div>
22+
</div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div class="demo-u-size-responsive">
2+
<div class="u-size-inline-1/2 u-size-inline-2/3@s u-size-inline-3/4@m u-size-inline-4/5@l u-size-inline-5/6@xl t-dark t-alternate u-rounded u-pad-n1">
3+
<div>1/2</div>
4+
<div>2/3@s</div>
5+
<div>3/4@m</div>
6+
<div>4/5@l</div>
7+
<div>5/6@xl</div>
8+
</div>
9+
<div class="u-size-inline-1/2 u-size-inline-1/3@s u-size-inline-1/4@m u-size-inline-1/5@l u-size-inline-1/6@xl t-dark t-alternate u-rounded u-pad-n1">
10+
<div>1/2</div>
11+
<div>1/3@s</div>
12+
<div>1/4@m</div>
13+
<div>1/5@l</div>
14+
<div>1/6@xl</div>
15+
</div>
16+
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.demo-u-size-inline {
2+
display: grid;
3+
gap: 1em;
4+
5+
> * {
6+
display: flex;
7+
gap: inherit;
8+
}
9+
}
10+
11+
.demo-u-size-responsive {
12+
display: flex;
13+
gap: 1em;
14+
}

src/utilities/size/size.scss

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@use 'sass:math';
2+
@use '../../mixins/media-query';
3+
4+
// Set the inline size styles in a mixin. The suffix will let us easily append
5+
// breakpoint suffixes.
6+
@mixin _inline-size-utilities($suffix: '') {
7+
@for $columns from 2 through 6 {
8+
@for $column from 1 through $columns {
9+
.u-size-inline-#{$column}\/#{$columns}#{$suffix} {
10+
inline-size: math.div($column, $columns) * 100% !important;
11+
}
12+
}
13+
}
14+
15+
.u-size-inline-full#{$suffix} {
16+
inline-size: 100% !important;
17+
}
18+
19+
.u-size-inline-auto#{$suffix} {
20+
inline-size: auto !important;
21+
}
22+
}
23+
24+
// Include the default versions (no media queries)
25+
@include _inline-size-utilities;
26+
27+
// Include responsive versions. This would be simpler if we could use the
28+
// `breakpoint-classes` mixin instead, but in this case we want all of the
29+
// responsive versions to be grouped by breakpoint so larger breakpoints will
30+
// naturally override smaller ones.
31+
@include media-query.breakpoint-loop using ($key) {
32+
$suffix: \@#{$key};
33+
@include _inline-size-utilities($suffix);
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Story, Canvas, Meta } from '@storybook/addon-docs';
2+
import inlineSizeDemo from './demo/inline-size.twig';
3+
import responsiveDemo from './demo/responsive.twig';
4+
import './demo/styles.scss';
5+
6+
<Meta title="Utilities/Size" />
7+
8+
# Size
9+
10+
Utilities for specifying the size occupied by an element. While largely unnecessary in objects and components thanks to [CSS Grid Layout](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout), these utilities can still be useful for fine-tuning one-off elements in page content.
11+
12+
## Logical Properties
13+
14+
All of our utilities use [logical properties](https://www.w3.org/TR/css-logical-1/) instead of specific directions [in supported browsers](https://caniuse.com/#feat=css-logical-props) to simplify potential localization projects.
15+
16+
The following table maps the traditional physical property for `direction: ltr` and `writing-mode: horizontal-tb` to its equivalent logical property:
17+
18+
| Physical Property | Logical Property |
19+
| ----------------- | ---------------- |
20+
| `height` | `block-size` |
21+
| `width` | `inline-size` |
22+
23+
You can observe how utilities respond to various `direction` and `writing-mode` values by selecting a different "Text flow" in this project's toolbar.
24+
25+
## Inline size
26+
27+
Utilities beginning with `u-size-inline-` specify the `inline-size` of an element:
28+
29+
- `u-size-inline-{fraction}` for percentage widths, where `{fraction}` represents the desired ratio. For example, `u-size-inline-2/5` would set `inline-size` to `40%`. Fractions with denominators of `2` through `6` are supported.
30+
- `u-size-inline-full` sets the `inline-size` to `100%` of the container.
31+
- `u-size-inline-auto` sets the `inline-size` to the natural value for the element.
32+
33+
<Canvas>
34+
<Story name="Inline size (Width)">{() => inlineSizeDemo()}</Story>
35+
</Canvas>
36+
37+
## Responsive
38+
39+
All of these utilities include responsive variations. Appending `@{breakpoint}` to the end of the class name will cause the utility to change for the given breakpoint.
40+
41+
<Canvas>
42+
<Story name="Responsive">{() => responsiveDemo()}</Story>
43+
</Canvas>

0 commit comments

Comments
 (0)