Skip to content

Commit 1a27ff5

Browse files
committed
prevent .no-copy elements from being copied during selction Right click copy or / Cmd+C
1 parent bf09870 commit 1a27ff5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/components/codeBlock/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
4848
const [showCopyButton, setShowCopyButton] = useState(false);
4949
useEffect(() => {
5050
setShowCopyButton(true);
51+
// prevent .no-copy elements from being copied during selction Right click copy or / Cmd+C
52+
const noCopyElements = codeRef.current?.querySelectorAll<HTMLSpanElement>('.no-copy');
53+
document.addEventListener('selectionchange', () => {
54+
// hide no copy elements within the selection
55+
const selection = window.getSelection();
56+
noCopyElements?.forEach(element => {
57+
if (selection?.containsNode(element, true)) {
58+
element.style.display = 'none';
59+
} else {
60+
element.style.display = 'inline';
61+
}
62+
});
63+
});
5164
}, []);
5265

5366
useCleanSnippetInClipboard(codeRef, {language});

src/components/codeKeywords/styles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ export const KeywordIndicator = styled(ArrowDown, {
154154
top: -1px;
155155
`;
156156

157-
export const KeywordSpan = styled(motion.span)<{
157+
export const KeywordSpan = styled(motion.span, {
158+
shouldForwardProp: prop => prop !== 'hasPreview',
159+
})<{
158160
hasPreview?: boolean;
159161
}>`
160162
grid-row: 1;

0 commit comments

Comments
 (0)