Skip to content

Commit ca77c57

Browse files
committed
fix: YozhikM#38
1 parent a7f8931 commit ca77c57

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ testRule(rule, {
2424
code:
2525
'a { animation-name: skew; } @media screen and (prefers-reduced-motion) { a { animation: none } }',
2626
},
27+
{
28+
code:
29+
'.foo { transition: all; @media (prefers-reduced-motion: reduce) { transition: none; } }',
30+
},
2731
],
2832

2933
reject: [

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ export const messages = utils.ruleMessages(ruleName, {
1212
});
1313
const targetProperties = ['transition', 'animation', 'animation-name'];
1414

15+
function checkChildrenNodes(childrenNodes, currentSelector, parentNode) {
16+
return childrenNodes.some(declaration => {
17+
const index = targetProperties.indexOf(declaration.prop);
18+
if (currentSelector === 'animation-name' && targetProperties[index] === 'animation')
19+
return true;
20+
if (currentSelector !== targetProperties[index]) return false;
21+
if (declaration.value !== 'none') return false;
22+
23+
return index >= 0 && parentNode.params.indexOf('prefers-reduced-motion') >= 0;
24+
});
25+
}
26+
1527
function check(selector, node) {
1628
const declarations = node.nodes;
1729
const params = node.parent.params;
@@ -48,24 +60,29 @@ function check(selector, node) {
4860
return parentNode.nodes.some(childrenNode => {
4961
const childrenNodes = childrenNode.nodes;
5062

63+
if (
64+
childrenNode.type === 'atrule' &&
65+
childrenNode.params.indexOf('prefers-reduced-motion') >= 0
66+
) {
67+
return childrenNodes.some(declaration => {
68+
const index = targetProperties.indexOf(declaration.prop);
69+
if (currentSelector === 'animation-name' && targetProperties[index] === 'animation')
70+
return true;
71+
if (currentSelector !== targetProperties[index]) return false;
72+
if (declaration.value !== 'none') return false;
73+
74+
return index >= 0;
75+
});
76+
}
77+
5178
if (
5279
!parentNode.params ||
5380
!Array.isArray(childrenNodes) ||
5481
selector !== childrenNode.selector
5582
)
5683
return false;
5784

58-
const matchedChildrenNodes = childrenNodes.some(declaration => {
59-
const index = targetProperties.indexOf(declaration.prop);
60-
if (currentSelector === 'animation-name' && targetProperties[index] === 'animation')
61-
return true;
62-
if (currentSelector !== targetProperties[index]) return false;
63-
if (declaration.value !== 'none') return false;
64-
65-
return index >= 0 && parentNode.params.indexOf('prefers-reduced-motion') >= 0;
66-
});
67-
68-
return matchedChildrenNodes;
85+
return checkChildrenNodes(childrenNodes, currentSelector, parentNode);
6986
});
7087
});
7188

0 commit comments

Comments
 (0)