forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DemoEditor.tsx
152 lines (144 loc) · 4.64 KB
/
DemoEditor.tsx
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
import * as React from 'react';
import SimpleCodeEditor from 'react-simple-code-editor';
import Box from '@mui/material/Box';
import NoSsr from '@mui/base/NoSsr';
import { styled, useTheme } from '@mui/material/styles';
import prism from '@mui/markdown/prism';
import MarkdownElement from 'docs/src/modules/components/MarkdownElement';
import CodeCopyButton from 'docs/src/modules/components/CodeCopyButton';
import { useTranslate } from 'docs/src/modules/utils/i18n';
import { useCodeCopy } from 'docs/src/modules/utils/CodeCopy';
import { blue, blueDark } from 'docs/src/modules/brandingTheme';
const StyledMarkdownElement = styled(MarkdownElement)(({ theme }) => ({
'& .scrollContainer': {
maxHeight: 'min(68vh, 1000px)',
overflow: 'auto',
backgroundColor: blueDark[800],
colorScheme: 'dark',
'&:hover': {
boxShadow: `0 0 0 3px ${
theme.palette.mode === 'dark' ? theme.palette.primaryDark[400] : theme.palette.primary.light
}`,
},
'&:focus-within': {
boxShadow: `0 0 0 2px ${
theme.palette.mode === 'dark' ? theme.palette.primaryDark.main : theme.palette.primary.main
}`,
},
[theme.breakpoints.up('sm')]: {
borderRadius: theme.shape.borderRadius,
},
},
'& pre': {
// The scroll container needs to be the parent of the editor, overriding:
// https://github.com/mui/material-ui/blob/269c1d0c7572fcb6ae3b270a2622d16c7e40c848/docs/src/modules/components/MarkdownElement.js#L27-L26
maxWidth: 'initial',
maxHeight: 'initial',
},
})) as any;
const StyledSimpleCodeEditor = styled(SimpleCodeEditor)(({ theme }) => ({
...theme.typography.body2,
fontSize: theme.typography.pxToRem(13),
fontFamily: theme.typography.fontFamilyCode,
fontWeight: 400,
WebkitFontSmoothing: 'subpixel-antialiased',
color: '#f8f8f2',
direction: 'ltr /*! @noflip */' as any,
float: 'left',
minWidth: '100%',
'& textarea': {
outline: 'none',
},
'& > textarea, & > pre': {
// Override inline-style
whiteSpace: 'pre !important',
},
}));
interface DemoEditorProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
copyButtonProps: {};
id: string;
language: string;
onChange: () => {};
value: string;
}
export default function DemoEditor(props: DemoEditorProps) {
const { language, value, onChange, copyButtonProps, children, id, ...other } = props;
const t = useTranslate();
const contextTheme = useTheme();
const wrapperRef = React.useRef<HTMLElement | null>(null);
const enterRef = React.useRef<HTMLElement | null>(null);
const handlers = useCodeCopy();
React.useEffect(() => {
wrapperRef.current!.querySelector('textarea')!.tabIndex = -1;
}, []);
return (
<StyledMarkdownElement
ref={wrapperRef}
onKeyDown={(event: React.KeyboardEvent) => {
if (event.key === 'Tab') {
return;
}
if (event.key === 'Escape') {
enterRef.current!.focus();
return;
}
if (event.key === 'Enter') {
const textarea = wrapperRef.current!.querySelector('textarea');
if (textarea !== document.activeElement) {
event.preventDefault();
event.stopPropagation();
textarea!.focus();
}
}
}}
{...other}
>
<div className="MuiCode-root" {...handlers}>
<div className="scrollContainer">
<StyledSimpleCodeEditor
padding={contextTheme.spacing(2)}
highlight={(code: any) =>
`<code class="language-${language}">${prism(code, language)}</code>`
}
id={id}
value={value}
onValueChange={onChange}
/>
</div>
<Box
ref={enterRef}
aria-live="polite"
tabIndex={0}
sx={(theme) => ({
position: 'absolute',
top: theme.spacing(1),
padding: theme.spacing(0.5, 1),
outline: 'none',
left: '50%',
border: '1px solid',
borderColor: blue[400],
backgroundColor: blueDark[600],
color: blueDark[50],
transform: 'translateX(-50%)',
borderRadius: '4px',
fontSize: theme.typography.pxToRem(13),
transition: 'all 0.3s',
'&:not(:focus)': {
top: 0,
opacity: 0,
pointerEvents: 'none',
},
})}
dangerouslySetInnerHTML={{
__html: t('editorHint'),
}}
/>
<NoSsr>
<CodeCopyButton {...copyButtonProps} code={value} />
</NoSsr>
{children}
</div>
</StyledMarkdownElement>
);
}