-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
IconPicker.tsx
155 lines (144 loc) · 4.53 KB
/
IconPicker.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, { useCallback, useMemo, useRef, useState } from "react";
import { IconType } from "react-icons";
import * as LuIcons from "react-icons/lu";
import { Input } from "@/components/ui/input";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { IoClose } from "react-icons/io5";
import Heading from "../ui/heading";
import { cn } from "@/lib/utils";
import { Button } from "../ui/button";
export type IconName = keyof typeof LuIcons;
export type IconElement = {
name?: string;
Icon?: IconType;
};
type IconPickerProps = {
selectedIcon?: IconElement;
setSelectedIcon?: React.Dispatch<
React.SetStateAction<IconElement | undefined>
>;
};
export default function IconPicker({
selectedIcon,
setSelectedIcon,
}: IconPickerProps) {
const [open, setOpen] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const [searchTerm, setSearchTerm] = useState("");
const iconSets = useMemo(() => [...Object.entries(LuIcons)], []);
const icons = useMemo(
() =>
iconSets.filter(
([name]) =>
name.toLowerCase().includes(searchTerm.toLowerCase()) ||
searchTerm === "",
),
[iconSets, searchTerm],
);
const handleIconSelect = useCallback(
({ name, Icon }: IconElement) => {
if (setSelectedIcon) {
setSelectedIcon({ name, Icon });
}
setSearchTerm("");
},
[setSelectedIcon],
);
return (
<div ref={containerRef}>
<Popover
open={open}
onOpenChange={(open) => {
setOpen(open);
}}
>
<PopoverTrigger asChild>
{!selectedIcon?.name || !selectedIcon?.Icon ? (
<Button className="mt-2 w-full text-muted-foreground">
Select an icon
</Button>
) : (
<div className="hover:cursor-pointer">
<div className="my-3 flex w-full flex-row items-center justify-between gap-2">
<div className="flex flex-row items-center gap-2">
<selectedIcon.Icon size={15} />
<div className="text-sm">
{selectedIcon.name
.replace(/^Lu/, "")
.replace(/([A-Z])/g, " $1")}
</div>
</div>
<IoClose
className="mx-2 hover:cursor-pointer"
onClick={() => {
handleIconSelect({ name: undefined, Icon: undefined });
}}
/>
</div>
</div>
)}
</PopoverTrigger>
<PopoverContent
align="start"
side="top"
container={containerRef.current}
className="flex max-h-[50dvh] flex-col overflow-y-hidden md:max-h-[30dvh]"
>
<div className="mb-3 flex flex-row items-center justify-between">
<Heading as="h4">Select an icon</Heading>
<span tabIndex={0} className="sr-only" />
<IoClose
size={15}
className="hover:cursor-pointer"
onClick={() => {
setOpen(false);
}}
/>
</div>
<Input
type="text"
placeholder="Search for an icon..."
className="text-md mb-3 md:text-sm"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
<div className="scrollbar-container flex h-full flex-col overflow-y-auto">
<div className="grid grid-cols-6 gap-2 pr-1">
{icons.map(([name, Icon]) => (
<div
key={name}
className={cn(
"flex flex-row items-center justify-center rounded-lg p-1 hover:cursor-pointer",
selectedIcon?.name === name
? "bg-selected text-white"
: "hover:bg-secondary-foreground",
)}
>
<Icon
className="size-6"
onClick={() => {
handleIconSelect({ name, Icon });
setOpen(false);
}}
/>
</div>
))}
</div>
</div>
</PopoverContent>
</Popover>
</div>
);
}
type IconRendererProps = {
icon: IconType;
size?: number;
className?: string;
};
export function IconRenderer({ icon, size, className }: IconRendererProps) {
return <>{React.createElement(icon, { size, className })}</>;
}