Logo

dev-resources.site

for different kinds of informations.

Comparing the copyToClipboard implementations in Shadcn-ui/ui and Codehike.

Published at
8/7/2024
Categories
codehike
shadcnui
typescript
opensource
Author
ramunarasinga
Author
13 person written this
ramunarasinga
open
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)
  }
}


Enter fullscreen mode Exit fullscreen mode

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)
  }
}


Enter fullscreen mode Exit fullscreen mode

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)
}


Enter fullscreen mode Exit fullscreen mode

Codehike implements the copyToClipboard differently.

  1. There is no analytics recorded when the copyToClipboard function is called like we saw this happening in Shadcn-ui/ui.
  2. 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)
}

Enter fullscreen mode Exit fullscreen mode




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.

Build open source projects like Shadcn-ui/ui from scratch

About me:

Website: https://ramunarasinga.com/

Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/

Github: https://github.com/Ramu-Narasinga

Email: [email protected]

Learn the best practices used in open source.

References:

  1. https://github.com/code-hike/codehike/blob/next/packages/mdx/src/mini-editor/editor-tween.tsx
  2. https://github.com/code-hike/codehike/blob/next/packages/mdx/src/smooth-code/copy-button.tsx#L3
  3. https://github.com/shadcn-ui/ui/blob/main/apps/www/components/copy-button.tsx#L31
  4. https://github.com/shadcn-ui/ui/blob/main/apps/www/components/copy-button.tsx#L24
  5. https://github.com/shadcn-ui/ui/blob/main/apps/www/lib/events.ts#L27
shadcnui Article's
30 articles in total
Favicon
Color Highlighting for Tailwind CSS Variables in VS Code
Favicon
Shadcn UI Theme generator, with OKLCH colors and ancient sacred geometry.
Favicon
Next.js Starter Kit: Build Fast & Secure Apps with Auth, Payments & More!
Favicon
Comparing the copyToClipboard implementations in Shadcn-ui/ui and Codehike.
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 3.1
Favicon
Comparison of file and component structures among Shadcn-ui, Plane.so and Gitroom.
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 3.0
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.15
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.14
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.13
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.12
Favicon
Create File Upload UI in Next.js with Shadcn UI
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.11
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.10
Favicon
How to Use Icons in Shadcn UI with Next.js
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.9
Favicon
Next.js with Shadcn UI Progress Bar Example
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.7
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.5
Favicon
How to Use Scroll Area in Next.js with Shadcn UI
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.4
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.3
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.2
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.0
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 2.1
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 1.1
Favicon
shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? β€” Part 1.0
Favicon
shadcn-ui/ui codebase analysis: How is β€œBlocks” page built β€” Part 5
Favicon
Next.js Image File Upload and Preview with shadcn/ui
Favicon
shadcn-ui/ui codebase analysis: How is β€œBlocks” page built β€” Part 3

Featured ones: