forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeStyles.js
243 lines (202 loc) · 6.24 KB
/
makeStyles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import React from 'react';
import { getDynamicStyles } from 'jss';
import mergeClasses from '../mergeClasses';
import multiKeyStore from './multiKeyStore';
import useTheme from '../useTheme';
import { StylesContext } from '../StylesProvider';
import { increment } from './indexCounter';
import getStylesCreator from '../getStylesCreator';
import noopTheme from '../getStylesCreator/noopTheme';
function getClasses({ state, stylesOptions }, classes, Component) {
if (stylesOptions.disableGeneration) {
return classes || {};
}
if (!state.cacheClasses) {
state.cacheClasses = {
// Cache for the finalized classes value.
value: null,
// Cache for the last used classes prop pointer.
lastProp: null,
// Cache for the last used rendered classes pointer.
lastJSS: {},
};
}
// Tracks if either the rendered classes or classes prop has changed,
// requiring the generation of a new finalized classes object.
let generate = false;
if (state.classes !== state.cacheClasses.lastJSS) {
state.cacheClasses.lastJSS = state.classes;
generate = true;
}
if (classes !== state.cacheClasses.lastProp) {
state.cacheClasses.lastProp = classes;
generate = true;
}
if (generate) {
state.cacheClasses.value = mergeClasses({
baseClasses: state.cacheClasses.lastJSS,
newClasses: classes,
Component,
});
}
return state.cacheClasses.value;
}
function attach({ state, theme, stylesOptions, stylesCreator, name }, props) {
if (stylesOptions.disableGeneration) {
return;
}
let sheetManager = multiKeyStore.get(stylesOptions.sheetsManager, stylesCreator, theme);
if (!sheetManager) {
sheetManager = {
refs: 0,
staticSheet: null,
dynamicStyles: null,
};
multiKeyStore.set(stylesOptions.sheetsManager, stylesCreator, theme, sheetManager);
}
const options = {
...stylesCreator.options,
...stylesOptions,
theme,
flip: typeof stylesOptions.flip === 'boolean' ? stylesOptions.flip : theme.direction === 'rtl',
};
options.generateId = options.serverGenerateClassName || options.generateClassName;
const sheetsRegistry = stylesOptions.sheetsRegistry;
if (sheetManager.refs === 0) {
let staticSheet;
if (stylesOptions.sheetsCache) {
staticSheet = multiKeyStore.get(stylesOptions.sheetsCache, stylesCreator, theme);
}
const styles = stylesCreator.create(theme, name);
if (!staticSheet) {
staticSheet = stylesOptions.jss.createStyleSheet(styles, {
link: false,
...options,
});
staticSheet.attach();
if (stylesOptions.sheetsCache) {
multiKeyStore.set(stylesOptions.sheetsCache, stylesCreator, theme, staticSheet);
}
}
if (sheetsRegistry) {
sheetsRegistry.add(staticSheet);
}
sheetManager.staticSheet = staticSheet;
sheetManager.dynamicStyles = getDynamicStyles(styles);
}
if (sheetManager.dynamicStyles) {
const dynamicSheet = stylesOptions.jss.createStyleSheet(sheetManager.dynamicStyles, {
link: true,
...options,
});
dynamicSheet.update(props);
dynamicSheet.attach();
state.dynamicSheet = dynamicSheet;
state.classes = mergeClasses({
baseClasses: sheetManager.staticSheet.classes,
newClasses: dynamicSheet.classes,
});
if (sheetsRegistry) {
sheetsRegistry.add(dynamicSheet);
}
} else {
state.classes = sheetManager.staticSheet.classes;
}
sheetManager.refs += 1;
}
function update({ state }, props) {
if (state.dynamicSheet) {
state.dynamicSheet.update(props);
}
}
function detach({ state, theme, stylesOptions, stylesCreator }) {
if (stylesOptions.disableGeneration) {
return;
}
const sheetManager = multiKeyStore.get(stylesOptions.sheetsManager, stylesCreator, theme);
sheetManager.refs -= 1;
const sheetsRegistry = stylesOptions.sheetsRegistry;
if (sheetManager.refs === 0) {
multiKeyStore.delete(stylesOptions.sheetsManager, stylesCreator, theme);
stylesOptions.jss.removeStyleSheet(sheetManager.staticSheet);
if (sheetsRegistry) {
sheetsRegistry.remove(sheetManager.staticSheet);
}
}
if (state.dynamicSheet) {
stylesOptions.jss.removeStyleSheet(state.dynamicSheet);
if (sheetsRegistry) {
sheetsRegistry.remove(state.dynamicSheet);
}
}
}
function useSynchronousEffect(func, values) {
const key = React.useRef([]);
let output;
// Store "generation" key. Just returns a new object every time
const currentKey = React.useMemo(() => ({}), values); // eslint-disable-line react-hooks/exhaustive-deps
// "the first render", or "memo dropped the value"
if (key.current !== currentKey) {
key.current = currentKey;
output = func();
}
React.useEffect(
() => () => {
if (output) {
output();
}
},
[currentKey], // eslint-disable-line react-hooks/exhaustive-deps
);
}
function makeStyles(stylesOrCreator, options = {}) {
const {
// alias for classNamePrefix, if provided will listen to theme (required for theme.overrides)
name,
// Help with debuggability.
classNamePrefix: classNamePrefixOption,
Component,
defaultTheme = noopTheme,
...stylesOptions2
} = options;
const stylesCreator = getStylesCreator(stylesOrCreator);
const classNamePrefix = name || classNamePrefixOption || 'makeStyles';
stylesCreator.options = {
index: increment(),
name,
meta: classNamePrefix,
classNamePrefix,
};
return (props = {}) => {
const theme = useTheme() || defaultTheme;
const stylesOptions = {
...React.useContext(StylesContext),
...stylesOptions2,
};
const instance = React.useRef();
const shouldUpdate = React.useRef();
useSynchronousEffect(() => {
const current = {
name,
state: {},
stylesCreator,
stylesOptions,
theme,
};
attach(current, props);
shouldUpdate.current = false;
instance.current = current;
return () => {
detach(current);
};
}, [theme, stylesCreator]);
React.useEffect(() => {
if (shouldUpdate.current) {
update(instance.current, props);
}
shouldUpdate.current = true;
});
return getClasses(instance.current, props.classes, Component);
};
}
export default makeStyles;