Skip to content

Commit 32b480d

Browse files
committed
Add stylis to support nested css + pseudo + mediaq
1 parent 19dd3c2 commit 32b480d

File tree

6 files changed

+354
-34
lines changed

6 files changed

+354
-34
lines changed

api.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import stylis from 'stylis';
23

34
/*
45
This get's used as decorator @css
@@ -69,13 +70,16 @@ let fillProps = (rawCSS, props) => {
6970
}
7071

7172
/*
72-
Add stylesheet for component
73+
Add insert rules in to css-constructor stylesheet
7374
*/
7475

7576
let insertRules = (realCSS) => {
76-
let sheet = getStyleSheet();
77+
let style = getStyleElement();
78+
/* Get unique classname */
7779
let className = getHash(realCSS);
78-
sheet.insertRule(`.${className}{${realCSS}}`, sheet.cssRules.length);
80+
/* Convert nested CSS */
81+
let styles = stylis(`.${className}`, realCSS);
82+
style.innerHTML += styles;
7983
return className;
8084
}
8185

@@ -86,19 +90,14 @@ let getHash = (string) => {
8690
return 'c' + hash;
8791
}
8892

89-
let getStyleSheet = () => {
90-
let sheets = document.styleSheets;
91-
let index = -1;
92-
for (let i = 0; i < sheets.length; i++) {
93-
if (sheets[i].title === 'css-constructor') index = i;
94-
}
95-
if (index !== -1) return sheets[index];
96-
else {
97-
let styleElement = document.createElement('style');
93+
let getStyleElement = () => {
94+
let styleElement = document.querySelector('[title=css-constructor]');
95+
if (!styleElement) {
96+
styleElement = document.createElement('style');
9897
styleElement.setAttribute('title', 'css-constructor');
9998
document.head.appendChild(styleElement);
100-
return styleElement.sheet;
10199
}
100+
return styleElement;
102101
}
103102

104103
let camelCase = (key) => key.replace(/(\-[a-z])/g, $1 => $1.toUpperCase().replace('-',''));

css-constructor.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)