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