forked from rocicorp/zero-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-content.tsx
More file actions
33 lines (27 loc) · 958 Bytes
/
copy-content.tsx
File metadata and controls
33 lines (27 loc) · 958 Bytes
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
'use client';
import {useEffect} from 'react';
export default function CopyContent({content}: {content: React.ReactNode}) {
useEffect(() => {
const copyButtons = document.querySelectorAll('.copy-button');
copyButtons.forEach(button => {
const handleClick = () => {
const codeBlock = button.nextElementSibling?.textContent; // Assumes the code block follows the button
if (codeBlock) {
navigator.clipboard.writeText(codeBlock).then(() => {
// Provide feedback to the user
button.textContent = 'Copied!';
setTimeout(() => {
button.textContent = 'Copy';
}, 2000);
});
}
};
button.addEventListener('click', handleClick);
// Cleanup listener
return () => {
button.removeEventListener('click', handleClick);
};
});
}, [content]); // Re-run if content changes
return <div>{content}</div>;
}