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

feat: add copy code button #778

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
17 changes: 15 additions & 2 deletions examples/ui-demo/src/components/preview/CodePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ExternalLink from "../shared/ExternalLink";
import { Roboto_Mono } from "next/font/google";
import { Config, useConfig } from "@/src/app/state";
import dedent from "dedent";
import { Check, Copy } from "lucide-react";
import { useState } from "react";
const robotoMono = Roboto_Mono({
subsets: ["latin"],
display: "swap",
Expand Down Expand Up @@ -39,10 +41,21 @@ export function CodePreview() {
}

function CodeBlock({ title, code }: { title: string; code: string }) {
const [copied, setCopied] = useState(false);

const onCopy = () => {
navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}

return (
<div className="rounded-lg flex flex-col text-sm overflow-hidden">
<div className="px-4 py-3 bg-gray-700 text-white font-medium">
{title}
<div className="flex justify-between px-4 py-3 bg-gray-700 text-white font-medium">
<div>{title}</div>
<button onClick={onCopy} className="bg-white/10 rounded-lg h-7 w-7 flex justify-center items-center">
{copied ? <Check size={16} /> : <Copy size={16} />}
</button>
</div>
<pre
className={cn("px-4 py-6 bg-gray-800 text-white", robotoMono.className)}
Expand Down
Loading