-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
31 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,35 @@ | ||
import {useState, useLayoutEffect} from 'react'; | ||
const {create} = require('nano-css'); | ||
const {addon: addonCssom} = require('nano-css/addon/cssom'); | ||
const {addon: addonPipe} = require('nano-css/addon/pipe'); | ||
|
||
export interface CssPipe { | ||
className: string; | ||
css: (css: object) => void; | ||
remove: () => void; | ||
} | ||
|
||
const flattenSelectors = (css) => { | ||
const flatenned = {}; | ||
const amp = {}; | ||
let hasAmp = false; | ||
|
||
for (const key in css) { | ||
const value = css[key]; | ||
if (typeof value === 'object') { | ||
flatenned[key] = value; | ||
} else { | ||
hasAmp = true; | ||
amp[key] = value; | ||
} | ||
} | ||
if (hasAmp) { | ||
flatenned['&'] = amp; | ||
} | ||
|
||
return flatenned; | ||
}; | ||
|
||
const nano = create(); | ||
addonCssom(nano); | ||
addonPipe(nano); | ||
import {useLayoutEffect, useMemo} from 'react'; | ||
import {create, NanoRenderer} from 'nano-css'; | ||
import {addon as addonCSSOM, CSSOMAddon} from 'nano-css/addon/cssom'; | ||
import {addon as addonVCSSOM, VCSSOMAddon} from 'nano-css/addon/vcssom'; | ||
import {cssToTree} from 'nano-css/addon/vcssom/cssToTree'; | ||
|
||
type Nano = | ||
& NanoRenderer | ||
& CSSOMAddon | ||
& VCSSOMAddon | ||
; | ||
const nano = create() as Nano; | ||
addonCSSOM(nano); | ||
addonVCSSOM(nano); | ||
|
||
let counter = 0; | ||
|
||
const useCss = (css: object): string => { | ||
const [pipe] = useState<CssPipe>(nano.pipe()); | ||
const className = useMemo(() => 'react-use-css-' + (counter++).toString(36), []); | ||
const sheet = useMemo(() => new nano.VSheet(), []); | ||
|
||
useLayoutEffect(() => { | ||
pipe.css(flattenSelectors(css)); | ||
return () => pipe.remove(); | ||
const tree = {}; | ||
cssToTree(tree, css, '.' + className, ''); | ||
sheet.diff(tree); | ||
|
||
return () => { | ||
sheet.diff({}); | ||
}; | ||
}); | ||
|
||
return pipe.className; | ||
return className; | ||
}; | ||
|
||
export default useCss; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters