Skip to content

Commit c21a1be

Browse files
committed
added copy button ©
1 parent a9e6897 commit c21a1be

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

components/ComponentShowcase.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { componentConfig } from "@/config";
22
import { H5 } from "@/packages/ui";
33
import { TabGroup, TabList, TabPanels, TabPanel, Tab } from "@headlessui/react";
44
import React, { HTMLAttributes } from "react";
5+
import { CopyButton } from "./CopyButton";
56

67
interface IComponentShowcase extends HTMLAttributes<HTMLDivElement> {
78
name: keyof typeof componentConfig.examples;
@@ -28,7 +29,10 @@ export function ComponentShowcase({ name, children }: IComponentShowcase) {
2829
</div>
2930
</TabPanel>
3031
<TabPanel>
31-
<div className="relative rounded overflow-auto">{Code}</div>
32+
<div className="relative rounded overflow-auto">
33+
<CopyButton value={Code.toString()} />
34+
{Code}
35+
</div>
3236
</TabPanel>
3337
</TabPanels>
3438
</TabGroup>

components/CopyButton.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use client";
2+
3+
import * as React from "react";
4+
import { cn } from "@/lib/utils";
5+
import { Button, IButtonProps } from "@/packages/ui";
6+
7+
interface ICopyButtonProps extends IButtonProps {
8+
value: string;
9+
src?: string;
10+
}
11+
12+
export async function copyToClipboardWithMeta(value: string, event?: Event) {
13+
navigator.clipboard.writeText(value);
14+
if (event) {
15+
// trackEvent(event);
16+
}
17+
}
18+
19+
export function CopyButton({
20+
value,
21+
className,
22+
src,
23+
...props
24+
}: ICopyButtonProps) {
25+
const [hasCopied, setHasCopied] = React.useState(false);
26+
27+
React.useEffect(() => {
28+
setTimeout(() => {
29+
setHasCopied(false);
30+
}, 2000);
31+
}, [hasCopied]);
32+
33+
return (
34+
<Button
35+
size="sm"
36+
className={cn(
37+
"relative z-10 h-6 w-6 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50 [&_svg]:h-3 [&_svg]:w-3",
38+
className
39+
)}
40+
onClick={() => {
41+
copyToClipboardWithMeta(value);
42+
setHasCopied(true);
43+
}}
44+
{...props}
45+
>
46+
{hasCopied ? "Copied" : "Copy"}
47+
</Button>
48+
);
49+
}

content/docs/components/button.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Button
33
description: This bold button makes sure your users click on it and perform the actions you want! 🚀
4-
lastUpdated: 12 Oct, 2024
4+
lastUpdated: 13 Oct, 2024
55
---
66

77
<ComponentShowcase name="button-style-default" />

packages/ui/Buttons/Button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const buttonVariants = cva("font-head transition-all", {
2323
},
2424
});
2525

26-
interface ButtonProps
26+
export interface IButtonProps
2727
extends ButtonHTMLAttributes<HTMLButtonElement>,
2828
VariantProps<typeof buttonVariants> {}
2929

@@ -33,7 +33,7 @@ export function Button({
3333
className = "",
3434
variant = "default",
3535
...props
36-
}: ButtonProps) {
36+
}: IButtonProps) {
3737
return (
3838
<button
3939
className={cn(buttonVariants({ variant, size }), className)}

0 commit comments

Comments
 (0)