-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: markdown code copy listener (#93)
- Loading branch information
1 parent
826aaaa
commit 4ae3a06
Showing
2 changed files
with
42 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use client'; | ||
import { useEffect } from 'react'; | ||
|
||
export default function MarkdownCopyListener() { | ||
useEffect(() => { | ||
const copyBtns = document.querySelectorAll('button.copy-code-btn'); | ||
copyBtns.forEach((btn) => { | ||
(btn as HTMLButtonElement).addEventListener('click', () => { | ||
const codeBlockContent = btn.parentElement?.nextElementSibling?.textContent; | ||
if (codeBlockContent) { | ||
navigator.clipboard.writeText(codeBlockContent).then(() => { | ||
btn.textContent = 'Copied'; | ||
setTimeout(() => { | ||
btn.textContent = 'Copy'; | ||
}, 2000); | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
return () => { | ||
copyBtns.forEach((btn) => { | ||
(btn as HTMLButtonElement).removeEventListener('click', () => {}); | ||
}); | ||
}; | ||
}, []); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters