File tree Expand file tree Collapse file tree 2 files changed +50
-10
lines changed Expand file tree Collapse file tree 2 files changed +50
-10
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,25 @@ Label {
24
24
:hover {
25
25
color: white;
26
26
}
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
+ }
27
46
}
28
47
` , { requestCSS : 'react-css-components?css!styles.react.css' } ) ;
29
48
console . log ( '--- js' ) ;
Original file line number Diff line number Diff line change 4
4
*/
5
5
6
6
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 ) ) ;
11
11
}
12
- let component = findComponent ( rule ) ;
13
- rule = rule . remove ( ) . clone ( ) ;
14
- rule . selector = component . selector + rule . selector ;
15
- rules . push ( rule ) ;
16
12
} ) ;
17
- root . append ( ...rules ) ;
13
+ root . append ( ...toAppend ) ;
18
14
return root ;
19
15
}
20
16
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
+
21
33
export function isVariant ( node ) {
22
34
return (
23
35
node . type === 'rule' &&
24
36
node . selector . charAt ( 0 ) === ':'
25
37
) ;
26
38
}
27
39
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
+
28
49
function findComponent ( node ) {
29
50
while ( true ) {
30
- if ( node . type === 'rule' && ! isVariant ( node ) ) {
51
+ if ( isComponent ( node ) ) {
31
52
return node ;
32
53
}
33
54
if ( ! node . parent ) {
You can’t perform that action at this time.
0 commit comments