Repository navigation
Expand file tree
/
Copy pathindex.tsx
More file actions
146 lines (123 loc) · 4.14 KB
/
Copy pathindex.tsx
File metadata and controls
146 lines (123 loc) · 4.14 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
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
'use client';
import {
DocumentDuplicateIcon,
CodeBracketIcon,
CheckIcon,
} from '@heroicons/react/24/outline';
import classNames from 'classnames';
import { Fragment, isValidElement, useRef } from 'react';
import BaseButton from '#ui/Common/BaseButton';
import useCopy from '#ui/hooks/useCopy';
import type { LinkLike } from '#ui/types';
import type { FC, PropsWithChildren, ReactElement, ReactNode } from 'react';
import styles from './index.module.css';
// Transforms a code element with plain text content into a more structured
// format for rendering with line numbers
const transformCode = <T extends ReactElement<PropsWithChildren>>(
code: T,
language: string
): ReactElement<HTMLElement> | T => {
if (!isValidElement(code)) {
// Early return when the `CodeBox` child is not a valid element since the
// type is a ReactNode, and can assume any value
return code;
}
const content = code.props?.children;
if (code.type !== 'code' || typeof content !== 'string') {
// There is no need to transform an element that is not a code element or
// a content that is not a string
return code;
}
// Note that since we use `.split` we will have an extra entry
// being an empty string, so we need to remove it
const lines = content.split('\n');
const extraClasses = classNames({ 'plain-text': language.length === 0 });
return (
<code className={extraClasses}>
{lines.flatMap((line, lineIndex) => {
const columns = line.split(' ');
return [
// eslint-disable-next-line @eslint-react/no-array-index-key -- lines from split string have no stable ID
<span key={lineIndex} className="line">
{columns.map((column, columnIndex) => (
// eslint-disable-next-line @eslint-react/no-array-index-key -- columns from split string have no stable ID
<Fragment key={columnIndex}>
<span>{column}</span>
{columnIndex < columns.length - 1 && <span> </span>}
</Fragment>
))}
</span>,
// Add a break line so the text content is formatted correctly
// when copying to clipboard
'\n',
];
})}
</code>
);
};
type CodeBoxProps = {
language: string;
className?: string;
as?: LinkLike;
copyButtonLabel: ReactNode;
copiedButtonLabel: ReactNode;
};
const BaseCodeBox: FC<PropsWithChildren<CodeBoxProps>> = ({
children,
language,
className,
copyButtonLabel,
copiedButtonLabel,
as = 'a',
}: PropsWithChildren<CodeBoxProps>) => {
const [copied, copy] = useCopy();
const containerRef = useRef<HTMLPreElement>(null);
const handleCopy = () => copy(containerRef.current?.textContent);
const FooterButtonIcon = copied ? DocumentDuplicateIcon : CodeBracketIcon;
const hideFooter = className?.includes('no-footer');
const activeLabel = copied ? copiedButtonLabel : copyButtonLabel;
const ghostLabel = typeof activeLabel === 'string' ? activeLabel : undefined;
return (
<div className={styles.root}>
<pre
ref={containerRef}
className={classNames(styles.content, className)}
tabIndex={0}
>
{transformCode(children as ReactElement<PropsWithChildren>, language)}
</pre>
{hideFooter && (
<BaseButton
as={as}
className={styles.ghostAction}
kind="neutral"
size="small"
aria-label={ghostLabel}
title={ghostLabel}
onClick={handleCopy}
>
{copied ? (
<CheckIcon className="size-4" />
) : (
<DocumentDuplicateIcon className="size-4" />
)}
</BaseButton>
)}
{!hideFooter && Boolean(language) && (
<div className={styles.footer}>
<span className={styles.language}>{language}</span>
<BaseButton
as={as}
className={styles.action}
kind="neutral"
onClick={handleCopy}
>
<FooterButtonIcon className="size-4" />
{copied ? copiedButtonLabel : copyButtonLabel}
</BaseButton>
</div>
)}
</div>
);
};
export default BaseCodeBox;