forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoEditor.tsx
169 lines (161 loc) · 5.35 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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, alpha, useTheme } from '@mui/material/styles';
import prism from '@mui/internal-markdown/prism';
import MarkdownElement from 'docs/src/modules/components/MarkdownElement';
import CodeCopyButton from 'docs/src/modules/components/CodeCopyButton';
import { useTranslate } from '@mui/docs/i18n';
import { useCodeCopy } from 'docs/src/modules/utils/CodeCopy';
import { blueDark } from '@mui/docs/branding';
const StyledMarkdownElement = styled(MarkdownElement)(({ theme }) => [
{
'& .scrollContainer': {
maxHeight: 'min(68vh, 1000px)',
overflow: 'auto',
marginTop: -1,
backgroundColor: 'hsl(210, 35%, 9%)', // a special, one-off, color tailored for the code blocks using MUI's branding theme blue palette as the starting point. It has a less saturaded color but still maintaining a bit of the blue tint.
border: `1px solid ${(theme.vars || theme).palette.divider}`,
colorScheme: 'dark',
'&:hover': {
boxShadow: `0 0 0 3px ${alpha((theme.vars || theme).palette.primary[500], 0.5)}`,
},
'&:focus-within': {
boxShadow: `0 0 0 2px ${alpha((theme.vars || theme).palette.primary[500], 0.5)}`,
},
[theme.breakpoints.up('sm')]: {
borderRadius: '0 0 12px 12px',
},
},
'& 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',
},
'& .MuiCode-copy': {
display: 'none',
},
},
theme.applyDarkStyles({
'& .scrollContainer': {
borderColor: (theme.vars || theme).palette.divider,
},
}),
]) 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: 0,
},
'& > 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);
const enterRef = React.useRef<HTMLElement>(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">
<NoSsr>
<CodeCopyButton {...copyButtonProps} code={value} />
</NoSsr>
<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.2, 1, 0.5, 1),
outline: 0,
left: '50%',
border: '1px solid',
borderColor: blueDark[600],
backgroundColor: blueDark[700],
color: '#FFF',
transform: 'translateX(-50%)',
borderRadius: '6px',
fontSize: theme.typography.pxToRem(13),
transition: 'all 0.3s',
boxShadow: '0 2px 4px rgba(0,0,0,0.5)',
'&:not(:focus)': {
top: 0,
opacity: 0,
pointerEvents: 'none',
},
'> kbd': {
padding: theme.spacing(0.2, 0.4),
backgroundColor: blueDark[500],
fontSize: theme.typography.pxToRem(11),
borderRadius: '6px',
border: '1px solid',
borderColor: blueDark[400],
},
})}
dangerouslySetInnerHTML={{
__html: t('editorHint'),
}}
/>
{children}
</div>
</StyledMarkdownElement>
);
}