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 link button to share component #86

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
156 changes: 78 additions & 78 deletions app/selects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,91 @@ import PageHeader from "@/demo/page-header";
import type { Metadata } from "next";

export const metadata: Metadata = {
title: "Select Components - Origin UI",
description:
"A collection of beautiful and accessible select components built with Tailwind CSS and Next.js.",
title: "Select Components - Origin UI",
description:
"A collection of beautiful and accessible select components built with Tailwind CSS and Next.js.",
};

const directory = "selects";
const files = [
"select-01",
"select-02",
"select-03",
"select-04",
"select-05",
"select-06",
"select-07",
"select-08",
"select-09",
"select-10",
"select-11",
"select-12",
"select-13",
"select-14",
"select-15",
"select-16",
"select-17",
"select-18",
"select-19",
"select-20",
"select-21",
"select-22",
"select-23",
"select-24",
"select-25",
"select-26",
"select-27",
"select-28",
"select-29",
"select-30",
"select-31",
"select-32",
"select-33",
"select-34",
"select-35",
"select-36",
"select-37",
"select-38",
"select-39",
"select-40",
"select-41",
"select-42",
"select-43",
"select-44",
"select-45",
"select-46",
"select-47",
"select-48",
"select-49",
"select-50",
"select-51",
"select-01",
"select-02",
"select-03",
"select-04",
"select-05",
"select-06",
"select-07",
"select-08",
"select-09",
"select-10",
"select-11",
"select-12",
"select-13",
"select-14",
"select-15",
"select-16",
"select-17",
"select-18",
"select-19",
"select-20",
"select-21",
"select-22",
"select-23",
"select-24",
"select-25",
"select-26",
"select-27",
"select-28",
"select-29",
"select-30",
"select-31",
"select-32",
"select-33",
"select-34",
"select-35",
"select-36",
"select-37",
"select-38",
"select-39",
"select-40",
"select-41",
"select-42",
"select-43",
"select-44",
"select-45",
"select-46",
"select-47",
"select-48",
"select-49",
"select-50",
"select-51",
];

export default function Page() {
return (
<main>
<div className="px-4 sm:px-6">
<div className="mx-auto w-full max-w-6xl">
<PageHeader title="Select">
A growing collection of {files.length} select components built with Next.js and
TailwindCSS.
</PageHeader>
return (
<main>
<div className="px-4 sm:px-6">
<div className="mx-auto w-full max-w-6xl">
<PageHeader title="Select">
A growing collection of {files.length} select components built with
Next.js and TailwindCSS.
</PageHeader>

<div className="grid max-w-6xl grid-cols-1 overflow-hidden sm:grid-cols-2 lg:grid-cols-3 [&>*]:relative [&>*]:px-1 [&>*]:py-12 [&>*]:before:absolute [&>*]:before:bg-border/70 [&>*]:before:[block-size:100vh] [&>*]:before:[inline-size:1px] [&>*]:before:[inset-block-start:0] [&>*]:before:[inset-inline-start:-1px] [&>*]:after:absolute [&>*]:after:bg-border/70 [&>*]:after:[block-size:1px] [&>*]:after:[inline-size:100vw] [&>*]:after:[inset-block-start:-1px] [&>*]:after:[inset-inline-start:0] sm:[&>*]:px-8 xl:[&>*]:px-12">
{files.map((componentName) => {
return (
<DemoComponent
key={componentName}
directory={directory}
componentName={componentName}
/>
);
})}
</div>
<div className="grid max-w-6xl grid-cols-1 overflow-hidden sm:grid-cols-2 lg:grid-cols-3 [&>*]:relative [&>*]:px-1 [&>*]:py-12 [&>*]:before:absolute [&>*]:before:bg-border/70 [&>*]:before:[block-size:100vh] [&>*]:before:[inline-size:1px] [&>*]:before:[inset-block-start:0] [&>*]:before:[inset-inline-start:-1px] [&>*]:after:absolute [&>*]:after:bg-border/70 [&>*]:after:[block-size:1px] [&>*]:after:[inline-size:100vw] [&>*]:after:[inset-block-start:-1px] [&>*]:after:[inset-inline-start:0] sm:[&>*]:px-8 xl:[&>*]:px-12">
{files.map((componentName) => {
return (
<DemoComponent
key={componentName}
directory={directory}
componentName={componentName}
/>
);
})}
</div>

<Cta />
</div>
</div>
</main>
);
<Cta />
</div>
</div>
</main>
);
}
59 changes: 59 additions & 0 deletions demo/card-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use client";
import { cn } from "@/lib/utils";
import LinkButton from "./link-button";
import CopyButton from "./copy-button";
import { useEffect, useRef } from "react";
import { useState } from "react";
import NameComponent from "./name-component";

