|
1 | 1 | // @flow |
2 | 2 | import * as React from 'react' |
3 | 3 | import hoistNonReactStatics from 'hoist-non-react-statics' |
4 | | -import {type StyleSheet, type Classes} from 'jss' |
5 | | -import {ThemeContext} from 'theming' |
| 4 | +import {type Classes} from 'jss' |
| 5 | +import {ThemeContext as DefaultThemeContext} from 'theming' |
6 | 6 |
|
7 | | -import type {HOCProps, HOCOptions, Styles, InnerProps, DynamicRules} from './types' |
| 7 | +import type {HOCProps, HOCOptions, Styles, InnerProps} from './types' |
8 | 8 | import getDisplayName from './getDisplayName' |
9 | 9 | import memoize from './utils/memoizeOne' |
10 | 10 | import mergeClasses from './utils/mergeClasses' |
11 | | -import JssContext from './JssContext' |
12 | 11 | import getSheetIndex from './utils/getSheetIndex' |
13 | | -import { |
14 | | - createStyleSheet, |
15 | | - updateDynamicRules, |
16 | | - addDynamicRules, |
17 | | - removeDynamicRules |
18 | | -} from './utils/sheets' |
19 | | -import {manageSheet, unmanageSheet} from './utils/managers' |
20 | | -import getSheetClasses from './utils/getSheetClasses' |
21 | | - |
22 | | -interface State { |
23 | | - dynamicRules: ?DynamicRules; |
24 | | - sheet: ?StyleSheet; |
25 | | - classes: {}; |
26 | | -} |
| 12 | +import createUseStyles from './createUseStyles' |
27 | 13 |
|
28 | 14 | const NoRenderer = (props: {children?: React.Node}) => props.children || null |
29 | 15 |
|
30 | | -const noTheme = {} |
31 | | - |
32 | | -type CreateWithStyles = <Theme>( |
33 | | - Styles<Theme>, |
34 | | - HOCOptions<Theme> | void |
35 | | -) => <Props: InnerProps>(React.ComponentType<Props>) => React.ComponentType<Props> |
| 16 | +type CreateWithStyles = <Theme: {}>(Styles<Theme>, HOCOptions<Theme> | void) => any => Classes |
36 | 17 |
|
37 | 18 | /** |
38 | 19 | * HOC creator function that wrapps the user component. |
39 | 20 | * |
40 | 21 | * `withStyles(styles, [options])(Component)` |
41 | 22 | */ |
| 23 | + |
42 | 24 | const createWithStyles: CreateWithStyles = <Theme>(styles, options = {}) => { |
43 | 25 | const {index = getSheetIndex(), theming, injectTheme, ...sheetOptions} = options |
44 | | - const isThemingEnabled = typeof styles === 'function' |
45 | | - const ThemeConsumer = (theming && theming.context.Consumer) || ThemeContext.Consumer |
| 26 | + const ThemeContext = theming ? theming.context : DefaultThemeContext |
46 | 27 |
|
47 | 28 | return <Props: InnerProps>(InnerComponent = NoRenderer) => { |
48 | 29 | const displayName = getDisplayName(InnerComponent) |
49 | 30 |
|
50 | | - const getTheme = (props): Theme => (isThemingEnabled ? props.theme : ((noTheme: any): Theme)) |
51 | | - |
52 | | - class WithStyles extends React.Component<HOCProps<Theme, Props>, State> { |
53 | | - static displayName = `WithStyles(${displayName})` |
| 31 | + const mergeClassesProp = memoize( |
| 32 | + (sheetClasses, classesProp): Classes => |
| 33 | + classesProp ? mergeClasses(sheetClasses, classesProp) : sheetClasses |
| 34 | + ) |
54 | 35 |
|
55 | | - // $FlowFixMe[prop-missing] |
56 | | - static defaultProps = {...InnerComponent.defaultProps} |
| 36 | + const hookOptions = Object.assign((sheetOptions: any), { |
| 37 | + theming, |
| 38 | + index, |
| 39 | + name: displayName |
| 40 | + }) |
57 | 41 |
|
58 | | - static createState(props) { |
59 | | - const sheet = createStyleSheet({ |
60 | | - styles, |
61 | | - theme: getTheme(props), |
62 | | - index, |
63 | | - name: displayName, |
64 | | - context: props.jssContext, |
65 | | - sheetOptions |
66 | | - }) |
| 42 | + const useStyles = createUseStyles(styles, hookOptions) |
67 | 43 |
|
68 | | - if (!sheet) { |
69 | | - return {classes: {}, dynamicRules: undefined, sheet: undefined} |
70 | | - } |
| 44 | + const WithStyles = React.forwardRef((props: HOCProps<Theme, Props>, ref) => { |
| 45 | + const theme = React.useContext(ThemeContext) |
71 | 46 |
|
72 | | - const dynamicRules = addDynamicRules(sheet, props) |
| 47 | + const newProps: Props & {theme: any} = {...props} |
73 | 48 |
|
74 | | - return { |
75 | | - sheet, |
76 | | - dynamicRules, |
77 | | - classes: getSheetClasses(sheet, dynamicRules) |
78 | | - } |
| 49 | + if (injectTheme && newProps.theme == null) { |
| 50 | + newProps.theme = theme |
79 | 51 | } |
80 | 52 |
|
81 | | - static manage(props, state) { |
82 | | - const {sheet} = state |
83 | | - if (sheet) { |
84 | | - manageSheet({ |
85 | | - sheet, |
86 | | - index, |
87 | | - context: props.jssContext, |
88 | | - theme: getTheme(props) |
89 | | - }) |
90 | | - } |
91 | | - } |
| 53 | + const sheetClasses = useStyles(newProps) |
92 | 54 |
|
93 | | - static unmanage(props, state) { |
94 | | - const {sheet, dynamicRules} = state |
95 | | - |
96 | | - if (sheet) { |
97 | | - unmanageSheet({ |
98 | | - context: props.jssContext, |
99 | | - index, |
100 | | - sheet, |
101 | | - theme: getTheme(props) |
102 | | - }) |
103 | | - |
104 | | - if (dynamicRules) { |
105 | | - removeDynamicRules(sheet, dynamicRules) |
106 | | - } |
107 | | - } |
108 | | - } |
| 55 | + const classes = mergeClassesProp(sheetClasses, props.classes) |
109 | 56 |
|
110 | | - mergeClassesProp = memoize( |
111 | | - (sheetClasses, classesProp): Classes => |
112 | | - classesProp ? mergeClasses(sheetClasses, classesProp) : sheetClasses |
113 | | - ) |
| 57 | + return <InnerComponent {...newProps} classes={classes} ref={ref} /> |
| 58 | + }) |
114 | 59 |
|
115 | | - constructor(props: HOCProps<Theme, Props>) { |
116 | | - super(props) |
| 60 | + WithStyles.displayName = `WithStyles(${displayName})` |
117 | 61 |
|
118 | | - this.state = WithStyles.createState(props) |
119 | | - const {registry} = props.jssContext |
120 | | - const {sheet} = this.state |
121 | | - if (sheet && registry) { |
122 | | - registry.add(sheet) |
123 | | - } |
124 | | - } |
| 62 | + // $FlowFixMe[prop-missing] https://github.com/facebook/flow/issues/7467 |
| 63 | + WithStyles.defaultProps = {...InnerComponent.defaultProps} |
125 | 64 |
|
126 | | - componentDidMount() { |
127 | | - const {props, state} = this |
128 | | - if (props && state) { |
129 | | - WithStyles.manage(props, state) |
130 | | - } |
131 | | - } |
| 65 | + // $FlowFixMe[prop-missing] |
| 66 | + WithStyles.InnerComponent = InnerComponent |
132 | 67 |
|
133 | | - componentDidUpdate(prevProps: HOCProps<Theme, Props>, prevState: State) { |
134 | | - if (isThemingEnabled && this.props.theme !== prevProps.theme) { |
135 | | - const newState = WithStyles.createState(this.props) |
136 | | - WithStyles.manage(this.props, newState) |
137 | | - WithStyles.unmanage(prevProps, prevState) |
138 | | - |
139 | | - // eslint-disable-next-line react/no-did-update-set-state |
140 | | - this.setState(newState) |
141 | | - } else if (this.state.sheet && this.state.dynamicRules) { |
142 | | - // Only update the rules when we don't generate a new sheet |
143 | | - updateDynamicRules(this.props, this.state.sheet, this.state.dynamicRules) |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - componentWillUnmount() { |
148 | | - WithStyles.unmanage(this.props, this.state) |
149 | | - } |
150 | | - |
151 | | - render() { |
152 | | - const {innerRef, jssContext, theme, classes, ...rest} = this.props |
153 | | - const {classes: sheetClasses} = this.state |
154 | | - const props = { |
155 | | - ...rest, |
156 | | - classes: this.mergeClassesProp(sheetClasses, classes) |
157 | | - } |
158 | | - |
159 | | - if (innerRef) props.ref = innerRef |
160 | | - if (injectTheme) props.theme = theme |
161 | | - |
162 | | - return <InnerComponent {...props} /> |
163 | | - } |
164 | | - } |
165 | | - |
166 | | - const JssContextSubscriber = React.forwardRef((props, ref) => ( |
167 | | - <JssContext.Consumer> |
168 | | - {context => { |
169 | | - if (isThemingEnabled || injectTheme) { |
170 | | - return ( |
171 | | - <ThemeConsumer> |
172 | | - {theme => ( |
173 | | - <WithStyles innerRef={ref} theme={theme} {...props} jssContext={context} /> |
174 | | - )} |
175 | | - </ThemeConsumer> |
176 | | - ) |
177 | | - } |
178 | | - |
179 | | - return <WithStyles innerRef={ref} {...props} jssContext={context} theme={noTheme} /> |
180 | | - }} |
181 | | - </JssContext.Consumer> |
182 | | - )) |
183 | | - |
184 | | - JssContextSubscriber.displayName = `JssContextSubscriber(${displayName})` |
185 | | - // $FlowFixMe[prop-missing] - React's types should allow custom static properties on component. |
186 | | - JssContextSubscriber.InnerComponent = InnerComponent |
187 | | - |
188 | | - return hoistNonReactStatics(JssContextSubscriber, InnerComponent) |
| 68 | + return hoistNonReactStatics(WithStyles, InnerComponent) |
189 | 69 | } |
190 | 70 | } |
191 | 71 |
|
|
0 commit comments