Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename reduced-motion to motion-reduced, add motion-safe
  • Loading branch information
adamwathan committed Jul 27, 2020
commit 9f3927760741e55eda3fb4e06a3877014c576d93
31 changes: 27 additions & 4 deletions __tests__/variantsAtRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ test('it can generate focus-visible variants', () => {
})
})

test('it can generate reduce-motion variants', () => {
test('it can generate motion-reduced variants', () => {
const input = `
@variants reduce-motion {
@variants motion-reduced {
.banana { color: yellow; }
.chocolate { color: brown; }
}
Expand All @@ -189,8 +189,31 @@ test('it can generate reduce-motion variants', () => {
.banana { color: yellow; }
.chocolate { color: brown; }
@media (prefers-reduced-motion: reduce) {
.reduce-motion\\:banana { color: yellow; }
.reduce-motion\\:chocolate { color: brown; }
.motion-reduced\\:banana { color: yellow; }
.motion-reduced\\:chocolate { color: brown; }
}
`

return run(input).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('it can generate motion-safe variants', () => {
const input = `
@variants motion-safe {
.banana { color: yellow; }
.chocolate { color: brown; }
}
`

const output = `
.banana { color: yellow; }
.chocolate { color: brown; }
@media (prefers-reduced-motion: no-preference) {
.motion-safe\\:banana { color: yellow; }
.motion-safe\\:chocolate { color: brown; }
}
`

Expand Down
19 changes: 17 additions & 2 deletions src/lib/substituteVariantsAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ function ensureIncludesDefault(variants) {

const defaultVariantGenerators = config => ({
default: generateVariantFunction(() => {}),
'reduce-motion': generateVariantFunction(({ container, separator, modifySelectors }) => {
'motion-safe': generateVariantFunction(({ container, separator, modifySelectors }) => {
const modified = modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(sel => {
sel.value = `reduce-motion${separator}${sel.value}`
sel.value = `motion-safe${separator}${sel.value}`
})
}).processSync(selector)
})
const mediaQuery = postcss.atRule({
name: 'media',
params: '(prefers-reduced-motion: no-preference)',
})
mediaQuery.append(modified)
container.append(mediaQuery)
}),
'motion-reduced': generateVariantFunction(({ container, separator, modifySelectors }) => {
const modified = modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(sel => {
sel.value = `motion-reduced${separator}${sel.value}`
})
}).processSync(selector)
})
Expand Down