const CardComponent = ({
componentName,
directory,
className,
children,
source,
}: {
componentName: string;
directory: string;
className?: string;
children: React.ReactNode;
source: string;
}) => {
const ref = useRef<HTMLDivElement>(null);
const [selected, setSelected] = useState(false);

useEffect(() => {
const isSelected =
typeof window !== "undefined" &&
window.location.hash === `#${componentName}`;

if (isSelected) {
setTimeout(() => {
window.scrollTo({
top: (ref.current?.offsetTop || 0) - window.innerHeight / 2 + 160,
behavior: "smooth",
});
setSelected(isSelected);
}, 300);
}
}, [componentName]);

return (
<div
ref={ref}
data-selected={selected}
className={cn(
"group/item relative hover",
selected && "bg-zinc-50 border-x-2 border-zinc-200",
className,
)}
>
{children}
<NameComponent componentName={componentName} isSelected={selected} />
<LinkButton componentName={componentName} directory={directory} />
<CopyButton componentSource={source || ""} />
</div>
);
};

export default CardComponent;
141 changes: 66 additions & 75 deletions demo/copy-button.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,78 @@
"use client";

import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import { Check, Code, Copy } from "lucide-react";
import { useState } from "react";

const CopyButton = ({ componentSource }: { componentSource: string }) => {
const [copied, setCopied] = useState<boolean>(false);
const [copied, setCopied] = useState<boolean>(false);

const handleCopy = async () => {
try {
await navigator.clipboard.writeText(componentSource);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
} catch (err) {
console.error("Failed to copy text: ", err);
}
};
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(componentSource);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
} catch (err) {
console.error("Failed to copy text: ", err);
}
};

return (
<div
className={cn(
"absolute right-2 top-2 transition-opacity",
!copied &&
"lg:opacity-0 lg:group-focus-within/item:opacity-100 lg:group-hover/item:opacity-100",
)}
>
<TooltipProvider delayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="text-muted-foreground/80 hover:bg-transparent hover:text-foreground disabled:opacity-100"
onClick={handleCopy}
aria-label={copied ? "Copied" : "Copy component source"}
disabled={copied}
>
<div
className={cn(
"transition-all",
copied ? "scale-100 opacity-100" : "scale-0 opacity-0",
)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="none"
aria-hidden="true"
>
<path
fill="#10B981"
d="M14.548 3.488a.75.75 0 0 1-.036 1.06l-8.572 8a.75.75 0 0 1-1.023 0l-3.429-3.2a.75.75 0 0 1 1.024-1.096l2.917 2.722 8.06-7.522a.75.75 0 0 1 1.06.036Z"
/>
</svg>
</div>
<div
className={cn(
"absolute transition-all",
copied ? "scale-0 opacity-0" : "scale-100 opacity-100",
)}
>
<svg
className="fill-current"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="none"
aria-hidden="true"
>
<path d="M3 2.5h7a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-.5.5H3a.5.5 0 0 1-.5-.5V3a.5.5 0 0 1 .5-.5ZM10 1H3a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h7a2 2 0 0 0 2-2V3a2 2 0 0 0-2-2Zm3 5.5h1a.5.5 0 0 1 .5.5v7a.5.5 0 0 1-.5.5H7a.5.5 0 0 1-.5-.5v-1H5v1a2 2 0 0 0 2 2h7a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-1v1.5Z" />
</svg>
</div>
</Button>
</TooltipTrigger>
<TooltipContent className="border border-input bg-popover px-2 py-1 text-xs text-muted-foreground">
Copy
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
);
return (
<div
className={cn(
"absolute right-2 top-2 transition-opacity",
!copied &&
"lg:opacity-0 lg:group-focus-within/item:opacity-100 lg:group-hover/item:opacity-100",
)}
>
<TooltipProvider delayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon"
className="text-muted-foreground/80 hover:bg-transparent hover:text-foreground disabled:opacity-100"
onClick={handleCopy}
aria-label={copied ? "Copied" : "Copy component source"}
disabled={copied}
>
<div
className={cn(
"transition-all",
copied ? "scale-100 opacity-100" : "scale-0 opacity-0",
)}
>
<Check
size={16}
strokeWidth={2}
aria-hidden="true"
className="text-green-500"
/>
</div>
<div
className={cn(
"absolute transition-all",
copied ? "scale-0 opacity-0" : "scale-100 opacity-100",
)}
>
<Copy size={16} strokeWidth={2} aria-hidden="true" />
</div>
</Button>
</TooltipTrigger>
<TooltipContent className="border border-input bg-popover px-2 py-1 text-xs text-muted-foreground">
Copy Code
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
);
};

export default CopyButton;
Loading