forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighlightedCode.tsx
More file actions
71 lines (66 loc) · 2.18 KB
/
HighlightedCode.tsx
File metadata and controls
71 lines (66 loc) · 2.18 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
import * as React from 'react';
import prism from '@mui/internal-markdown/prism';
import NoSsr from '@mui/material/NoSsr';
import { ButtonProps } from '@mui/material/Button';
import { SxProps, styled } from '@mui/material/styles';
import { useCodeCopy, CodeCopyButton } from '../CodeCopy';
import { MarkdownElement } from '../MarkdownElement';
const Pre = styled('pre')(({ theme }) => ({
margin: 0,
color: 'hsl(60deg 30% 96.08%)', // fallback color until Prism's theme is loaded
WebkitOverflowScrolling: 'touch', // iOS momentum scrolling.
'& code': {
// Avoid layout jump after hydration (style injected by Prism)
...theme.typography.caption,
fontFamily: theme.typography.fontFamilyCode,
fontWeight: 400,
WebkitFontSmoothing: 'subpixel-antialiased',
// Reset for Safari
// https://github.com/necolas/normalize.css/blob/master/normalize.css#L102
},
}));
export interface HighlightedCodeProps {
code: string;
copyButtonHidden?: boolean;
copyButtonProps?: ButtonProps;
language: string;
parentComponent?: React.ElementType;
plainStyle?: boolean;
preComponent?: React.ElementType;
sx?: SxProps;
}
export function HighlightedCode(props: HighlightedCodeProps) {
const {
code,
copyButtonHidden = false,
copyButtonProps,
language,
plainStyle,
parentComponent: Component = plainStyle ? React.Fragment : MarkdownElement,
preComponent: PreComponent = plainStyle ? Pre : 'pre',
...other
} = props;
const renderedCode = React.useMemo(() => {
return prism(code.trim(), language);
}, [code, language]);
const handlers = useCodeCopy();
const componentProps = !plainStyle ? other : undefined;
return (
<Component {...componentProps}>
<div className="MuiCode-root" {...handlers} style={{ height: '100%' }}>
{copyButtonHidden ? null : (
<NoSsr>
<CodeCopyButton code={code} {...copyButtonProps} />
</NoSsr>
)}
<PreComponent>
<code
className={`language-${language}`}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: renderedCode }}
/>
</PreComponent>
</div>
</Component>
);
}