"use client";
import { useState, useMemo } from "react";
import { Type, Search, Check } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { cn } from "@/lib/utils";
type Font = {
name: string;
family: string;
category: string;
weights: number[];
};
const fonts: Font[] = [
{ name: "Inter", family: "Inter, sans-serif", category: "sans", weights: [400, 500, 600, 700] },
{ name: "Roboto", family: "Roboto, sans-serif", category: "sans", weights: [300, 400, 500, 700] },
{ name: "Open Sans", family: "'Open Sans', sans-serif", category: "sans", weights: [400, 600, 700] },
{ name: "Lato", family: "Lato, sans-serif", category: "sans", weights: [300, 400, 700] },
{ name: "Poppins", family: "Poppins, sans-serif", category: "sans", weights: [400, 500, 600, 700] },
{ name: "Montserrat", family: "Montserrat, sans-serif", category: "sans", weights: [400, 500, 600, 700] },
{ name: "Playfair Display", family: "'Playfair Display', serif", category: "serif", weights: [400, 500, 600, 700] },
{ name: "Merriweather", family: "Merriweather, serif", category: "serif", weights: [300, 400, 700] },
{ name: "Lora", family: "Lora, serif", category: "serif", weights: [400, 500, 600, 700] },
{ name: "Georgia", family: "Georgia, serif", category: "serif", weights: [400, 700] },
{ name: "Bebas Neue", family: "'Bebas Neue', sans-serif", category: "display", weights: [400] },
{ name: "Oswald", family: "Oswald, sans-serif", category: "display", weights: [400, 500, 600, 700] },
{ name: "Archivo Black", family: "'Archivo Black', sans-serif", category: "display", weights: [400] },
{ name: "JetBrains Mono", family: "'JetBrains Mono', monospace", category: "mono", weights: [400, 500, 600, 700] },
{ name: "Fira Code", family: "'Fira Code', monospace", category: "mono", weights: [400, 500, 600, 700] },
{ name: "Source Code Pro", family: "'Source Code Pro', monospace", category: "mono", weights: [400, 500, 600, 700] },
];
export const title = "React Dialog Block Font Picker";
export default function DialogFontPicker() {
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
const [selectedFont, setSelectedFont] = useState<Font>(fonts[0]);
const filteredFonts = useMemo(() => {
return fonts.filter((font) =>
font.name.toLowerCase().includes(search.toLowerCase())
);
}, [search]);
const handleConfirm = () => {
console.log("Font selected:", selectedFont);
setOpen(false);
};
const handleClose = () => {
setOpen(false);
setTimeout(() => setSearch(""), 200);
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<div className="flex min-h-[350px] items-center justify-center">
<DialogTrigger asChild>
<Button variant="outline">
<Type className="mr-2 h-4 w-4" />
Choose Font
</Button>
</DialogTrigger>
</div>
<DialogContent className="sm:max-w-sm">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Type className="h-5 w-5" />
Font Picker
</DialogTitle>
<DialogDescription>
Select a font for your design.
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div
className="rounded-lg border p-4 text-center"
style={{ fontFamily: selectedFont.family }}
>
<p className="text-xl">
The quick brown fox jumps over the lazy dog
</p>
<p className="text-sm text-muted-foreground mt-2">
{selectedFont.name} · {selectedFont.category}
</p>
</div>
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search fonts..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="pl-9"
/>
</div>
<div className="h-[200px] overflow-y-auto space-y-1">
{filteredFonts.map((font) => (
<button
key={font.name}
onClick={() => setSelectedFont(font)}
className={cn(
"w-full flex items-center justify-between p-2.5 rounded-lg transition-colors text-left",
selectedFont.name === font.name
? "bg-primary text-primary-foreground"
: "hover:bg-muted"
)}
>
<span
className="text-sm"
style={{ fontFamily: font.family }}
>
{font.name}
</span>
{selectedFont.name === font.name && (
<Check className="h-4 w-4 shrink-0" />
)}
</button>
))}
{filteredFonts.length === 0 && (
<div className="flex flex-col items-center justify-center py-8 text-center">
<Type className="h-8 w-8 text-muted-foreground mb-2" />
<p className="text-sm text-muted-foreground">No fonts found</p>
</div>
)}
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button onClick={handleConfirm}>Select</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}