-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDemoLiveContent.tsx
More file actions
90 lines (81 loc) · 2.79 KB
/
DemoLiveContent.tsx
File metadata and controls
90 lines (81 loc) · 2.79 KB
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
'use client';
import * as React from 'react';
import { useEditable } from 'use-editable';
import type { ContentProps } from '@mui/internal-docs-infra/CodeHighlighter/types';
import { useDemo } from '@mui/internal-docs-infra/useDemo';
import { LabeledSwitch } from '@/components/LabeledSwitch';
import { Tabs } from '@/components/Tabs';
import { Select } from '@/components/Select';
import styles from './DemoLiveContent.module.css';
import '../../../code-highlighter/demos/syntax.css';
const variantNames: Record<string, string | undefined> = {
CssModules: 'CSS Modules',
};
export function DemoLiveContent(props: ContentProps<object>) {
// @focus-start @padding 1
const preRef = React.useRef<HTMLPreElement | null>(null);
const demo = useDemo(props, { preClassName: styles.codeBlock, preRef });
const hasJsTransform = demo.availableTransforms.includes('js');
const isJsSelected = demo.selectedTransform === 'js';
const labels = { false: 'TS', true: 'JS' };
const toggleJs = React.useCallback(
(checked: boolean) => {
demo.selectTransform(checked ? 'js' : null);
},
[demo],
);
const tabs = React.useMemo(
() => demo.files.map(({ name }) => ({ id: name, name })),
[demo.files],
);
const variants = React.useMemo(
() =>
demo.variants.map((variant) => ({ value: variant, label: variantNames[variant] || variant })),
[demo.variants],
);
const onChange = React.useCallback(
(text: string) => {
demo.setSource?.(text);
},
[demo],
);
useEditable(preRef, onChange, { indentation: 2, disabled: !demo.setSource });
return (
<div className={styles.container}>
<div className={styles.demoSection}>{demo.component}</div>
<div className={styles.codeSection}>
<div className={styles.header}>
<div className={styles.headerContainer}>
<div className={styles.tabContainer}>
<Tabs
tabs={tabs}
selectedTabId={demo.selectedFileName}
onTabSelect={demo.selectFileName}
/>
</div>
<div className={styles.headerActions}>
{demo.variants.length > 1 && (
<Select
items={variants}
value={demo.selectedVariant}
onValueChange={demo.selectVariant}
/>
)}
{hasJsTransform && (
<div className={styles.switchContainer}>
<LabeledSwitch
checked={isJsSelected}
onCheckedChange={toggleJs}
labels={labels}
/>
</div>
)}
</div>
</div>
</div>
<div className={styles.code}>{demo.selectedFile}</div>
</div>
</div>
);
// @focus-end
}