Comparing the copyToClipboard implementations in Shadcn-ui/ui and Codehike.
In this article, we will compare the Copy button code between Shadcn-ui/ui and Codehike.
copyToClipboard in Shadcn-ui/ui
The code snippet below is picked from shadcn-ui source code.
export async function copyToClipboardWithMeta(value: string, event?: Event) {
navigator.clipboard.writeText(value)
if (event) {
trackEvent(event)
}
}
I think ‘withMeta’ in the function name copyToClipboardWithMeta refers to the analytics recorded in the trackEvent function.
import va from "@vercel/analytics"
export function trackEvent(input: Event): void {
const event = eventSchema.parse(input)
if (event) {
va.track(event.name, event.properties)
}
}
copyToClipboard in Codehike
The code snippet below is picked from codehike source code.
function copyToClipboard(text: string) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text)
return
}
navigator.clipboard.writeText(text)
}
Codehike implements the copyToClipboard differently.
-
There is no analytics recorded when the copyToClipboard function is called like we saw this happening in Shadcn-ui/ui.
-
There is a fallback method in case the navigation.clipboard API is not available.
fallbackCopyTextToClipboard:
This below code snippet is picked from Codehike source code. This function is just under the copyToClipboard.
function fallbackCopyTextToClipboard(text: string) {
var textArea = document.createElement("textarea")
textArea.value = text
// Avoid scrolling to bottom
textArea.style.top = "0"
textArea.style.left = "0"
textArea.style.position = "fixed"
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
try {
var successful = document.execCommand("copy")
// var msg = successful ? "successful" : "unsuccessful"
// console.log("Fallback: Copying text command was " + msg)
} catch (err) {
// console.error("Fallback: Oops, unable to copy", err)
}
document.body.removeChild(textArea)
}
Conclusion:
If I were to implement a copyToClipboard functionality, I would also add a fallback in case the navigator.clipboard is not available in a given browser like in Codehike and if you also use Vercel analytics in your application, you might as well record your analytics like in shadcn-ui/ui.
Get free courses inspired by the best practices used in open source.
About me:
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.
I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com
My Github — https://github.com/ramu-narasinga
My website — https://ramunarasinga.com
My Youtube channel — https://www.youtube.com/@thinkthroo
Learning platform — https://thinkthroo.com
Codebase Architecture — https://app.thinkthroo.com/architecture
Best practices — https://app.thinkthroo.com/best-practices
Production-grade projects — https://app.thinkthroo.com/production-grade-projects
References:
-
https://github.com/code-hike/codehike/blob/next/packages/mdx/src/mini-editor/editor-tween.tsx
-
https://github.com/code-hike/codehike/blob/next/packages/mdx/src/smooth-code/copy-button.tsx#L3
-
https://github.com/shadcn-ui/ui/blob/main/apps/www/components/copy-button.tsx#L31
-
https://github.com/shadcn-ui/ui/blob/main/apps/www/components/copy-button.tsx#L24
-
https://github.com/shadcn-ui/ui/blob/main/apps/www/lib/events.ts#L27