forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Demo.js
539 lines (498 loc) · 27.2 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import * as React from 'react';
import PropTypes from 'prop-types';
import { useRouter } from 'next/router';
import { debounce } from '@mui/material/utils';
import { alpha, styled } from '@mui/material/styles';
import { styled as joyStyled } from '@mui/joy/styles';
import { unstable_useId as useId } from '@mui/utils';
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 DemoSandbox from 'docs/src/modules/components/DemoSandbox';
import ReactRunner from 'docs/src/modules/components/ReactRunner';
import DemoEditor from 'docs/src/modules/components/DemoEditor';
import DemoEditorError from 'docs/src/modules/components/DemoEditorError';
import { AdCarbonInline } from 'docs/src/modules/components/AdCarbon';
import { pathnameToLanguage } from 'docs/src/modules/utils/helpers';
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';
import { blue, blueDark, grey } from 'docs/src/modules/brandingTheme';
/**
* Removes leading spaces (indentation) present in the `.tsx` previews
* to be able to replace the existing code with the incoming dynamic code
* @param {string} input
*/
function trimLeadingSpaces(input = '') {
return input.replace(/^\s+/gm, '');
}
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 { canonicalAs } = pathnameToLanguage(router.asPath);
return React.useMemo(() => {
let product;
let name = 'Material UI';
if (canonicalAs.startsWith('/joy-ui/')) {
product = 'joy-ui';
name = 'Joy UI';
} else if (canonicalAs.startsWith('/base/')) {
product = 'base';
name = 'MUI Base';
} else if (canonicalAs.startsWith('/x/')) {
name = 'MUI X';
}
return {
scope: demo.scope,
jsxPreview: demo.jsxPreview,
...(codeVariant === CODE_VARIANTS.TS && demo.rawTS
? {
codeVariant: CODE_VARIANTS.TS,
githubLocation: githubLocation.replace(/\.js$/, '.tsx'),
raw: demo.rawTS,
Component: demo.tsx,
sourceLanguage: 'tsx',
}
: {
codeVariant: CODE_VARIANTS.JS,
githubLocation,
raw: demo.raw,
Component: demo.js,
sourceLanguage: 'jsx',
}),
title: `${getDemoName(githubLocation)} demo — ${name}`,
product,
language: userLanguage,
};
}, [canonicalAs, codeVariant, demo, githubLocation, userLanguage]);
}
function useDemoElement({ demoData, editorCode, setDebouncedError, liveDemoActive }) {
const debouncedSetError = React.useMemo(
() => debounce(setDebouncedError, 300),
[setDebouncedError],
);
React.useEffect(() => {
return () => {
debouncedSetError.clear();
};
}, [debouncedSetError]);
// Memoize to avoid rendering the demo more than it needs to be.
// For example, avoid a render when the demo is hovered.
const BundledComponent = React.useMemo(() => <demoData.Component />, [demoData]);
const LiveComponent = React.useMemo(
() => (
<ReactRunner
scope={demoData.scope}
onError={debouncedSetError}
code={
editorCode.isPreview
? trimLeadingSpaces(demoData.raw).replace(
trimLeadingSpaces(demoData.jsxPreview),
editorCode.value,
)
: editorCode.value
}
/>
),
[demoData, debouncedSetError, editorCode.isPreview, editorCode.value],
);
// No need for a live environment if the code matches with the component rendered server-side.
return editorCode.value === editorCode.initialEditorCode && liveDemoActive === false
? BundledComponent
: LiveComponent;
}
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[700]} 0px, transparent 50%),
radial-gradient(at 0% 5%, ${theme.palette.primaryDark[700]} 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-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4h-9v4h-1v-4H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15v-9H0v-1h15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v9h9zm-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),
}),
},
/* Isolate the demo with an outline. */
...(bg === 'outlined' && {
padding: theme.spacing(3),
backgroundColor:
theme.palette.mode === 'dark' ? alpha(blueDark[800], 0.2) : alpha(grey[50], 0.2),
backgroundImage:
theme.palette.mode === 'dark'
? `
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.10'%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-4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-9v9zm10 0h9v-9h-9v9zm10 0h9v-9h-9v9z'/%3E%3Cpath d='M6 5V0H5v5H0v1h5v94h1V6h94V5H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");`
: `
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-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");`,
border: `1px solid ${theme.palette.mode === 'dark' ? alpha(grey[700], 0.3) : grey[100]}`,
borderLeftWidth: 0,
borderRightWidth: 0,
}),
/* Prepare the background to display an inner elevation. */
...(bg === true && {
padding: theme.spacing(3),
backgroundColor: theme.vars.palette.background.level2,
}),
/* Mostly meant for introduction demos. */
...(bg === 'gradient' && {
padding: theme.spacing(0),
overflow: 'auto',
backgroundColor: theme.palette.mode === 'dark' ? blueDark[800] : alpha(blue[50], 0.5),
border: `1px solid ${
theme.palette.mode === 'dark' ? alpha(blueDark[500], 0.7) : alpha(blue[100], 0.5)
}`,
backgroundImage:
theme.palette.mode === 'dark'
? `radial-gradient(at 51% 52%, ${alpha(blueDark[700], 0.5)} 0px, transparent 50%),
radial-gradient(at 80% 0%, ${blueDark[700]} 0px, transparent 50%),
radial-gradient(at 0% 95%, ${blueDark[700]} 0px, transparent 50%),
radial-gradient(at 0% 5%, ${blueDark[700]} 0px, transparent 25%),
radial-gradient(at 93% 85%, ${alpha(blueDark[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(blue[50], 0.5)} 0px, transparent 50%),
radial-gradient(at 80% 0%, #FFFFFF 0px, transparent 20%),
radial-gradient(at 0% 95%, ${alpha(blue[100], 0.3)}, transparent 40%),
radial-gradient(at 0% 20%, ${blue[50]} 0px, transparent 50%),
radial-gradient(at 93% 85%, ${alpha(blue[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-1v-4h-9v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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(0),
}),
}));
const DemoCodeViewer = styled(HighlightedCode)(({ theme }) => ({
'& pre': {
margin: 0,
maxHeight: 'min(68vh, 1000px)',
maxWidth: 'initial',
borderRadius: 0,
[theme.breakpoints.up('sm')]: {
borderRadius: theme.shape.borderRadius,
},
},
}));
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 { 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 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(demoData.jsxPreview);
const [demoKey, setDemoKey] = React.useReducer((key) => key + 1, 0);
const demoId = `demo-${useId()}`;
const demoSourceId = `demoSource-${useId()}`;
const openDemoSource = codeOpen || showPreview;
const initialFocusRef = React.useRef(null);
const [showAd, setShowAd] = React.useState(false);
const adVisibility = showAd && !disableAd && !demoOptions.disableAd;
const DemoRoot = demoData.product === 'joy-ui' ? DemoRootJoy : DemoRootMaterial;
const Wrapper = demoData.product === 'joy-ui' ? BrandingProvider : React.Fragment;
const isPreview = !codeOpen && showPreview;
const initialEditorCode = isPreview
? demoData.jsxPreview
: // Prettier remove all the leading lines except for the last one, remove it as we don't
// need it in the live edit view.
demoData.raw.replace(/\n$/, '');
const [editorCode, setEditorCode] = React.useState({
value: initialEditorCode,
isPreview,
initialEditorCode,
});
const resetDemo = () => {
setEditorCode({
value: initialEditorCode,
isPreview,
initialEditorCode,
});
setDemoKey();
};
React.useEffect(() => {
setEditorCode({
value: initialEditorCode,
isPreview,
initialEditorCode,
});
}, [initialEditorCode, isPreview]);
const [debouncedError, setDebouncedError] = React.useState(null);
const [liveDemoActive, setLiveDemoActive] = React.useState(false);
const demoElement = useDemoElement({
demoData,
editorCode,
setDebouncedError,
liveDemoActive,
});
return (
<Root>
<AnchorLink id={`${demoName}`} />
<DemoRoot
hiddenToolbar={demoOptions.hideToolbar}
bg={demoOptions.bg}
id={demoId}
onMouseEnter={handleDemoHover}
onMouseLeave={handleDemoHover}
>
<Wrapper {...(demoData.product === 'joy-ui' && { mode })}>
<InitialFocus
aria-label={t('initialFocusLabel')}
action={initialFocusRef}
tabIndex={-1}
/>
</Wrapper>
<DemoSandbox
key={demoKey}
style={demoSandboxedStyle}
iframe={demoOptions.iframe}
name={demoName}
onResetDemoClick={resetDemo}
>
{demoElement}
</DemoSandbox>
</DemoRoot>
<AnchorLink id={`${demoName}.js`} />
<AnchorLink id={`${demoName}.tsx`} />
<Wrapper {...(demoData.product === 'joy-ui' ? { 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>
{/* A limitation from https://github.com/nihgwu/react-runner,
we can't inject the `window` of the iframe so we need a disableLiveEdit option. */}
{demoOptions.disableLiveEdit ? (
<DemoCodeViewer
code={editorCode.value}
id={demoSourceId}
language={demoData.sourceLanguage}
copyButtonProps={{
'data-ga-event-category': codeOpen ? 'demo-expand' : 'demo',
'data-ga-event-label': demoOptions.demo,
'data-ga-event-action': 'copy-click',
}}
/>
) : (
<DemoEditor
// Mount a new text editor when the preview mode change to reset the undo/redo history.
key={editorCode.isPreview}
value={editorCode.value}
onChange={(value) => {
setEditorCode({
...editorCode,
value,
});
}}
onFocus={() => {
setLiveDemoActive(true);
}}
id={demoSourceId}
language={demoData.sourceLanguage}
copyButtonProps={{
'data-ga-event-category': codeOpen ? 'demo-expand' : 'demo',
'data-ga-event-label': demoOptions.demo,
'data-ga-event-action': 'copy-click',
}}
>
<DemoEditorError>{debouncedError}</DemoEditorError>
</DemoEditor>
)}
</Collapse>
{adVisibility ? <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.
};