Skip to content

Commit 5dcb9f9

Browse files
committed
Add reduce-motion variant
1 parent 4ee53f3 commit 5dcb9f9

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

__tests__/variantsAtRule.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,29 @@ test('it can generate focus-visible variants', () => {
177177
})
178178
})
179179

180+
test('it can generate reduce-motion variants', () => {
181+
const input = `
182+
@variants reduce-motion {
183+
.banana { color: yellow; }
184+
.chocolate { color: brown; }
185+
}
186+
`
187+
188+
const output = `
189+
.banana { color: yellow; }
190+
.chocolate { color: brown; }
191+
@media (prefers-reduced-motion: reduce) {
192+
.reduce-motion\\:banana { color: yellow; }
193+
.reduce-motion\\:chocolate { color: brown; }
194+
}
195+
`
196+
197+
return run(input).then(result => {
198+
expect(result.css).toMatchCss(output)
199+
expect(result.warnings().length).toBe(0)
200+
})
201+
})
202+
180203
test('it can generate first-child variants', () => {
181204
const input = `
182205
@variants first {

src/lib/substituteVariantsAtRules.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ function ensureIncludesDefault(variants) {
2323

2424
const defaultVariantGenerators = config => ({
2525
default: generateVariantFunction(() => {}),
26+
'reduce-motion': generateVariantFunction(({ container, separator, modifySelectors }) => {
27+
const modified = modifySelectors(({ selector }) => {
28+
return selectorParser(selectors => {
29+
selectors.walkClasses(sel => {
30+
sel.value = `reduce-motion${separator}${sel.value}`
31+
})
32+
}).processSync(selector)
33+
})
34+
const mediaQuery = postcss.atRule({ name: 'media', params: '(prefers-reduced-motion: reduce)' })
35+
mediaQuery.append(modified)
36+
container.append(mediaQuery)
37+
}),
2638
'group-hover': generateVariantFunction(({ modifySelectors, separator }) => {
2739
return modifySelectors(({ selector }) => {
2840
return selectorParser(selectors => {

0 commit comments

Comments
 (0)