Skip to content

Commit 0116cc9

Browse files
committed
feat: 🎸 refresh useCss hook
1 parent 19ac56f commit 0116cc9

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dependencies": {
3333
"@types/react": "^16.8.2",
3434
"keyboardjs": "^2.5.1",
35-
"nano-css": "^3.4.0",
35+
"nano-css": "^5.0.0",
3636
"react-wait": "^0.3.0",
3737
"rebound": "^0.1.0",
3838
"throttle-debounce": "^2.0.1",

src/useCss.ts

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,35 @@
1-
import {useState, useLayoutEffect} from 'react';
2-
const {create} = require('nano-css');
3-
const {addon: addonCssom} = require('nano-css/addon/cssom');
4-
const {addon: addonPipe} = require('nano-css/addon/pipe');
5-
6-
export interface CssPipe {
7-
className: string;
8-
css: (css: object) => void;
9-
remove: () => void;
10-
}
11-
12-
const flattenSelectors = (css) => {
13-
const flatenned = {};
14-
const amp = {};
15-
let hasAmp = false;
16-
17-
for (const key in css) {
18-
const value = css[key];
19-
if (typeof value === 'object') {
20-
flatenned[key] = value;
21-
} else {
22-
hasAmp = true;
23-
amp[key] = value;
24-
}
25-
}
26-
if (hasAmp) {
27-
flatenned['&'] = amp;
28-
}
29-
30-
return flatenned;
31-
};
32-
33-
const nano = create();
34-
addonCssom(nano);
35-
addonPipe(nano);
1+
import {useLayoutEffect, useMemo} from 'react';
2+
import {create, NanoRenderer} from 'nano-css';
3+
import {addon as addonCSSOM, CSSOMAddon} from 'nano-css/addon/cssom';
4+
import {addon as addonVCSSOM, VCSSOMAddon} from 'nano-css/addon/vcssom';
5+
import {cssToTree} from 'nano-css/addon/vcssom/cssToTree';
6+
7+
type Nano =
8+
& NanoRenderer
9+
& CSSOMAddon
10+
& VCSSOMAddon
11+
;
12+
const nano = create() as Nano;
13+
addonCSSOM(nano);
14+
addonVCSSOM(nano);
15+
16+
let counter = 0;
3617

3718
const useCss = (css: object): string => {
38-
const [pipe] = useState<CssPipe>(nano.pipe());
19+
const className = useMemo(() => 'react-use-css-' + (counter++).toString(36), []);
20+
const sheet = useMemo(() => new nano.VSheet(), []);
3921

4022
useLayoutEffect(() => {
41-
pipe.css(flattenSelectors(css));
42-
return () => pipe.remove();
23+
const tree = {};
24+
cssToTree(tree, css, '.' + className, '');
25+
sheet.diff(tree);
26+
27+
return () => {
28+
sheet.diff({});
29+
};
4330
});
4431

45-
return pipe.className;
32+
return className;
4633
};
4734

4835
export default useCss;

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7301,10 +7301,10 @@ nan@^2.9.2:
73017301
resolved "https://registry.yarnpkg.com/nan/-/nan-2.11.1.tgz#90e22bccb8ca57ea4cd37cc83d3819b52eea6766"
73027302
integrity sha512-iji6k87OSXa0CcrLl9z+ZiYSuR2o+c0bGuNmXdrhTQTakxytAFsC56SArGYoiHlJlFoHSnvmhpceZJaXkVuOtA==
73037303

7304-
nano-css@^3.4.0:
7305-
version "3.4.0"
7306-
resolved "https://registry.yarnpkg.com/nano-css/-/nano-css-3.4.0.tgz#b8086a6ee5e198e31c74897187d44f52c0177cd7"
7307-
integrity sha512-75LqVARJoJDMGlRpRA8LtzlU8NxkxqkCR67DZgk0LP7tsglfjCFdNJcG4NNEDLLeLvCdnhyljdn54wAVWHoEeg==
7304+
nano-css@^5.0.0:
7305+
version "5.0.0"
7306+
resolved "https://registry.yarnpkg.com/nano-css/-/nano-css-5.0.0.tgz#faad380ef64a307a7fe63145a8d70ebe26cf5331"
7307+
integrity sha512-pk63UbtYcjndBNeIv8OBfknhDHchc6d/V3ROwFDSS2kxfy6Dp5Ab+6mcDy7q3i6JEmSbNyd2EJcYk9ZdxJj/FQ==
73087308
dependencies:
73097309
css-tree "^1.0.0-alpha.28"
73107310
csstype "^2.5.5"

0 commit comments

Comments
 (0)