|
1 | | -import {PureComponent, createElement} from 'react' |
2 | | -import {create as createJss, getDynamicStyles} from 'jss' |
| 1 | +import {create as createJss} from 'jss' |
3 | 2 | import preset from 'jss-preset-default' |
4 | | -import raf from 'raf' |
5 | | -import filterProps from './utils/filter-props' |
6 | 3 |
|
7 | | -const jssDefault = createJss(preset()) |
| 4 | +import createStyled from './createStyled' |
8 | 5 |
|
9 | | -type StyledElementAttrsType = { tag: string, styles: Object } |
10 | | -type StyledElementType = Function & StyledElementAttrsType |
11 | | -type tagOrStyledElementTypeype = string | StyledElementType |
12 | | -type StyledElementPropsType = { |
13 | | - classes: Object, |
14 | | - children: ?any, |
15 | | - className: ?string, |
16 | | -} |
| 6 | +const jss: Function = createJss(preset()) |
17 | 7 |
|
18 | | -const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {}) => { |
19 | | - let sheet |
20 | | - let dynamicSheet |
21 | | - let counter = 0 |
| 8 | +export const Styled = createStyled(jss) |
| 9 | +export default Styled() |
22 | 10 |
|
23 | | - let dynamicCounter = 0 |
24 | | - raf(function update() { |
25 | | - if (dynamicCounter) { |
26 | | - dynamicCounter = 0 |
27 | | - dynamicSheet.attach().link() |
28 | | - } |
29 | | - raf(update) |
30 | | - }) |
31 | | - |
32 | | - const styled = ( |
33 | | - tagOrStyledElement: tagOrStyledElementTypeype, |
34 | | - ownStyles: Object |
35 | | - ): StyledElementType => { |
36 | | - const {tag, styles}: StyledElementAttrsType = typeof tagOrStyledElement === 'string' |
37 | | - ? {tag: tagOrStyledElement, styles: {}} |
38 | | - : tagOrStyledElement |
39 | | - |
40 | | - const elementStyles = {...styles, ...ownStyles} |
41 | | - const dynamicStyles = getDynamicStyles(elementStyles) |
42 | | - const staticTag = `${tag}-${++counter}` |
43 | | - |
44 | | - return class StyledElement extends PureComponent { |
45 | | - static tag = tag |
46 | | - |
47 | | - static styles = elementStyles |
48 | | - |
49 | | - props: StyledElementPropsType |
50 | | - |
51 | | - tagScoped = '' |
52 | | - |
53 | | - constructor(props) { |
54 | | - super(props) |
55 | | - this.tagScoped = `${tag}-${++counter}` |
56 | | - } |
57 | | - |
58 | | - componentWillMount() { |
59 | | - if (!sheet) { |
60 | | - sheet = jss.createStyleSheet(baseStyles, { |
61 | | - link: true, |
62 | | - meta: 'StaticBaseSheet', |
63 | | - }).attach() |
64 | | - |
65 | | - dynamicSheet = jss.createStyleSheet({}, { |
66 | | - link: true, |
67 | | - meta: 'DynamicComponentSheet', |
68 | | - }).attach() |
69 | | - } |
70 | | - |
71 | | - if (!sheet.getRule(staticTag)) { |
72 | | - sheet.addRule(staticTag, elementStyles) |
73 | | - } |
74 | | - |
75 | | - if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) { |
76 | | - dynamicSheet.detach().addRule(this.tagScoped, dynamicStyles) |
77 | | - dynamicSheet.update(this.tagScoped, this.props) |
78 | | - dynamicCounter++ |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - componentWillReceiveProps(nextProps: StyledElementPropsType) { |
83 | | - if (dynamicStyles) { |
84 | | - dynamicSheet.update(this.tagScoped, nextProps) |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - componentWillUnmount() { |
89 | | - dynamicSheet.deleteRule(this.tagScoped) |
90 | | - } |
91 | | - |
92 | | - render() { |
93 | | - if (!sheet) return null |
94 | | - |
95 | | - const {children, className, ...attrs} = this.props |
96 | | - |
97 | | - const props = filterProps(attrs) |
98 | | - const tagClass = [ |
99 | | - sheet.classes[staticTag], |
100 | | - dynamicSheet.classes[this.tagScoped], |
101 | | - className, |
102 | | - ] |
103 | | - .filter(Boolean) |
104 | | - .join(' ') |
105 | | - |
106 | | - return createElement(tag, {...props, className: tagClass}, children) |
107 | | - } |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - return Object.assign(styled, {styles: baseStyles}) |
112 | | -} |
113 | | - |
114 | | -const defaultStyledCreator = createStyled() |
115 | | -const defaultStyled = defaultStyledCreator() |
116 | | - |
117 | | -export { |
118 | | - createStyled, |
119 | | - defaultStyledCreator as Styled, |
120 | | -} |
121 | | - |
122 | | -export default defaultStyled |
| 11 | +export {default as injectStyled} from './injectStyled' |
0 commit comments