-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Demo.js
405 lines (374 loc) · 15.6 KB
/
Demo.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import * as React from 'react';
import PropTypes from 'prop-types';
import { useRouter } from 'next/router';
import { alpha, styled } from '@mui/material/styles';
import { styled as joyStyled } from '@mui/joy/styles';
import IconButton from '@mui/material/IconButton';
import Collapse from '@mui/material/Collapse';
import NoSsr from '@mui/material/NoSsr';
import HighlightedCode from 'docs/src/modules/components/HighlightedCode';
import DemoSandboxed from 'docs/src/modules/components/DemoSandboxed';
import { AdCarbonInline } from 'docs/src/modules/components/AdCarbon';
import { useCodeVariant } from 'docs/src/modules/utils/codeVariant';
import { CODE_VARIANTS } from 'docs/src/modules/constants';
import { useUserLanguage, useTranslate } from 'docs/src/modules/utils/i18n';
import BrandingProvider from 'docs/src/BrandingProvider';
const DemoToolbar = React.lazy(() => import('./DemoToolbar'));
// Sync with styles from DemoToolbar
// Importing the styles results in no bundle size reduction
const DemoToolbarFallbackRoot = styled('div')(({ theme }) => {
return {
display: 'none',
[theme.breakpoints.up('sm')]: {
display: 'flex',
height: theme.spacing(8),
},
};
});
export function DemoToolbarFallback() {
const t = useTranslate();
return <DemoToolbarFallbackRoot aria-busy aria-label={t('demoToolbarLabel')} role="toolbar" />;
}
function getDemoName(location) {
return location.replace(/(.+?)(\w+)\.\w+$$/, '$2');
}
function useDemoData(codeVariant, demo, githubLocation) {
const userLanguage = useUserLanguage();
const router = useRouter();
const asPathWithoutLang = router.asPath.replace(/^\/[a-zA-Z]{2}\//, '/');
let product;
let name = 'Material UI';
if (asPathWithoutLang.startsWith('/joy-ui/')) {
product = 'joy-ui';
name = 'Joy UI';
}
if (asPathWithoutLang.startsWith('/base/')) {
product = 'base';
name = 'MUI Base';
}
if (asPathWithoutLang.startsWith('/x/')) {
name = 'MUI X';
}
const title = `${getDemoName(githubLocation)} demo — ${name}`;
if (codeVariant === CODE_VARIANTS.TS && demo.rawTS) {
return {
codeVariant: CODE_VARIANTS.TS,
githubLocation: githubLocation.replace(/\.js$/, '.tsx'),
language: userLanguage,
raw: demo.rawTS,
Component: demo.tsx,
sourceLanguage: 'tsx',
title,
product,
};
}
return {
codeVariant: CODE_VARIANTS.JS,
githubLocation,
language: userLanguage,
raw: demo.raw,
Component: demo.js,
sourceLanguage: 'jsx',
title,
product,
};
}
// TODO: replace with React.useOpaqueReference if it is released
function useUniqueId(prefix) {
// useOpaqueReference
const [id, setId] = React.useState();
React.useEffect(() => {
setId(Math.random().toString(36).slice(2));
}, []);
return id ? `${prefix}${id}` : id;
}
const Root = styled('div')(({ theme }) => ({
marginBottom: 24,
marginLeft: theme.spacing(-2),
marginRight: theme.spacing(-2),
[theme.breakpoints.up('sm')]: {
marginLeft: 0,
marginRight: 0,
},
}));
const DemoRootMaterial = styled('div', {
shouldForwardProp: (prop) => prop !== 'hiddenToolbar' && prop !== 'bg',
})(({ theme, hiddenToolbar, bg }) => ({
position: 'relative',
outline: 0,
margin: 'auto',
display: 'flex',
justifyContent: 'center',
[theme.breakpoints.up('sm')]: {
borderRadius: 10,
...(bg === 'outlined' && {
borderLeftWidth: 1,
borderRightWidth: 1,
}),
/* Make no difference between the demo and the markdown. */
...(bg === 'inline' && {
padding: theme.spacing(0),
}),
...(hiddenToolbar && {
paddingTop: theme.spacing(1),
}),
},
/* Isolate the demo with an outline. */
...(bg === 'outlined' && {
padding: theme.spacing(3),
backgroundColor: theme.palette.background.paper,
border: `1px solid ${theme.palette.divider}`,
borderLeftWidth: 0,
borderRightWidth: 0,
}),
/* Prepare the background to display an inner elevation. */
...(bg === true && {
padding: theme.spacing(3),
backgroundColor:
theme.palette.mode === 'dark' ? theme.palette.grey[900] : theme.palette.grey[100],
}),
/* Mostly meant for introduction demos. */
...(bg === 'gradient' && {
padding: theme.spacing(20, 8),
border: `1px solid ${
theme.palette.mode === 'dark'
? alpha(theme.palette.primaryDark[500], 0.7)
: alpha(theme.palette.primary[100], 0.5)
}`,
overflow: 'hidden',
backgroundColor:
theme.palette.mode === 'dark'
? theme.palette.primaryDark[800]
: alpha(theme.palette.primary[50], 0.5),
backgroundClip: 'padding-box',
backgroundImage:
theme.palette.mode === 'dark'
? `radial-gradient(at 51% 52%, ${alpha(
theme.palette.primaryDark[700],
0.5,
)} 0px, transparent 50%),
radial-gradient(at 80% 0%, ${theme.palette.primaryDark[700]} 0px, transparent 50%),
radial-gradient(at 0% 95%, ${theme.palette.primaryDark[600]} 0px, transparent 50%),
radial-gradient(at 0% 20%, ${theme.palette.primaryDark[600]} 0px, transparent 35%),
radial-gradient(at 93% 85%, ${alpha(
theme.palette.primaryDark[500],
0.8,
)} 0px, transparent 50%),
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100'%3E%3Cg fill-rule='evenodd'%3E%3Cg fill='%23003A75' fill-opacity='0.15'%3E%3Cpath opacity='.5' d='M96 95h4v1h-4v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h9V0h1v15h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9h4v1h-4v9zm-1 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9z'/%3E%3Cpath d='M6 5V0H5v5H0v1h5v94h1V6h94V5H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");`
: `radial-gradient(at 51% 52%, ${alpha(
theme.palette.primary[50],
0.5,
)} 0px, transparent 50%),
radial-gradient(at 80% 0%, #FFFFFF 0px, transparent 20%),
radial-gradient(at 0% 95%, ${alpha(theme.palette.primary[100], 0.3)}, transparent 40%),
radial-gradient(at 0% 20%, ${theme.palette.primary[50]} 0px, transparent 50%),
radial-gradient(at 93% 85%, ${alpha(
theme.palette.primary[100],
0.2,
)} 0px, transparent 50%),
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100'%3E%3Cg fill-rule='evenodd'%3E%3Cg fill='%23003A75' fill-opacity='0.03'%3E%3Cpath opacity='.5' d='M96 95h4v1h-4v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h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m9-10v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-10 0v-9h-9v9h9zm-9-10h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9z'/%3E%3Cpath d='M6 5V0H5v5H0v1h5v94h1V6h94V5H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");`,
}),
...(hiddenToolbar && {
paddingTop: theme.spacing(2),
}),
}));
const DemoRootJoy = joyStyled('div', {
shouldForwardProp: (prop) => prop !== 'hiddenToolbar' && prop !== 'bg',
})(({ theme, hiddenToolbar, bg }) => ({
position: 'relative',
outline: 0,
margin: 'auto',
display: 'flex',
justifyContent: 'center',
[theme.breakpoints.up('sm')]: {
borderRadius: 10,
...(bg === 'outlined' && {
borderLeftWidth: 1,
borderRightWidth: 1,
}),
/* Make no difference between the demo and the markdown. */
...(bg === 'inline' && {
padding: theme.spacing(0),
}),
...(hiddenToolbar && {
paddingTop: theme.spacing(1),
}),
},
/* Isolate the demo with an outline. */
...(bg === 'outlined' && {
padding: theme.spacing(3),
backgroundColor: theme.vars.palette.background.surface,
border: `1px solid`,
borderColor: theme.vars.palette.divider,
borderLeftWidth: 0,
borderRightWidth: 0,
}),
/* Prepare the background to display an inner elevation. */
...(bg === true && {
padding: theme.spacing(3),
backgroundColor: theme.vars.palette.background.level2,
}),
...(hiddenToolbar && {
paddingTop: theme.spacing(3),
}),
}));
const Code = styled(HighlightedCode)(({ theme }) => ({
padding: 0,
marginBottom: theme.spacing(1),
marginTop: theme.spacing(2),
[theme.breakpoints.up('sm')]: {
marginTop: theme.spacing(0),
},
'& pre': {
margin: '0 auto',
maxHeight: 'min(68vh, 1000px)',
},
}));
const AnchorLink = styled('div')({
marginTop: -64, // height of toolbar
position: 'absolute',
});
const InitialFocus = styled(IconButton)(({ theme }) => ({
position: 'absolute',
top: 0,
left: 0,
width: theme.spacing(4),
height: theme.spacing(4),
pointerEvents: 'none',
}));
export default function Demo(props) {
const router = useRouter();
const asPathWithoutLang = router.asPath.replace(/^\/[a-zA-Z]{2}\//, '/');
const { demo, demoOptions, disableAd, githubLocation, mode } = props;
if (process.env.NODE_ENV !== 'production') {
if (demoOptions.hideToolbar === false) {
throw new Error(
[
'"hiddenToolbar": false is already the default.',
`Please remove the property in {{"demo": "${demoOptions.demo}", …}}.`,
].join('\n'),
);
}
}
const t = useTranslate();
const codeVariant = useCodeVariant();
const demoData = useDemoData(codeVariant, demo, githubLocation);
const [demoHovered, setDemoHovered] = React.useState(false);
const handleDemoHover = (event) => {
setDemoHovered(event.type === 'mouseenter');
};
const DemoComponent = demoData.Component;
const demoName = getDemoName(demoData.githubLocation);
const demoSandboxedStyle = React.useMemo(
() => ({
maxWidth: demoOptions.maxWidth,
height: demoOptions.height,
}),
[demoOptions.height, demoOptions.maxWidth],
);
if (demoOptions.bg == null) {
demoOptions.bg = 'outlined';
}
if (demoOptions.iframe) {
demoOptions.bg = true;
}
const [codeOpen, setCodeOpen] = React.useState(demoOptions.defaultCodeOpen || false);
const shownOnce = React.useRef(false);
if (codeOpen) {
shownOnce.current = true;
}
React.useEffect(() => {
const navigatedDemoName = getDemoName(window.location.hash);
if (demoName === navigatedDemoName) {
setCodeOpen(true);
}
}, [demoName]);
const showPreview =
!demoOptions.hideToolbar && demoOptions.defaultCodeOpen !== false && Boolean(demo.jsxPreview);
const [demoKey, resetDemo] = React.useReducer((key) => key + 1, 0);
const demoId = useUniqueId('demo-');
const demoSourceId = useUniqueId(`demoSource-`);
const openDemoSource = codeOpen || showPreview;
const initialFocusRef = React.useRef(null);
const [showAd, setShowAd] = React.useState(false);
const isJoy = asPathWithoutLang.startsWith('/joy-ui');
const DemoRoot = asPathWithoutLang.startsWith('/joy-ui') ? DemoRootJoy : DemoRootMaterial;
const Wrapper = asPathWithoutLang.startsWith('/joy-ui') ? BrandingProvider : React.Fragment;
return (
<Root>
<AnchorLink id={`${demoName}`} />
<DemoRoot
hiddenToolbar={demoOptions.hideToolbar}
bg={demoOptions.bg}
id={demoId}
onMouseEnter={handleDemoHover}
onMouseLeave={handleDemoHover}
>
<Wrapper {...(isJoy && { mode })}>
<InitialFocus
aria-label={t('initialFocusLabel')}
action={initialFocusRef}
tabIndex={-1}
/>
</Wrapper>
<DemoSandboxed
key={demoKey}
style={demoSandboxedStyle}
component={DemoComponent}
iframe={demoOptions.iframe}
name={demoName}
onResetDemoClick={resetDemo}
/>
</DemoRoot>
<AnchorLink id={`${demoName}.js`} />
<AnchorLink id={`${demoName}.tsx`} />
<Wrapper {...(isJoy ? { mode } : {})}>
{demoOptions.hideToolbar ? null : (
<NoSsr defer fallback={<DemoToolbarFallback />}>
<React.Suspense fallback={<DemoToolbarFallback />}>
<DemoToolbar
codeOpen={codeOpen}
codeVariant={codeVariant}
demo={demo}
demoData={demoData}
demoHovered={demoHovered}
demoId={demoId}
demoName={demoName}
demoOptions={demoOptions}
demoSourceId={demoSourceId}
initialFocusRef={initialFocusRef}
onCodeOpenChange={() => {
setCodeOpen((open) => !open);
setShowAd(true);
}}
onResetDemoClick={resetDemo}
openDemoSource={openDemoSource}
showPreview={showPreview}
/>
</React.Suspense>
</NoSsr>
)}
<Collapse in={openDemoSource} unmountOnExit>
<Code
id={demoSourceId}
code={showPreview && !codeOpen ? demo.jsxPreview : demoData.raw}
language={demoData.sourceLanguage}
copyButtonProps={{
'data-ga-event-category': codeOpen ? 'demo-expand' : 'demo',
'data-ga-event-label': demoOptions.demo,
'data-ga-event-action': 'copy-click',
}}
/>
</Collapse>
{showAd && !disableAd && !demoOptions.disableAd ? <AdCarbonInline /> : null}
</Wrapper>
</Root>
);
}
Demo.propTypes = {
demo: PropTypes.object.isRequired,
demoOptions: PropTypes.object.isRequired,
disableAd: PropTypes.bool.isRequired,
githubLocation: PropTypes.string.isRequired,
mode: PropTypes.string, // temporary, just to make Joy docs work.
};