Skip to content

Commit c2e7947

Browse files
committed
add copy functionality
1 parent 7e7032d commit c2e7947

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/generators/jsx-ast/utils/buildBarProps.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const buildMetaBarProps = (head, entries) => {
4242
.map(entry => ({
4343
depth: entry.heading.depth,
4444
value: entry.heading.data.name,
45+
data: { id: entry.heading.data.slug },
4546
}));
4647

4748
return {

src/generators/web/components/CodeBox.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1+
import { useState } from 'react';
12
import { getLanguageDisplayName } from '@node-core/rehype-shiki';
23
import BaseCodeBox from '@node-core/ui-components/Common/BaseCodeBox';
34

45
const MDXCodeBox = ({ className, ...props }) => {
56
const matches = className?.match(/language-(?<language>[a-zA-Z]+)/);
67
const language = matches?.groups?.language ?? '';
8+
const [copyText, setCopyText] = useState('Copy to clipboard');
9+
10+
const handleCopy = async (text) => {
11+
await navigator.clipboard.writeText(text);
12+
13+
setCopyText('Copied to clipboard!');
14+
setTimeout(() => {
15+
setCopyText('Copy to clipboard');
16+
}, 500);
17+
};
718

819
return (
920
<BaseCodeBox
1021
as={'a'}
11-
onCopy={() => {}}
22+
onCopy={handleCopy}
1223
language={getLanguageDisplayName(language)}
1324
{...props}
14-
copyText={'Copy to clipboard'}
15-
copiedText={'Copied to clipboard'}
25+
copyText={copyText}
1626
/>
1727
);
1828
};

0 commit comments

Comments
 (0)