Open History to see the last commit
1import type { ProjectIconColor, ProjectIconOverride } from "@t3tools/contracts";
2import { DynamicIcon, type IconName } from "lucide-react/dynamic";
3import { useEffect, useMemo, useRef, useState } from "react";
4import {
5 filterProjectIconNames,
6 firstEmoji,
7 PROJECT_EMOJIS,
8 PROJECT_ICON_COLORS,
9 projectIconColorClassName,
10} from "../../projectIconOptions";
11import { cn } from "~/lib/utils";
12import { Button } from "../ui/button";
13import {
14 Dialog,
15 DialogDescription,
16 DialogFooter,
17 DialogHeader,
18 DialogPanel,
19 DialogPopup,
20 DialogTitle,
21} from "../ui/dialog";
22import { Input } from "../ui/input";
23import { ScrollArea } from "../ui/scroll-area";
24import { Toggle, ToggleGroup } from "../ui/toggle-group";
25β
26const DEFAULT_ICON: IconName = "folder-code";
27const DEFAULT_COLOR: ProjectIconColor = "blue";
28β
29function iconLabel(name: string): string {
30 return name
31 .split("-")
32 .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
33 .join(" ");
34}
35β
36export function ProjectIconPickerDialog({
37 current,
38 open,
39 onOpenChange,
40 onSelect,
41}: {
42 readonly current: ProjectIconOverride | null;
43 readonly open: boolean;
44 readonly onOpenChange: (open: boolean) => void;
45 readonly onSelect: (icon: ProjectIconOverride) => void;
46}) {
47 const [mode, setMode] = useState<"lucide" | "emoji">(
48 current?.kind === "lucide" ? "lucide" : "emoji",
49 );
50 const [iconName, setIconName] = useState<IconName>(
51 current?.kind === "lucide" ? (current.name as IconName) : DEFAULT_ICON,
52 );
53 const [color, setColor] = useState<ProjectIconColor>(
54 current?.kind === "lucide" ? current.color : DEFAULT_COLOR,
55 );
56 const [emoji, setEmoji] = useState(current?.kind === "emoji" ? current.emoji : "π»");
57 const [query, setQuery] = useState("");
58 const [customEmoji, setCustomEmoji] = useState("");
59 const previousOpenRef = useRef(false);
60β
61 useEffect(() => {
62 if (open && !previousOpenRef.current) {
63 setMode(current?.kind === "lucide" ? "lucide" : "emoji");
64 setIconName(current?.kind === "lucide" ? (current.name as IconName) : DEFAULT_ICON);
65 setColor(current?.kind === "lucide" ? current.color : DEFAULT_COLOR);
66 setEmoji(current?.kind === "emoji" ? current.emoji : "π»");
67 setQuery("");
68 setCustomEmoji("");
69 }
70 previousOpenRef.current = open;
71 }, [current, open]);
72β
73 const icons = useMemo(() => filterProjectIconNames(query), [query]);
74 const selectedColorClassName = projectIconColorClassName(color);
75 const save = () => {
76 onSelect(
77 mode === "lucide" ? { kind: "lucide", name: iconName, color } : { kind: "emoji", emoji },
78 );
79 onOpenChange(false);
80 };
81β
82 return (
83 <Dialog open={open} onOpenChange={onOpenChange}>
84 <DialogPopup className="w-full sm:w-[32rem]">
85 <DialogHeader>
86 <DialogTitle>Choose project icon</DialogTitle>
87 <DialogDescription>Pick an emoji, or choose any Lucide icon and color.</DialogDescription>
88 </DialogHeader>
89 <DialogPanel className="flex min-h-0 flex-col gap-4">
90 <ToggleGroup
91 aria-label="Icon type"
92 variant="segmented"
93 value={[mode]}
94 onValueChange={(next) => {
95 const value = next[0];
96 if (value === "lucide" || value === "emoji") setMode(value);
97 }}
98 >
99 <Toggle value="emoji">Emoji</Toggle>
100 <Toggle value="lucide">Icons</Toggle>
101 </ToggleGroup>
102β
103 {mode === "lucide" ? (
104 <>
105 <div>
106 <div className="mb-2 text-xs font-medium text-muted-foreground">Color</div>
107 <div className="flex flex-wrap gap-1.5" role="group" aria-label="Icon color">
108 {PROJECT_ICON_COLORS.map((option) => (
109 <button
110 key={option.value}
111 type="button"
112 aria-label={option.label}
113 aria-pressed={color === option.value}
114 className={cn(
115 "flex size-6 items-center justify-center rounded-full border border-transparent outline-none focus-visible:ring-2 focus-visible:ring-ring",
116 color === option.value && "border-foreground/64",
117 )}
118 onClick={() => setColor(option.value)}
119 >
120 <span className={cn("size-4 rounded-full", option.swatchClassName)} />
121 </button>
122 ))}
123 </div>
124 </div>
125 <Input
126 type="search"
127 value={query}
128 aria-label="Search Lucide icons"
129 placeholder="Search all Lucide icons"
130 onChange={(event) => setQuery(event.currentTarget.value)}
131 />
132 <ScrollArea scrollFade className="max-h-64">
133 <div className="grid grid-cols-8 gap-1 p-0.5 sm:grid-cols-10">
134 {icons.map((name) => (
135 <button
136 key={name}
137 type="button"
138 aria-label={iconLabel(name)}
139 aria-pressed={iconName === name}
140 className={cn(
141 "flex aspect-square items-center justify-center rounded-md border border-transparent outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring",
142 iconName === name && "border-border bg-accent",
143 selectedColorClassName,
144 )}
145 onClick={() => setIconName(name)}
146 >
147 <DynamicIcon name={name} className="size-5" />
148 </button>
149 ))}
150 </div>
151 </ScrollArea>
152 {icons.length === 0 ? (
153 <p className="py-8 text-center text-sm text-muted-foreground">No icons found.</p>
154 ) : null}
155 </>
156 ) : (
157 <>
158 <ScrollArea scrollFade className="max-h-64">
159 <div className="grid grid-cols-8 gap-1 p-0.5 sm:grid-cols-10">
160 {PROJECT_EMOJIS.map((option) => (
161 <button
162 key={option.emoji}
163 type="button"
164 aria-label={option.label}
165 aria-pressed={emoji === option.emoji}
166 className={cn(
167 "flex aspect-square items-center justify-center rounded-md border border-transparent text-xl outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring",
168 emoji === option.emoji && "border-border bg-accent",
169 )}
170 onClick={() => setEmoji(option.emoji)}
171 >
172 {option.emoji}
173 </button>
174 ))}
175 </div>
176 </ScrollArea>
177 <div>
178 <div className="mb-2 text-xs font-medium text-muted-foreground">
179 Or paste any emoji
180 </div>
181 <Input
182 value={customEmoji}
183 aria-label="Custom emoji"
184 placeholder="Paste an emoji"
185 onChange={(event) => {
186 const value = event.currentTarget.value;
187 setCustomEmoji(value);
188 const nextEmoji = firstEmoji(value);
189 if (nextEmoji) setEmoji(nextEmoji);
190 }}
191 />
192 </div>
193 </>
194 )}
195 </DialogPanel>
196 <DialogFooter>
197 <Button variant="outline" onClick={() => onOpenChange(false)}>
198 Cancel
199 </Button>
200 <Button onClick={save}>Save icon</Button>
201 </DialogFooter>
202 </DialogPopup>
203 </Dialog>
204 );
205}