Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#180): Added copy to clipboard functionality #184

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/components/ViewSnippetSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import { cn } from "@/lib/utils"
import { useCurrentSnippet } from "@/hooks/use-current-snippet"
import { useToast } from "@/hooks/use-toast"
import { useFilesDialog } from "./dialogs/files-dialog"
import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard"

export default function ViewSnippetSidebar({
className,
}: { className?: string }) {
}: {
className?: string
}) {
const { snippet } = useCurrentSnippet()
const { toast } = useToast()
const { Dialog: FilesDialog, openDialog: openFilesDialog } = useFilesDialog()
const { copyToClipboard } = useCopyToClipboard()

return (
<div
Expand Down Expand Up @@ -126,20 +130,41 @@ export default function ViewSnippetSidebar({
<div className="p-4 border-t border-gray-200 space-y-4">
<div className="space-y-1">
<div className="text-xs font-medium">Copy embed code</div>
<div className="text-[0.5em] p-2 rounded-sm bg-blue-50 border border-blue-200 cursor-pointer font-mono whitespace-nowrap overflow-hidden text-ellipsis">
<div
className="text-[0.5em] p-2 rounded-sm bg-blue-50 border border-blue-200 cursor-pointer font-mono whitespace-nowrap overflow-hidden text-ellipsis"
onClick={() =>
copyToClipboard(
`<iframe src="https://snippets.tscircuit.com/embed/seveibar/circuitmodule" width="100%" height="100%"></iframe>`,
)
}
>
{`<iframe src="https://snippets.tscircuit.com/embed/seveibar/circuitmodule" width="100%" height="100%"></iframe>`}
</div>
</div>
<div className="space-y-1">
<div className="text-xs font-medium">Copy import code</div>
<div className="text-[0.5em] p-2 rounded-sm bg-blue-50 border border-blue-200 cursor-pointer font-mono whitespace-nowrap overflow-hidden text-ellipsis">
<div
className="text-[0.5em] p-2 rounded-sm bg-blue-50 border border-blue-200 cursor-pointer font-mono whitespace-nowrap overflow-hidden text-ellipsis"
onClick={() =>
copyToClipboard(
`import CircuitModule from "@tsci/${snippet?.owner_name}.${snippet?.unscoped_name}"`,
)
}
>
import CircuitModule from "@tsci/{snippet?.owner_name}.
{snippet?.unscoped_name}"
</div>
</div>
<div className="space-y-1">
<div className="text-xs font-medium">Copy install command</div>
<div className="text-[0.5em] p-2 rounded-sm bg-blue-50 border border-blue-200 cursor-pointer font-mono whitespace-nowrap overflow-hidden text-ellipsis">
<div
className="text-[0.5em] p-2 rounded-sm bg-blue-50 border border-blue-200 cursor-pointer font-mono whitespace-nowrap overflow-hidden text-ellipsis"
onClick={() =>
copyToClipboard(
`tsci add @tsci/${snippet?.owner_name}.${snippet?.unscoped_name}`,
)
}
>
tsci add @tsci/{snippet?.owner_name}.{snippet?.unscoped_name}
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/use-copy-to-clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useToast } from "@/hooks/use-toast"
import { useCallback } from "react"

export const useCopyToClipboard = () => {
const { toast } = useToast()

const copyToClipboard = useCallback(
async (text: string) => {
try {
await navigator.clipboard.writeText(text)
toast({
title: "Copied to clipboard!",
description: "The text has been copied successfully.",
})
} catch (error) {
toast({
title: "Failed to copy",
description: "There was an error copying the text.",
})
}
},
[toast],
)

return { copyToClipboard }
}
Loading