Skip to content

Commit 285abe9

Browse files
committed
feat: support nested pseudoclasses
1 parent 419511b commit 285abe9

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/__tests__/index-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ Label {
2424
:hover {
2525
color: white;
2626
}
27+
}
28+
`, {requestCSS: 'react-css-components?css!styles.react.css'});
29+
console.log('--- js');
30+
console.log(js);
31+
console.log('--- css');
32+
console.log(css);
33+
});
34+
35+
it.only('renders react components with nested pseudoclasses', function() {
36+
let {js, css} = render(`
37+
38+
Label {
39+
color: red;
40+
:hover {
41+
color: white;
42+
:focus {
43+
color: black;
44+
}
45+
}
2746
}
2847
`, {requestCSS: 'react-css-components?css!styles.react.css'});
2948
console.log('--- js');

src/transformVariants.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,51 @@
44
*/
55

66
export default function transformVariants(root) {
7-
let rules = [];
8-
root.walkRules(rule => {
9-
if (!isVariant(rule)) {
10-
return;
7+
let toAppend = [];
8+
root.each(rule => {
9+
if (isComponent(rule)) {
10+
toAppend = toAppend.concat(flattenVariants(rule));
1111
}
12-
let component = findComponent(rule);
13-
rule = rule.remove().clone();
14-
rule.selector = component.selector + rule.selector;
15-
rules.push(rule);
1612
});
17-
root.append(...rules);
13+
root.append(...toAppend);
1814
return root;
1915
}
2016

17+
function flattenVariants(rule) {
18+
let toRemove = [];
19+
let toAppend = [];
20+
rule.each(variant => {
21+
if (!isVariant(variant)) {
22+
return;
23+
}
24+
toRemove.push(variant);
25+
variant = variant.clone();
26+
variant.selector = rule.selector + variant.selector;
27+
toAppend = toAppend.concat(variant, ...flattenVariants(variant));
28+
});
29+
toRemove.forEach(variant => variant.remove());
30+
return toAppend;
31+
}
32+
2133
export function isVariant(node) {
2234
return (
2335
node.type === 'rule' &&
2436
node.selector.charAt(0) === ':'
2537
);
2638
}
2739

40+
export function isComponent(node) {
41+
return (
42+
node.type === 'rule' &&
43+
node.selector.charAt(0) !== ':' &&
44+
node.parent &&
45+
node.parent.type === 'root'
46+
);
47+
}
48+
2849
function findComponent(node) {
2950
while (true) {
30-
if (node.type === 'rule' && !isVariant(node)) {
51+
if (isComponent(node)) {
3152
return node;
3253
}
3354
if (!node.parent) {

0 commit comments

Comments
 (0)