-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathDefaultTheme.js
316 lines (286 loc) · 7.96 KB
/
DefaultTheme.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import * as React from 'react';
import PropTypes from 'prop-types';
import Box from '@mui/material/Box';
import ExpandIcon from '@mui/icons-material/ExpandMore';
import CollapseIcon from '@mui/icons-material/ChevronRight';
import TreeView from '@mui/lab/TreeView';
import MuiTreeItem, { treeItemClasses } from '@mui/lab/TreeItem';
import clsx from 'clsx';
import { styled, createTheme, lighten } from '@mui/material/styles';
import FormControlLabel from '@mui/material/FormControlLabel';
import Switch from '@mui/material/Switch';
import { useTranslate } from 'docs/src/modules/utils/i18n';
/**
* @param {unknown} value
*/
function getType(value) {
if (Array.isArray(value)) {
return 'array';
}
if (/^(#|rgb|rgba|hsl|hsla)/.test(value)) {
return 'color';
}
if (value === null) {
return 'null';
}
return typeof value;
}
/**
* @param {unknown} value
* @param {ReturnType<typeof getType>} type
*/
function getLabel(value, type) {
switch (type) {
case 'array':
return `Array(${value.length})`;
case 'null':
return 'null';
case 'undefined':
return 'undefined';
case 'function':
return `f ${value.name}()`;
case 'object':
return 'Object';
case 'string':
return `"${value}"`;
case 'symbol':
return `Symbol(${String(value)})`;
case 'bigint':
case 'boolean':
case 'number':
default:
return String(value);
}
}
function getTokenType(type) {
switch (type) {
case 'color':
return 'string';
case 'object':
case 'array':
return 'comment';
default:
return type;
}
}
const Color = styled('span')(({ theme }) => ({
backgroundColor: '#fff',
display: 'inline-block',
marginBottom: -1,
marginRight: theme.spacing(0.5),
border: '1px solid',
backgroundImage:
'url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%202%202%22%3E%3Cpath%20d%3D%22M1%202V0h1v1H0v1z%22%20fill-opacity%3D%22.2%22%2F%3E%3C%2Fsvg%3E")',
}));
function ObjectEntryLabel(props) {
const { objectKey, objectValue } = props;
const type = getType(objectValue);
const label = getLabel(objectValue, type);
const tokenType = getTokenType(type);
return (
<React.Fragment>
{`${objectKey}: `}
{type === 'color' ? (
<Color style={{ borderColor: lighten(label, 0.7) }}>
<Box
component="span"
sx={{ display: 'block', width: 11, height: 11 }}
style={{ backgroundColor: label }}
/>
</Color>
) : null}
<span className={clsx('token', tokenType)}>{label}</span>
</React.Fragment>
);
}
ObjectEntryLabel.propTypes = {
objectKey: PropTypes.any,
objectValue: PropTypes.any,
};
const TreeItem = styled(MuiTreeItem)({
[`&:focus > .${treeItemClasses.content}`]: {
backgroundColor: lighten('#333', 0.08),
outline: `2px dashed ${lighten('#333', 0.3)}`,
},
[`& .${treeItemClasses.content}`]: {
'&:hover': {
backgroundColor: lighten('#333', 0.08),
},
},
});
function ObjectEntry(props) {
const { nodeId, objectKey, objectValue } = props;
const keyPrefix = nodeId;
let children = null;
if (
(objectValue !== null && typeof objectValue === 'object') ||
typeof objectValue === 'function'
) {
children =
Object.keys(objectValue).length === 0
? undefined
: Object.keys(objectValue).map((key) => {
return (
<ObjectEntry
key={key}
nodeId={`${keyPrefix}.${key}`}
objectKey={key}
objectValue={objectValue[key]}
/>
);
});
}
return (
<TreeItem
nodeId={nodeId}
label={<ObjectEntryLabel objectKey={objectKey} objectValue={objectValue} />}
>
{children}
</TreeItem>
);
}
ObjectEntry.propTypes = {
nodeId: PropTypes.string.isRequired,
objectKey: PropTypes.any.isRequired,
objectValue: PropTypes.any,
};
function Inspector(props) {
const { data, expandPaths, ...other } = props;
const keyPrefix = '$ROOT';
const defaultExpanded = React.useMemo(() => {
return Array.isArray(expandPaths)
? expandPaths.map((expandPath) => `${keyPrefix}.${expandPath}`)
: [];
}, [keyPrefix, expandPaths]);
// for default* to take effect we need to remount
const key = React.useMemo(() => defaultExpanded.join(''), [defaultExpanded]);
return (
<TreeView
sx={{ bgcolor: '#333', color: '#fff', borderRadius: 1, p: 1 }}
key={key}
defaultCollapseIcon={<ExpandIcon />}
defaultEndIcon={<div style={{ width: 24 }} />}
defaultExpanded={defaultExpanded}
defaultExpandIcon={<CollapseIcon />}
{...other}
>
{Object.keys(data).map((objectKey) => {
return (
<ObjectEntry
key={objectKey}
nodeId={`${keyPrefix}.${objectKey}`}
objectKey={objectKey}
objectValue={data[objectKey]}
/>
);
})}
</TreeView>
);
}
Inspector.propTypes = {
data: PropTypes.any,
expandPaths: PropTypes.arrayOf(PropTypes.string),
};
function computeNodeIds(object, prefix) {
if (
(object !== null && typeof object === 'object') ||
typeof object === 'function'
) {
const ids = [];
Object.keys(object).forEach((key) => {
ids.push(
`${prefix}${key}`,
...computeNodeIds(object[key], `${prefix}${key}.`),
);
});
return ids;
}
return [];
}
function useNodeIdsLazy(object) {
const [allNodeIds, setAllNodeIds] = React.useState([]);
// technically we want to compute them lazily until we need them (expand all)
// yielding is good enough. technically we want to schedule the computation
// with low pri and upgrade the priority later
React.useEffect(() => {
setAllNodeIds(computeNodeIds(object, ''));
}, [object]);
return allNodeIds;
}
function DefaultTheme() {
const [checked, setChecked] = React.useState(false);
const [expandPaths, setExpandPaths] = React.useState(null);
const t = useTranslate();
const [darkTheme, setDarkTheme] = React.useState(false);
React.useEffect(() => {
let expandPath;
decodeURI(document.location.search.slice(1))
.split('&')
.forEach((param) => {
const [name, value] = param.split('=');
if (name === 'expand-path') {
expandPath = value;
} else if (name === 'expend-path' && !expandPath) {
// 'expend-path' is for backwards compatibility of any external links with a prior typo.
expandPath = value;
}
});
if (!expandPath) {
return;
}
setExpandPaths(
expandPath
.replace('$.', '')
.split('.')
.reduce((acc, path) => {
const last = acc.length > 0 ? `${acc[acc.length - 1]}.` : '';
acc.push(last + path);
return acc;
}, []),
);
}, []);
const data = React.useMemo(() => {
return createTheme({
palette: { mode: darkTheme ? 'dark' : 'light' },
});
}, [darkTheme]);
const allNodeIds = useNodeIdsLazy(data);
React.useDebugValue(allNodeIds);
React.useEffect(() => {
if (checked) {
// in case during the event handler allNodeIds wasn't computed yet
setExpandPaths(allNodeIds);
}
}, [checked, allNodeIds]);
return (
<Box sx={{ width: '100%' }}>
<FormControlLabel
sx={{ pb: 1 }}
control={
<Switch
checked={checked}
onChange={(event) => {
setChecked(event.target.checked);
setExpandPaths(event.target.checked ? allNodeIds : []);
}}
/>
}
label={t('expandAll')}
/>
<FormControlLabel
sx={{ pb: 1 }}
control={
<Switch
checked={darkTheme}
onChange={(event) => {
setDarkTheme(event.target.checked);
}}
/>
}
label={t('useDarkTheme')}
/>
<Inspector data={data} expandPaths={expandPaths} />
</Box>
);
}
export default DefaultTheme;