|
1 | 1 | /** |
2 | | - * Reduced-motion for animations |
| 2 | + * Motion preference helpers for animations |
3 | 3 | * |
4 | | - * @author Paolo Tesei |
| 4 | + * Driven by `html.js-animation` / `html.no-js-animation` (toggled from |
| 5 | + * prefers-reduced-motion in header.php, and by the a11y access config plugin). |
5 | 6 | * |
6 | | - * @param $reduce |
| 7 | + * @author Paolo Tesei |
7 | 8 | * |
8 | 9 | * Examples : |
9 | 10 | * |
10 | 11 | * @include reduced-motion { ... } |
11 | | - * return @media screen and (prefers-reduced-motion : reduce) { ... } |
12 | | - * |
13 | | - * you can add heavy animation using : |
| 12 | + * → html.no-js-animation & { ... } |
| 13 | + * → html.no-js-animation { ... } when used at root / inside :root or html |
14 | 14 | * |
15 | | - * @include reduced-motion (false) { ... } |
16 | | - * return @media screen and (prefers-reduced-motion : no-preference) { ... } |
| 15 | + * @include allows-motion { ... } |
| 16 | + * → html.js-animation & { ... } |
| 17 | + * → html.js-animation { ... } when used at root / inside :root or html |
17 | 18 | * |
18 | 19 | */ |
19 | 20 |
|
20 | | -@mixin reduced-motion($reduce: true) { |
21 | | - @if ($reduce == true) { |
22 | | - @media screen and (prefers-reduced-motion: reduce) { |
| 21 | +@mixin _html-animation-class($class) { |
| 22 | + // `:root` and `html` are the same element as the animation class host, so they |
| 23 | + // cannot appear as descendants of `html.#{$class}`. |
| 24 | + $parent: "#{&}"; |
| 25 | + |
| 26 | + @if not & or $parent == ":root" or $parent == "html" { |
| 27 | + @at-root html.#{$class} { |
23 | 28 | @content; |
24 | 29 | } |
25 | 30 | } @else { |
26 | | - @media screen and (prefers-reduced-motion: no-preference) { |
| 31 | + html.#{$class} & { |
27 | 32 | @content; |
28 | 33 | } |
29 | 34 | } |
30 | 35 | } |
| 36 | + |
| 37 | +// Styles when motion is reduced (`html.no-js-animation`). |
| 38 | +@mixin reduced-motion { |
| 39 | + @include _html-animation-class("no-js-animation") { |
| 40 | + @content; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Styles when animations are allowed (`html.js-animation`). |
| 45 | +@mixin allows-motion { |
| 46 | + @include _html-animation-class("js-animation") { |
| 47 | + @content; |
| 48 | + } |
| 49 | +} |
0 commit comments