@mantine/code-highlight 7.3.0-alpha.0 → 7.3.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -6
- package/esm/index.css +0 -1
- package/src/CodeHighlight.module.css +0 -173
- package/src/CodeHighlight.story.tsx +0 -273
- package/src/CodeHighlight.test.tsx +0 -32
- package/src/CodeHighlight.theme.module.css +0 -95
- package/src/CodeHighlight.tsx +0 -122
- package/src/CodeHighlightTabs.test.tsx +0 -41
- package/src/CodeHighlightTabs.tsx +0 -264
- package/src/CopyIcon.tsx +0 -37
- package/src/ExpandIcon.tsx +0 -42
- package/src/FileIcon.tsx +0 -29
- package/src/InlineCodeHighlight.test.tsx +0 -25
- package/src/InlineCodeHighlight.tsx +0 -71
- package/src/index.ts +0 -20
- package/src/use-highlight.ts +0 -35
- package/tsconfig.build.json +0 -23
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -23
- /package/{cjs/index.css → styles.css} +0 -0
- /package/{esm/index.layer.css → styles.layer.css} +0 -0
package/src/CodeHighlight.tsx
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import cx from 'clsx';
|
|
3
|
-
import {
|
|
4
|
-
Box,
|
|
5
|
-
BoxProps,
|
|
6
|
-
StylesApiProps,
|
|
7
|
-
factory,
|
|
8
|
-
ElementProps,
|
|
9
|
-
useProps,
|
|
10
|
-
useStyles,
|
|
11
|
-
CopyButton,
|
|
12
|
-
Tooltip,
|
|
13
|
-
ActionIcon,
|
|
14
|
-
ScrollArea,
|
|
15
|
-
Factory,
|
|
16
|
-
} from '@mantine/core';
|
|
17
|
-
import { useHighlight } from './use-highlight';
|
|
18
|
-
import { CopyIcon } from './CopyIcon';
|
|
19
|
-
import _classes from './CodeHighlight.module.css';
|
|
20
|
-
import themeClasses from './CodeHighlight.theme.module.css';
|
|
21
|
-
|
|
22
|
-
const classes = { ..._classes, root: cx(_classes.root, themeClasses.theme) };
|
|
23
|
-
|
|
24
|
-
export type CodeHighlightStylesNames = 'root' | 'code' | 'pre' | 'copy';
|
|
25
|
-
|
|
26
|
-
export interface CodeHighlightProps
|
|
27
|
-
extends BoxProps,
|
|
28
|
-
StylesApiProps<CodeHighlightFactory>,
|
|
29
|
-
ElementProps<'div'> {
|
|
30
|
-
/** Code to highlight */
|
|
31
|
-
code: string;
|
|
32
|
-
|
|
33
|
-
/** Code language, `'tsx'` by default */
|
|
34
|
-
language?: string;
|
|
35
|
-
|
|
36
|
-
/** Determines whether copy button should be displayed, `true` by default */
|
|
37
|
-
withCopyButton?: boolean;
|
|
38
|
-
|
|
39
|
-
/** Copy tooltip label, `'Copy code'` by default */
|
|
40
|
-
copyLabel?: string;
|
|
41
|
-
|
|
42
|
-
/** Copied tooltip label, `'Copied'` by default */
|
|
43
|
-
copiedLabel?: string;
|
|
44
|
-
|
|
45
|
-
/** Determines whether code should be highlighted only after component is mounted to the dom (disables code highlight on server), `false` by default */
|
|
46
|
-
highlightOnClient?: boolean;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export type CodeHighlightFactory = Factory<{
|
|
50
|
-
props: CodeHighlightProps;
|
|
51
|
-
ref: HTMLDivElement;
|
|
52
|
-
stylesNames: CodeHighlightStylesNames;
|
|
53
|
-
}>;
|
|
54
|
-
|
|
55
|
-
const defaultProps: Partial<CodeHighlightProps> = {
|
|
56
|
-
copyLabel: 'Copy code',
|
|
57
|
-
copiedLabel: 'Copied',
|
|
58
|
-
language: 'tsx',
|
|
59
|
-
withCopyButton: true,
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const CodeHighlight = factory<CodeHighlightFactory>((_props, ref) => {
|
|
63
|
-
const props = useProps('CodeHighlight', defaultProps, _props);
|
|
64
|
-
const {
|
|
65
|
-
classNames,
|
|
66
|
-
className,
|
|
67
|
-
style,
|
|
68
|
-
styles,
|
|
69
|
-
unstyled,
|
|
70
|
-
vars,
|
|
71
|
-
children,
|
|
72
|
-
code,
|
|
73
|
-
copiedLabel,
|
|
74
|
-
copyLabel,
|
|
75
|
-
language,
|
|
76
|
-
withCopyButton,
|
|
77
|
-
highlightOnClient,
|
|
78
|
-
...others
|
|
79
|
-
} = props;
|
|
80
|
-
|
|
81
|
-
const getStyles = useStyles<CodeHighlightFactory>({
|
|
82
|
-
name: 'CodeHighlight',
|
|
83
|
-
props,
|
|
84
|
-
classes,
|
|
85
|
-
className,
|
|
86
|
-
style,
|
|
87
|
-
classNames,
|
|
88
|
-
styles,
|
|
89
|
-
unstyled,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const getCodeProps = useHighlight({
|
|
93
|
-
code,
|
|
94
|
-
language: language!,
|
|
95
|
-
highlightOnClient,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return (
|
|
99
|
-
<Box {...getStyles('root')} ref={ref} {...others} dir="ltr">
|
|
100
|
-
{withCopyButton && (
|
|
101
|
-
<CopyButton value={code.trim()}>
|
|
102
|
-
{({ copied, copy }) => (
|
|
103
|
-
<Tooltip label={copied ? copiedLabel : copyLabel} fz="sm" position="left">
|
|
104
|
-
<ActionIcon onClick={copy} variant="none" {...getStyles('copy')}>
|
|
105
|
-
<CopyIcon copied={copied} />
|
|
106
|
-
</ActionIcon>
|
|
107
|
-
</Tooltip>
|
|
108
|
-
)}
|
|
109
|
-
</CopyButton>
|
|
110
|
-
)}
|
|
111
|
-
|
|
112
|
-
<ScrollArea type="hover" dir="ltr" offsetScrollbars={false}>
|
|
113
|
-
<pre {...getStyles('pre')}>
|
|
114
|
-
<code {...getStyles('code')} {...getCodeProps()} />
|
|
115
|
-
</pre>
|
|
116
|
-
</ScrollArea>
|
|
117
|
-
</Box>
|
|
118
|
-
);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
CodeHighlight.displayName = '@mantine/core/CodeHighlight';
|
|
122
|
-
CodeHighlight.classes = classes;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { tests } from '@mantine/tests';
|
|
2
|
-
import {
|
|
3
|
-
CodeHighlightTabs,
|
|
4
|
-
CodeHighlightTabsProps,
|
|
5
|
-
CodeHighlightTabsStylesNames,
|
|
6
|
-
} from './CodeHighlightTabs';
|
|
7
|
-
|
|
8
|
-
const defaultProps: CodeHighlightTabsProps = {
|
|
9
|
-
code: [
|
|
10
|
-
{ fileName: 'Demo.tsx', language: 'tsx', code: 'test-tsx' },
|
|
11
|
-
{ fileName: 'Demo.module.css', language: 'css', code: 'test-css' },
|
|
12
|
-
],
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
describe('@mantine/code-highlight/CodeHighlightTabs', () => {
|
|
16
|
-
tests.itSupportsSystemProps<CodeHighlightTabsProps, CodeHighlightTabsStylesNames>({
|
|
17
|
-
component: CodeHighlightTabs,
|
|
18
|
-
props: defaultProps,
|
|
19
|
-
polymorphic: true,
|
|
20
|
-
styleProps: true,
|
|
21
|
-
extend: true,
|
|
22
|
-
variant: true,
|
|
23
|
-
size: true,
|
|
24
|
-
id: true,
|
|
25
|
-
classes: true,
|
|
26
|
-
refType: HTMLDivElement,
|
|
27
|
-
displayName: '@mantine/core/CodeHighlightTabs',
|
|
28
|
-
stylesApiSelectors: [
|
|
29
|
-
'root',
|
|
30
|
-
'code',
|
|
31
|
-
'codeWrapper',
|
|
32
|
-
'control',
|
|
33
|
-
'controls',
|
|
34
|
-
'file',
|
|
35
|
-
'files',
|
|
36
|
-
'header',
|
|
37
|
-
'pre',
|
|
38
|
-
'showCodeButton',
|
|
39
|
-
],
|
|
40
|
-
});
|
|
41
|
-
});
|
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import hljs from 'highlight.js';
|
|
3
|
-
import cx from 'clsx';
|
|
4
|
-
import { useUncontrolled } from '@mantine/hooks';
|
|
5
|
-
import {
|
|
6
|
-
Box,
|
|
7
|
-
BoxProps,
|
|
8
|
-
StylesApiProps,
|
|
9
|
-
factory,
|
|
10
|
-
ElementProps,
|
|
11
|
-
useProps,
|
|
12
|
-
useStyles,
|
|
13
|
-
CopyButton,
|
|
14
|
-
Tooltip,
|
|
15
|
-
ActionIcon,
|
|
16
|
-
UnstyledButton,
|
|
17
|
-
ScrollArea,
|
|
18
|
-
createVarsResolver,
|
|
19
|
-
rem,
|
|
20
|
-
Factory,
|
|
21
|
-
} from '@mantine/core';
|
|
22
|
-
import { CopyIcon } from './CopyIcon';
|
|
23
|
-
import { FileIcon } from './FileIcon';
|
|
24
|
-
import { ExpandIcon } from './ExpandIcon';
|
|
25
|
-
import _classes from './CodeHighlight.module.css';
|
|
26
|
-
import themeClasses from './CodeHighlight.theme.module.css';
|
|
27
|
-
|
|
28
|
-
const classes = { ..._classes, root: cx(_classes.root, themeClasses.theme) };
|
|
29
|
-
|
|
30
|
-
export type CodeHighlightTabsStylesNames =
|
|
31
|
-
| 'root'
|
|
32
|
-
| 'code'
|
|
33
|
-
| 'codeWrapper'
|
|
34
|
-
| 'showCodeButton'
|
|
35
|
-
| 'pre'
|
|
36
|
-
| 'controls'
|
|
37
|
-
| 'control'
|
|
38
|
-
| 'header'
|
|
39
|
-
| 'file'
|
|
40
|
-
| 'files'
|
|
41
|
-
| 'fileIcon';
|
|
42
|
-
|
|
43
|
-
export type CodeHighlightTabsCssVariables = {
|
|
44
|
-
root: '--ch-max-collapsed-height';
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export interface CodeHighlightTabsCode {
|
|
48
|
-
language?: string;
|
|
49
|
-
code: string;
|
|
50
|
-
fileName?: string;
|
|
51
|
-
icon?: React.ReactNode;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface CodeHighlightTabsProps
|
|
55
|
-
extends BoxProps,
|
|
56
|
-
StylesApiProps<CodeHighlightTabsFactory>,
|
|
57
|
-
ElementProps<'div'> {
|
|
58
|
-
/** Code to highlight with meta data (file name and icon) */
|
|
59
|
-
code: CodeHighlightTabsCode | CodeHighlightTabsCode[];
|
|
60
|
-
|
|
61
|
-
/** Default active tab index */
|
|
62
|
-
defaultActiveTab?: number;
|
|
63
|
-
|
|
64
|
-
/** Index of controlled active tab state */
|
|
65
|
-
activeTab?: number;
|
|
66
|
-
|
|
67
|
-
/** Called when tab changes */
|
|
68
|
-
onTabChange?: (tab: number) => void;
|
|
69
|
-
|
|
70
|
-
/** Determines whether header with file names and copy button should be rendered, `true` by default */
|
|
71
|
-
withHeader?: boolean;
|
|
72
|
-
|
|
73
|
-
/** Copy tooltip label, `'Copy code'` by default */
|
|
74
|
-
copyLabel?: string;
|
|
75
|
-
|
|
76
|
-
/** Copied tooltip label, `'Copied'` by default */
|
|
77
|
-
copiedLabel?: string;
|
|
78
|
-
|
|
79
|
-
/** Function that returns icon based on file name */
|
|
80
|
-
getFileIcon?: (fileName: string) => React.ReactNode;
|
|
81
|
-
|
|
82
|
-
/** `max-height` of code in collapsed state */
|
|
83
|
-
maxCollapsedHeight?: React.CSSProperties['maxHeight'];
|
|
84
|
-
|
|
85
|
-
/** Controlled expanded state */
|
|
86
|
-
expanded?: boolean;
|
|
87
|
-
|
|
88
|
-
/** Uncontrolled expanded state initial value */
|
|
89
|
-
defaultExpanded?: boolean;
|
|
90
|
-
|
|
91
|
-
/** Called when expanded state changes */
|
|
92
|
-
onExpandedChange?: (expanded: boolean) => void;
|
|
93
|
-
|
|
94
|
-
/** Expand button label and tooltip, `'Expand code'` by default */
|
|
95
|
-
expandCodeLabel?: string;
|
|
96
|
-
|
|
97
|
-
/** Collapse button label and tooltip, `'Collapse code'` by default */
|
|
98
|
-
collapseCodeLabel?: string;
|
|
99
|
-
|
|
100
|
-
/** Determines whether to show the expand button, `false` by default */
|
|
101
|
-
withExpandButton?: boolean;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export type CodeHighlightTabsFactory = Factory<{
|
|
105
|
-
props: CodeHighlightTabsProps;
|
|
106
|
-
ref: HTMLDivElement;
|
|
107
|
-
stylesNames: CodeHighlightTabsStylesNames;
|
|
108
|
-
}>;
|
|
109
|
-
|
|
110
|
-
const defaultProps: Partial<CodeHighlightTabsProps> = {
|
|
111
|
-
withHeader: true,
|
|
112
|
-
copyLabel: 'Copy code',
|
|
113
|
-
copiedLabel: 'Copied',
|
|
114
|
-
maxCollapsedHeight: rem('8rem'),
|
|
115
|
-
expandCodeLabel: 'Expand code',
|
|
116
|
-
collapseCodeLabel: 'Collapse code',
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
const varsResolver = createVarsResolver<CodeHighlightTabsFactory>((_, { maxCollapsedHeight }) => ({
|
|
120
|
-
root: { '--ch-max-collapsed-height': rem(maxCollapsedHeight) },
|
|
121
|
-
}));
|
|
122
|
-
|
|
123
|
-
export const CodeHighlightTabs = factory<CodeHighlightTabsFactory>((_props, ref) => {
|
|
124
|
-
const props = useProps('CodeHighlightTabs', defaultProps, _props);
|
|
125
|
-
const {
|
|
126
|
-
classNames,
|
|
127
|
-
className,
|
|
128
|
-
style,
|
|
129
|
-
styles,
|
|
130
|
-
unstyled,
|
|
131
|
-
vars,
|
|
132
|
-
children,
|
|
133
|
-
code,
|
|
134
|
-
defaultActiveTab,
|
|
135
|
-
activeTab,
|
|
136
|
-
onTabChange,
|
|
137
|
-
withHeader,
|
|
138
|
-
copiedLabel,
|
|
139
|
-
copyLabel,
|
|
140
|
-
getFileIcon,
|
|
141
|
-
maxCollapsedHeight,
|
|
142
|
-
expanded,
|
|
143
|
-
defaultExpanded,
|
|
144
|
-
onExpandedChange,
|
|
145
|
-
expandCodeLabel,
|
|
146
|
-
collapseCodeLabel,
|
|
147
|
-
withExpandButton,
|
|
148
|
-
...others
|
|
149
|
-
} = props;
|
|
150
|
-
|
|
151
|
-
const getStyles = useStyles<CodeHighlightTabsFactory>({
|
|
152
|
-
name: 'CodeHighlightTabs',
|
|
153
|
-
props,
|
|
154
|
-
classes,
|
|
155
|
-
className,
|
|
156
|
-
style,
|
|
157
|
-
classNames,
|
|
158
|
-
styles,
|
|
159
|
-
unstyled,
|
|
160
|
-
vars,
|
|
161
|
-
varsResolver,
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
const [value, setValue] = useUncontrolled({
|
|
165
|
-
defaultValue: defaultActiveTab,
|
|
166
|
-
value: activeTab,
|
|
167
|
-
finalValue: 0,
|
|
168
|
-
onChange: onTabChange,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const [_expanded, setExpanded] = useUncontrolled({
|
|
172
|
-
defaultValue: defaultExpanded,
|
|
173
|
-
value: expanded,
|
|
174
|
-
finalValue: true,
|
|
175
|
-
onChange: onExpandedChange,
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
const nodes = Array.isArray(code) ? code : [code];
|
|
179
|
-
const currentCode = nodes[value];
|
|
180
|
-
|
|
181
|
-
const highlighted = hljs.highlight(currentCode.code.trim(), {
|
|
182
|
-
language: currentCode.language || 'plaintext',
|
|
183
|
-
}).value;
|
|
184
|
-
|
|
185
|
-
const files = nodes.map((node, index) => (
|
|
186
|
-
<UnstyledButton
|
|
187
|
-
{...getStyles('file')}
|
|
188
|
-
key={node.fileName}
|
|
189
|
-
mod={{ active: index === value }}
|
|
190
|
-
onClick={() => setValue(index)}
|
|
191
|
-
>
|
|
192
|
-
<FileIcon
|
|
193
|
-
fileIcon={node.icon}
|
|
194
|
-
getFileIcon={getFileIcon}
|
|
195
|
-
fileName={node.fileName}
|
|
196
|
-
{...getStyles('fileIcon')}
|
|
197
|
-
/>
|
|
198
|
-
<span>{node.fileName}</span>
|
|
199
|
-
</UnstyledButton>
|
|
200
|
-
));
|
|
201
|
-
|
|
202
|
-
return (
|
|
203
|
-
<Box {...getStyles('root')} mod={{ collapsed: !_expanded }} ref={ref} {...others} dir="ltr">
|
|
204
|
-
{withHeader && (
|
|
205
|
-
<div {...getStyles('header')}>
|
|
206
|
-
<ScrollArea type="never" dir="ltr" offsetScrollbars={false}>
|
|
207
|
-
<div {...getStyles('files')}>{files}</div>
|
|
208
|
-
</ScrollArea>
|
|
209
|
-
<div {...getStyles('controls')}>
|
|
210
|
-
{withExpandButton && (
|
|
211
|
-
<Tooltip
|
|
212
|
-
label={_expanded ? collapseCodeLabel : expandCodeLabel}
|
|
213
|
-
fz="sm"
|
|
214
|
-
position="left"
|
|
215
|
-
>
|
|
216
|
-
<ActionIcon
|
|
217
|
-
onClick={() => setExpanded(!_expanded)}
|
|
218
|
-
variant="none"
|
|
219
|
-
aria-label={_expanded ? collapseCodeLabel : expandCodeLabel}
|
|
220
|
-
{...getStyles('control')}
|
|
221
|
-
>
|
|
222
|
-
<ExpandIcon expanded={_expanded} />
|
|
223
|
-
</ActionIcon>
|
|
224
|
-
</Tooltip>
|
|
225
|
-
)}
|
|
226
|
-
|
|
227
|
-
<CopyButton value={currentCode.code.trim()}>
|
|
228
|
-
{({ copied, copy }) => (
|
|
229
|
-
<Tooltip label={copied ? copiedLabel : copyLabel} fz="sm" position="left">
|
|
230
|
-
<ActionIcon
|
|
231
|
-
onClick={copy}
|
|
232
|
-
variant="none"
|
|
233
|
-
{...getStyles('control')}
|
|
234
|
-
aria-label={copied ? copiedLabel : copyLabel}
|
|
235
|
-
>
|
|
236
|
-
<CopyIcon copied={copied} />
|
|
237
|
-
</ActionIcon>
|
|
238
|
-
</Tooltip>
|
|
239
|
-
)}
|
|
240
|
-
</CopyButton>
|
|
241
|
-
</div>
|
|
242
|
-
</div>
|
|
243
|
-
)}
|
|
244
|
-
|
|
245
|
-
<ScrollArea type="auto" dir="ltr" offsetScrollbars={false}>
|
|
246
|
-
<Box {...getStyles('codeWrapper')} mod={{ expanded: _expanded }}>
|
|
247
|
-
<pre {...getStyles('pre')}>
|
|
248
|
-
<code {...getStyles('code')} dangerouslySetInnerHTML={{ __html: highlighted }} />
|
|
249
|
-
</pre>
|
|
250
|
-
</Box>
|
|
251
|
-
</ScrollArea>
|
|
252
|
-
<UnstyledButton
|
|
253
|
-
{...getStyles('showCodeButton')}
|
|
254
|
-
mod={{ hidden: _expanded }}
|
|
255
|
-
onClick={() => setExpanded(true)}
|
|
256
|
-
>
|
|
257
|
-
{expandCodeLabel}
|
|
258
|
-
</UnstyledButton>
|
|
259
|
-
</Box>
|
|
260
|
-
);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
CodeHighlightTabs.displayName = '@mantine/core/CodeHighlightTabs';
|
|
264
|
-
CodeHighlightTabs.classes = classes;
|
package/src/CopyIcon.tsx
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { rem } from '@mantine/core';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
|
|
4
|
-
interface CopyIconProps extends React.ComponentPropsWithoutRef<'svg'> {
|
|
5
|
-
copied: boolean;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function CopyIcon({ copied, style, ...others }: CopyIconProps) {
|
|
9
|
-
return (
|
|
10
|
-
<svg
|
|
11
|
-
xmlns="http://www.w3.org/2000/svg"
|
|
12
|
-
style={{ width: rem(18), height: rem(18), ...style }}
|
|
13
|
-
viewBox="0 0 24 24"
|
|
14
|
-
strokeWidth="2"
|
|
15
|
-
stroke="currentColor"
|
|
16
|
-
fill="none"
|
|
17
|
-
strokeLinecap="round"
|
|
18
|
-
strokeLinejoin="round"
|
|
19
|
-
{...others}
|
|
20
|
-
>
|
|
21
|
-
{copied ? (
|
|
22
|
-
<>
|
|
23
|
-
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
|
24
|
-
<path d="M5 12l5 5l10 -10"></path>
|
|
25
|
-
</>
|
|
26
|
-
) : (
|
|
27
|
-
<>
|
|
28
|
-
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
|
29
|
-
<path d="M8 8m0 2a2 2 0 0 1 2 -2h8a2 2 0 0 1 2 2v8a2 2 0 0 1 -2 2h-8a2 2 0 0 1 -2 -2z"></path>
|
|
30
|
-
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
|
31
|
-
</>
|
|
32
|
-
)}
|
|
33
|
-
</svg>
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
CopyIcon.displayName = '@mantine/code-highlight/CopyIcon';
|
package/src/ExpandIcon.tsx
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { rem } from '@mantine/core';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
|
|
4
|
-
interface ExpandIconProps extends React.ComponentPropsWithoutRef<'svg'> {
|
|
5
|
-
expanded: boolean;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function ExpandIcon({ expanded, style, ...others }: ExpandIconProps) {
|
|
9
|
-
return (
|
|
10
|
-
<svg
|
|
11
|
-
xmlns="http://www.w3.org/2000/svg"
|
|
12
|
-
style={{ width: rem(18), height: rem(18), ...style }}
|
|
13
|
-
viewBox="0 0 24 24"
|
|
14
|
-
strokeWidth="2"
|
|
15
|
-
stroke="currentColor"
|
|
16
|
-
fill="none"
|
|
17
|
-
strokeLinecap="round"
|
|
18
|
-
strokeLinejoin="round"
|
|
19
|
-
{...others}
|
|
20
|
-
>
|
|
21
|
-
{expanded ? (
|
|
22
|
-
<>
|
|
23
|
-
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
|
|
24
|
-
<path d="M12 13v-8l-3 3m6 0l-3 -3" />
|
|
25
|
-
<path d="M9 17l1 0" />
|
|
26
|
-
<path d="M14 17l1 0" />
|
|
27
|
-
<path d="M19 17l1 0" />
|
|
28
|
-
<path d="M4 17l1 0" />
|
|
29
|
-
</>
|
|
30
|
-
) : (
|
|
31
|
-
<>
|
|
32
|
-
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
|
|
33
|
-
<path d="M12 11v8l3 -3m-6 0l3 3" />
|
|
34
|
-
<path d="M9 7l1 0" />
|
|
35
|
-
<path d="M14 7l1 0" />
|
|
36
|
-
<path d="M19 7l1 0" />
|
|
37
|
-
<path d="M4 7l1 0" />
|
|
38
|
-
</>
|
|
39
|
-
)}
|
|
40
|
-
</svg>
|
|
41
|
-
);
|
|
42
|
-
}
|
package/src/FileIcon.tsx
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
|
|
3
|
-
interface FileIconProps {
|
|
4
|
-
fileName: string | undefined;
|
|
5
|
-
getFileIcon?: ((fileName: string) => React.ReactNode) | undefined;
|
|
6
|
-
fileIcon: React.ReactNode | undefined;
|
|
7
|
-
className?: string;
|
|
8
|
-
style?: React.CSSProperties;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function FileIcon({ fileIcon, fileName, getFileIcon, className, style }: FileIconProps) {
|
|
12
|
-
if (fileIcon) {
|
|
13
|
-
return (
|
|
14
|
-
<span className={className} style={style}>
|
|
15
|
-
{fileIcon}
|
|
16
|
-
</span>
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (getFileIcon && fileName) {
|
|
21
|
-
return (
|
|
22
|
-
<span className={className} style={style}>
|
|
23
|
-
{getFileIcon(fileName)}
|
|
24
|
-
</span>
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { tests } from '@mantine/tests';
|
|
2
|
-
import {
|
|
3
|
-
InlineCodeHighlight,
|
|
4
|
-
InlineCodeHighlightProps,
|
|
5
|
-
InlineCodeHighlightStylesNames,
|
|
6
|
-
} from './InlineCodeHighlight';
|
|
7
|
-
|
|
8
|
-
const defaultProps: InlineCodeHighlightProps = {
|
|
9
|
-
code: 'test-code',
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
describe('@mantine/code-highlight/InlineCodeHighlight', () => {
|
|
13
|
-
tests.itSupportsSystemProps<InlineCodeHighlightProps, InlineCodeHighlightStylesNames>({
|
|
14
|
-
component: InlineCodeHighlight,
|
|
15
|
-
props: defaultProps,
|
|
16
|
-
polymorphic: true,
|
|
17
|
-
styleProps: true,
|
|
18
|
-
extend: true,
|
|
19
|
-
variant: true,
|
|
20
|
-
size: true,
|
|
21
|
-
refType: HTMLElement,
|
|
22
|
-
displayName: '@mantine/core/InlineCodeHighlight',
|
|
23
|
-
stylesApiSelectors: ['code'],
|
|
24
|
-
});
|
|
25
|
-
});
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import cx from 'clsx';
|
|
3
|
-
import {
|
|
4
|
-
Box,
|
|
5
|
-
BoxProps,
|
|
6
|
-
StylesApiProps,
|
|
7
|
-
factory,
|
|
8
|
-
ElementProps,
|
|
9
|
-
useProps,
|
|
10
|
-
useStyles,
|
|
11
|
-
Factory,
|
|
12
|
-
} from '@mantine/core';
|
|
13
|
-
import hljs from 'highlight.js';
|
|
14
|
-
import _classes from './CodeHighlight.module.css';
|
|
15
|
-
import themeClasses from './CodeHighlight.theme.module.css';
|
|
16
|
-
|
|
17
|
-
const classes = { ..._classes, code: cx(_classes.code, themeClasses.theme) };
|
|
18
|
-
|
|
19
|
-
export type InlineCodeHighlightStylesNames = 'code';
|
|
20
|
-
|
|
21
|
-
export interface InlineCodeHighlightProps
|
|
22
|
-
extends BoxProps,
|
|
23
|
-
StylesApiProps<InlineCodeHighlightFactory>,
|
|
24
|
-
ElementProps<'div'> {
|
|
25
|
-
/** Code to highlight */
|
|
26
|
-
code: string;
|
|
27
|
-
|
|
28
|
-
/** Code language, `'tsx'` by default */
|
|
29
|
-
language?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type InlineCodeHighlightFactory = Factory<{
|
|
33
|
-
props: InlineCodeHighlightProps;
|
|
34
|
-
ref: HTMLElement;
|
|
35
|
-
stylesNames: InlineCodeHighlightStylesNames;
|
|
36
|
-
}>;
|
|
37
|
-
|
|
38
|
-
const defaultProps: Partial<InlineCodeHighlightProps> = {
|
|
39
|
-
language: 'tsx',
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export const InlineCodeHighlight = factory<InlineCodeHighlightFactory>((_props, ref) => {
|
|
43
|
-
const props = useProps('InlineCodeHighlight', defaultProps, _props);
|
|
44
|
-
const { classNames, className, style, styles, unstyled, vars, code, language, ...others } = props;
|
|
45
|
-
|
|
46
|
-
const getStyles = useStyles<InlineCodeHighlightFactory>({
|
|
47
|
-
name: 'InlineCodeHighlight',
|
|
48
|
-
props,
|
|
49
|
-
classes,
|
|
50
|
-
className,
|
|
51
|
-
style,
|
|
52
|
-
classNames,
|
|
53
|
-
styles,
|
|
54
|
-
unstyled,
|
|
55
|
-
rootSelector: 'code',
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
const highlighted = hljs.highlight(code.trim(), { language: language! }).value;
|
|
59
|
-
|
|
60
|
-
return (
|
|
61
|
-
<Box
|
|
62
|
-
{...getStyles('code')}
|
|
63
|
-
component="code"
|
|
64
|
-
ref={ref}
|
|
65
|
-
{...others}
|
|
66
|
-
dangerouslySetInnerHTML={{ __html: highlighted }}
|
|
67
|
-
/>
|
|
68
|
-
);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
InlineCodeHighlight.displayName = '@mantine/core/InlineCodeHighlight';
|
package/src/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export { CodeHighlightTabs } from './CodeHighlightTabs';
|
|
2
|
-
export { CodeHighlight } from './CodeHighlight';
|
|
3
|
-
export { InlineCodeHighlight } from './InlineCodeHighlight';
|
|
4
|
-
export type {
|
|
5
|
-
CodeHighlightTabsFactory,
|
|
6
|
-
CodeHighlightTabsStylesNames,
|
|
7
|
-
CodeHighlightTabsProps,
|
|
8
|
-
CodeHighlightTabsCode,
|
|
9
|
-
CodeHighlightTabsCssVariables,
|
|
10
|
-
} from './CodeHighlightTabs';
|
|
11
|
-
export type {
|
|
12
|
-
CodeHighlightFactory,
|
|
13
|
-
CodeHighlightProps,
|
|
14
|
-
CodeHighlightStylesNames,
|
|
15
|
-
} from './CodeHighlight';
|
|
16
|
-
export type {
|
|
17
|
-
InlineCodeHighlightFactory,
|
|
18
|
-
InlineCodeHighlightProps,
|
|
19
|
-
InlineCodeHighlightStylesNames,
|
|
20
|
-
} from './InlineCodeHighlight';
|
package/src/use-highlight.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { useState, useEffect } from 'react';
|
|
2
|
-
import hljs from 'highlight.js';
|
|
3
|
-
|
|
4
|
-
interface UseHighlightInput {
|
|
5
|
-
code: string;
|
|
6
|
-
language: string;
|
|
7
|
-
highlightOnClient: boolean | undefined;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function useHighlight({ code, language, highlightOnClient }: UseHighlightInput) {
|
|
11
|
-
const lang = hljs.getLanguage(language) ? language : 'plaintext';
|
|
12
|
-
const getHighlightedCode = () => hljs.highlight(code.trim(), { language: lang }).value;
|
|
13
|
-
const [highlighted, setHighlighted] = useState(!highlightOnClient);
|
|
14
|
-
const [highlightedCode, setHighlightedCode] = useState(
|
|
15
|
-
highlightOnClient ? code : getHighlightedCode()
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
const getCodeProps = () =>
|
|
19
|
-
highlighted
|
|
20
|
-
? { dangerouslySetInnerHTML: { __html: highlightedCode } }
|
|
21
|
-
: { children: code.trim() };
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (highlightOnClient) {
|
|
25
|
-
setHighlightedCode(getHighlightedCode());
|
|
26
|
-
setHighlighted(true);
|
|
27
|
-
}
|
|
28
|
-
}, []);
|
|
29
|
-
|
|
30
|
-
useEffect(() => {
|
|
31
|
-
setHighlightedCode(getHighlightedCode());
|
|
32
|
-
}, [code]);
|
|
33
|
-
|
|
34
|
-
return getCodeProps;
|
|
35
|
-
}
|