/
Prism.tsx
222 lines (200 loc) · 6.93 KB
/
Prism.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { forwardRef } from 'react';
import Highlight, { defaultProps, Language, PrismTheme } from 'prism-react-renderer';
import {
ActionIcon,
useMantineTheme,
Tooltip,
DefaultProps,
Selectors,
Box,
ScrollArea,
useComponentDefaultProps,
MantineColor,
MantineNumberSize,
MantineTheme,
} from '@mantine/core';
import { useClipboard } from '@mantine/hooks';
import { CopyIcon } from './CopyIcon';
import { getPrismTheme as defaultGetPrismTheme } from './prism-theme';
import useStyles from './Prism.styles';
export type PrismStylesNames = Selectors<typeof useStyles>;
export interface PrismProps
extends DefaultProps<PrismStylesNames>,
Omit<React.ComponentPropsWithRef<'div'>, 'children'> {
/** Code which will be highlighted */
children: string;
/** Programming language that should be highlighted */
language: Language;
/** True to remove copy to clipboard button */
noCopy?: boolean;
/** Copy button tooltip */
copyLabel?: string;
/** Copy button tooltip in copied state */
copiedLabel?: string;
/** Display line numbers */
withLineNumbers?: boolean;
/** Highlight line at given line number with color from theme.colors */
highlightLines?: Record<string, { color: MantineColor; label?: string }>;
/** Force color scheme, defaults to theme.colorScheme */
colorScheme?: 'dark' | 'light';
/** Change scroll area component */
scrollAreaComponent?: any;
/** Defines whether the code should be trimmed, defaults to true */
trim?: boolean;
/** Key of theme.radius or number to set border-radius in px */
radius?: MantineNumberSize;
/** Provide custom color scheme */
getPrismTheme?(theme: MantineTheme, colorScheme: 'light' | 'dark'): PrismTheme;
}
const prismDefaultProps: Partial<PrismProps> = {
noCopy: false,
copyLabel: 'Copy code',
copiedLabel: 'Copied',
withLineNumbers: false,
trim: true,
highlightLines: {},
scrollAreaComponent: ScrollArea,
getPrismTheme: defaultGetPrismTheme,
};
export const Prism = forwardRef<HTMLDivElement, PrismProps>((props: PrismProps, ref) => {
const {
className,
children,
language,
noCopy,
classNames,
styles,
copyLabel,
copiedLabel,
withLineNumbers,
highlightLines,
scrollAreaComponent: ScrollAreaComponent,
colorScheme,
trim,
unstyled,
radius,
getPrismTheme,
...others
} = useComponentDefaultProps('Prism', prismDefaultProps, props);
const code = trim && typeof children === 'string' ? children.trim() : children;
const maxLineSize = code.split('\n').length.toString().length;
const theme = useMantineTheme();
const clipboard = useClipboard();
const _colorScheme = colorScheme || theme.colorScheme;
const { classes, cx } = useStyles(
{
colorScheme: _colorScheme,
native: ScrollAreaComponent !== ScrollArea,
maxLineSize,
radius,
},
{ classNames, styles, unstyled, name: 'Prism' }
);
return (
<Box className={cx(classes.root, className)} ref={ref} {...others} translate="no">
{!noCopy && (
<Tooltip
label={clipboard.copied ? copiedLabel : copyLabel}
position="left"
withArrow
arrowSize={6}
offset={6}
color={clipboard.copied ? 'teal' : undefined}
unstyled={unstyled}
>
<ActionIcon
className={classes.copy}
aria-label={clipboard.copied ? copiedLabel : copyLabel}
onClick={() => clipboard.copy(code)}
unstyled={unstyled}
>
<CopyIcon copied={clipboard.copied} />
</ActionIcon>
</Tooltip>
)}
<Highlight
{...defaultProps}
theme={getPrismTheme(theme, _colorScheme)}
code={code}
language={language}
>
{({
className: inheritedClassName,
style: inheritedStyle,
tokens,
getLineProps,
getTokenProps,
}) => (
<ScrollAreaComponent className={classes.scrollArea} dir="ltr">
<pre className={cx(classes.code, inheritedClassName)} style={inheritedStyle} dir="ltr">
{tokens
.map((line, index) => {
if (
index === tokens.length - 1 &&
line.length === 1 &&
line[0].content === '\n'
) {
return null;
}
const lineNumber = index + 1;
const lineProps = getLineProps({ line, key: index });
const shouldHighlight = lineNumber in highlightLines;
const lineColor =
_colorScheme === 'dark'
? theme.fn.rgba(
theme.fn.themeColor(highlightLines[lineNumber]?.color, 9),
0.25
)
: theme.fn.themeColor(highlightLines[lineNumber]?.color, 0);
return (
<div
{...lineProps}
className={cx(classes.line, lineProps.className)}
style={{ ...(shouldHighlight ? { backgroundColor: lineColor } : null) }}
>
{withLineNumbers && (
<div
className={classes.lineNumber}
style={{
color: shouldHighlight
? theme.fn.themeColor(
highlightLines[lineNumber]?.color,
_colorScheme === 'dark' ? 5 : 8
)
: undefined,
}}
>
{highlightLines[lineNumber]?.label || lineNumber}
</div>
)}
<div className={classes.lineContent}>
{line.map((token, key) => {
const tokenProps = getTokenProps({ token, key });
return (
<span
{...tokenProps}
style={{
...tokenProps.style,
color: shouldHighlight
? theme.fn.themeColor(
highlightLines[lineNumber]?.color,
_colorScheme === 'dark' ? 5 : 8
)
: (tokenProps?.style?.color as string),
}}
/>
);
})}
</div>
</div>
);
})
.filter(Boolean)}
</pre>
</ScrollAreaComponent>
)}
</Highlight>
</Box>
);
});
Prism.displayName = '@mantine/prism/Prism';