-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCodeEditorContent.tsx
More file actions
52 lines (45 loc) · 1.61 KB
/
CodeEditorContent.tsx
File metadata and controls
52 lines (45 loc) · 1.61 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
'use client';
import * as React from 'react';
import { useEditable } from 'use-editable';
import type { ContentProps } from '@mui/internal-docs-infra/CodeHighlighter/types';
import { useCode } from '@mui/internal-docs-infra/useCode';
import { LabeledSwitch } from '@/components/LabeledSwitch';
import styles from './CodeEditorContent.module.css';
import '../../../code-highlighter/demos/syntax.css';
export function CodeEditorContent(props: ContentProps<object>) {
// @focus-start @padding 1
const preRef = React.useRef<HTMLPreElement | null>(null);
const code = useCode(props, { preClassName: styles.codeBlock, preRef });
const hasJsTransform = code.availableTransforms.includes('js');
const isJsSelected = code.selectedTransform === 'js';
const labels = { false: 'TS', true: 'JS' };
const toggleJs = React.useCallback(
(checked: boolean) => {
code.selectTransform(checked ? 'js' : null);
},
[code],
);
const onInput = React.useCallback(
(text: string) => {
code.setSource?.(text);
},
[code],
);
useEditable(preRef, onInput, { indentation: 2 });
return (
<div className={styles.container}>
<div className={styles.header}>
<span className={styles.name}>{code.selectedFileName}</span>
<div className={styles.headerActions}>
{hasJsTransform && (
<div className={styles.switchContainer}>
<LabeledSwitch checked={isJsSelected} onCheckedChange={toggleJs} labels={labels} />
</div>
)}
</div>
</div>
<div className={styles.code}>{code.selectedFile}</div>
</div>
);
// @focus-end
}