Skip to content
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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 75 additions & 19 deletions src/components/patterns/pattern-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export default function PatternCard({
return (
<div className="group relative">
<div
className={`relative aspect-square rounded-xl sm:rounded-2xl overflow-hidden bg-background shadow-sm transition-all duration-300 ${activePattern === pattern.id
className={`relative aspect-square rounded-xl sm:rounded-2xl overflow-hidden bg-background shadow-sm transition-all duration-300 ${
activePattern === pattern.id
? "ring-2 ring-primary ring-offset-2"
: ""
} ${activeMobileCard === pattern.id
} ${
activeMobileCard === pattern.id
? "scale-[1.02] shadow-lg sm:scale-100"
: "hover:shadow-lg hover:scale-[1.02]"
}`}
}`}
onClick={() => handleCardInteraction(pattern.id)}
>
{/* Favorite Button with Star Icon */}
Expand All @@ -57,25 +59,27 @@ export default function PatternCard({
e.stopPropagation();
toggleFavourite(pattern.id);
}}
className={`absolute top-2 left-2 z-10 p-2 rounded-full backdrop-blur-md shadow-lg border transition-all cursor-pointer duration-200 hover:scale-110 group/star ${isFavourite(pattern.id)
className={`absolute top-2 left-2 z-10 p-2 rounded-full backdrop-blur-md shadow-lg border transition-all cursor-pointer duration-200 hover:scale-110 group/star ${
isFavourite(pattern.id)
? isPatternDark
? "bg-yellow-500/20 border-yellow-400/30 text-yellow-400"
: "bg-yellow-500/20 border-yellow-500/30 text-yellow-600"
: isPatternDark
? "bg-black/20 border-white/20 text-white hover:bg-black/30 hover:border-white/30"
: "bg-black/20 border-white/30 text-white hover:bg-black/30 hover:border-white/40"
}`}
}`}
title={
isFavourite(pattern.id)
? "Remove from favorites"
: "Add to favorites"
}
>
<Star
className={`h-4 w-4 transition-all duration-200 ${isFavourite(pattern.id)
className={`h-4 w-4 transition-all duration-200 ${
isFavourite(pattern.id)
? "fill-current scale-110"
: "group-hover/star:scale-110"
}`}
}`}
/>
</button>

Expand Down Expand Up @@ -116,23 +120,49 @@ export default function PatternCard({
size="sm"
onClick={(e) => {
e.stopPropagation();
copyToClipboard(pattern.code, pattern.id);
copyToClipboard(pattern);
}}
className={`flex-1 border-0 text-xs h-8 ${isCopied(pattern.id)
className={`flex-1 border-0 text-xs h-8 ${
isCopied(pattern.id, "react")
? "bg-gray-700 hover:bg-gray-800 text-white"
: "bg-gray-900/90 hover:bg-gray-900 text-white"
}`}
disabled={isCopied(pattern.id)}
}`}
disabled={isCopied(pattern.id, "react")}
>
{isCopied(pattern.id) ? (
{isCopied(pattern.id, "react") ? (
<>
<Check className="h-3 w-3 mr-1" />
Copied
</>
) : (
<>
<Copy className="h-3 w-3 mr-1" />
Copy
React
</>
)}
</Button>
<Button
size="sm"
onClick={(e) => {
e.stopPropagation();
copyToClipboard(pattern, true);
}}
className={`flex-1 border-0 text-xs h-8 ${
isCopied(pattern.id, "vanilla")
? "bg-gray-700 hover:bg-gray-800 text-white"
: "bg-gray-900/90 hover:bg-gray-900 text-white"
}`}
disabled={isCopied(pattern.id, "vanilla")}
>
{isCopied(pattern.id, "vanilla") ? (
<>
<Check className="h-3 w-3 mr-1" />
Copied
</>
) : (
<>
<Copy className="h-3 w-3 mr-1" />
Vanilla
</>
)}
</Button>
Expand Down Expand Up @@ -164,23 +194,49 @@ export default function PatternCard({
size="sm"
onClick={(e) => {
e.stopPropagation();
copyToClipboard(pattern.code, pattern.id);
copyToClipboard(pattern);
}}
className={`cursor-pointer shadow-xl backdrop-blur-md gap-1 border-0 transition-all duration-200 hover:scale-105 text-xs sm:text-sm px-3 py-2 h-auto w-full xs:w-auto ${
isCopied(pattern.id, "react")
? "bg-gray-700 hover:bg-gray-800 text-white border border-gray-500"
: "bg-gray-900/90 hover:bg-gray-900 text-white"
}`}
disabled={isCopied(pattern.id, "react")}
>
{isCopied(pattern.id, "react") ? (
<>
<Check className="h-3 w-3" />
Copied
</>
) : (
<>
<Copy className="h-3 w-3" />
React
</>
)}
</Button>
<Button
size="sm"
onClick={(e) => {
e.stopPropagation();
copyToClipboard(pattern, true);
}}
className={`cursor-pointer shadow-xl backdrop-blur-md gap-1 border-0 transition-all duration-200 hover:scale-105 text-xs sm:text-sm px-3 py-2 h-auto w-full xs:w-auto ${isCopied(pattern.id)
className={`cursor-pointer shadow-xl backdrop-blur-md gap-1 border-0 transition-all duration-200 hover:scale-105 text-xs sm:text-sm px-3 py-2 h-auto w-full xs:w-auto ${
isCopied(pattern.id, "vanilla")
? "bg-gray-700 hover:bg-gray-800 text-white border border-gray-500"
: "bg-gray-900/90 hover:bg-gray-900 text-white"
}`}
disabled={isCopied(pattern.id)}
}`}
disabled={isCopied(pattern.id, "vanilla")}
>
{isCopied(pattern.id) ? (
{isCopied(pattern.id, "vanilla") ? (
<>
<Check className="h-3 w-3" />
Copied
</>
) : (
<>
<Copy className="h-3 w-3" />
Copy
Vanilla
</>
)}
</Button>
Expand Down
52 changes: 43 additions & 9 deletions src/hooks/useCopy.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
"use client";

import { Pattern } from "@/types";
import { useState } from "react";
import { toast } from "sonner";

interface CopiedState {
id: string;
type: "react" | "vanilla";
}

export function useCopy() {
const [copiedId, setCopiedId] = useState<string | null>(null);
const [copiedState, setCopiedState] = useState<CopiedState | null>(null);

const copyToClipboard = async (code: string, id: string) => {
const copyToClipboard = async (pattern: Pattern, vanilla = false) => {
try {
await navigator.clipboard.writeText(code);
setCopiedId(id);
toast.success("Code copied to clipboard!");
setTimeout(() => setCopiedId(null), 1000);
} catch (error) {
console.error("Failed to copy to clipboard:", error);
let textToCopy = pattern.code;

if (vanilla) {
const el = document.createElement("div");
Object.assign(el.style, {
minHeight: "100vh",
width: "100%",
position: "relative",
});
const containerStyle = el.getAttribute("style") || "";

const bg = document.createElement("div");
Object.assign(bg.style, {
position: "absolute",
inset: "0",
zIndex: "0",
...pattern.style,
});
const bgStyle = bg.getAttribute("style") || "";

textToCopy = `
<div style="${containerStyle}">
<div style="${bgStyle}"></div>
</div>
`.trim();
}

await navigator.clipboard.writeText(textToCopy);
setCopiedState({ id: pattern.id, type: vanilla ? "vanilla" : "react" });
toast.success("Code copied!");
setTimeout(() => setCopiedState(null), 1000);
} catch (err) {
console.error(err);
toast.error("Failed to copy code");
}
};

const isCopied = (id: string) => copiedId === id;
const isCopied = (id: string, type: "react" | "vanilla" = "react") =>
copiedState?.id === id && copiedState?.type === type;

return {
copyToClipboard,
Expand Down