Skip to content

Commit d62b925

Browse files
committed
fix: media-prefers-reduced-motion couple fixes
1 parent 0ae7f54 commit d62b925

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/rules/media-prefers-reduced-motion/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export default function mediaPrefersReducedMotion(actual, _, context) {
162162
const isAccepted = check(selector, node)
163163

164164
if (context.fix && !isAccepted) {
165-
const media = parse('@media screen and (prefers-reduced-motion: reduce) {}')
165+
const media = parse('\n@media screen and (prefers-reduced-motion: reduce) {}')
166166

167167
media.nodes.forEach((o) => {
168168
o.raws.after = '\n'
@@ -176,17 +176,24 @@ export default function mediaPrefersReducedMotion(actual, _, context) {
176176
after: '\n',
177177
semicolon: true
178178
}
179-
cloneRule.nodes.forEach((o) => {
180-
if (o.prop === 'animation-name') {
181-
o.prop = 'animation'
182-
}
183179

184-
if (targetProperties.indexOf(o.prop) >= 0) {
185-
o.value = 'none'
180+
cloneRule.removeAll()
181+
182+
node.nodes.forEach((declaration) => {
183+
if (targetProperties.indexOf(declaration.prop) >= 0) {
184+
const newDeclaration = declaration.clone()
185+
186+
if (newDeclaration.prop === 'animation-name') {
187+
newDeclaration.prop = 'animation'
188+
}
189+
190+
newDeclaration.value = 'none'
191+
cloneRule.append(newDeclaration)
186192
}
187193
})
194+
188195
media.first.append(cloneRule)
189-
node.before(media)
196+
node.after(media)
190197

191198
return
192199
}

src/rules/media-prefers-reduced-motion/index.spec.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,40 @@ testRule({
2626
},
2727
{
2828
code: '.foo { transition: all; @media (prefers-reduced-motion: reduce) { transition: none; } }'
29+
},
30+
{
31+
code: '.foo { transition: all 5s; color: red; } @media screen and (prefers-reduced-motion: reduce) { .foo { transition: none } }'
2932
}
3033
],
3134

3235
reject: [
3336
{
3437
code: 'a { animation-name: skew; }',
3538
fixed:
36-
'@media screen and (prefers-reduced-motion: reduce) {\na { animation: none;\n}\n}\na { animation-name: skew; }',
39+
'a { animation-name: skew; }\n@media screen and (prefers-reduced-motion: reduce) {\na { animation: none;\n}\n}',
3740
message: messages.expected('a'),
3841
line: 1,
3942
column: 3
4043
},
4144
{
4245
code: 'a { animation-name: skew; } @media screen and (prefers-reduced-motion) { a { transition: none; } }',
4346
fixed:
44-
'@media screen and (prefers-reduced-motion: reduce) {\na { animation: none;\n}\n} a { animation-name: skew; } @media screen and (prefers-reduced-motion) { a { transition: none; } }',
47+
'a { animation-name: skew; }\n@media screen and (prefers-reduced-motion: reduce) {\na { animation: none;\n}\n} @media screen and (prefers-reduced-motion) { a { transition: none; } }',
4548
message: messages.expected('a'),
4649
line: 1,
4750
column: 3
4851
},
4952
{
5053
code: '.foo { animation: 1s ease-in; } @media screen and (prefers-reduced-motion) { .foo { animation: 1s ease-in; } }',
5154
fixed:
52-
'@media screen and (prefers-reduced-motion: reduce) {\n.foo { animation: none;\n}\n} .foo { animation: 1s ease-in; } @media screen and (prefers-reduced-motion) { .foo { animation: 1s ease-in; } }',
55+
'.foo { animation: 1s ease-in; }\n@media screen and (prefers-reduced-motion: reduce) {\n.foo { animation: none;\n}\n} @media screen and (prefers-reduced-motion) { .foo { animation: 1s ease-in; } }',
56+
message: messages.expected('.foo'),
57+
line: 1,
58+
column: 3
59+
},
60+
{
61+
code: '.foo { transition: all 5s; color: red; }',
62+
fixed: '.foo { transition: all 5s; color: red; }\n@media screen and (prefers-reduced-motion: reduce) {\n.foo { transition: none;\n}\n}',
5363
message: messages.expected('.foo'),
5464
line: 1,
5565
column: 3

0 commit comments

Comments
 (0)