|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import { Search, Check, X } from "lucide-react"; |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogContent, |
| 8 | + DialogHeader, |
| 9 | + DialogTitle, |
| 10 | + DialogTrigger, |
| 11 | +} from "@/components/ui/dialog"; |
| 12 | +import { Input } from "@/components/ui/input"; |
| 13 | +import { ScrollArea } from "@/components/ui/scroll-area"; |
| 14 | +import { cn } from "@/lib/utils"; |
| 15 | +import { icons} from "lucide-react"; |
| 16 | +import type { LucideIcon } from "lucide-react"; |
| 17 | +import { Button, buttonVariants } from "@/components/ui/button"; |
| 18 | +import { useVirtualizer, type VirtualItem } from "@tanstack/react-virtual"; |
| 19 | +import { Suspense, useEffect } from "react"; |
| 20 | + |
| 21 | +interface IconPickerDialogProps { |
| 22 | + onSelect: (iconName: string) => void; |
| 23 | + selectedIcon?: string; |
| 24 | +} |
| 25 | + |
| 26 | +const IconListGrid = ({ |
| 27 | + search, |
| 28 | + onSelect, |
| 29 | + selectedIcon, |
| 30 | + setOpen, |
| 31 | +}: { |
| 32 | + search: string; |
| 33 | + onSelect: (iconName: string) => void; |
| 34 | + selectedIcon?: string; |
| 35 | + setOpen: (open: boolean) => void; |
| 36 | +}) => { |
| 37 | + const IconsKeyList = Object.keys(icons); |
| 38 | + const filterIcons = (search: string) => { |
| 39 | + if (search === "") { |
| 40 | + return IconsKeyList; |
| 41 | + } |
| 42 | + return IconsKeyList.filter((iconName) => |
| 43 | + iconName.toLowerCase().includes(search.toLowerCase()) |
| 44 | + ); |
| 45 | + }; |
| 46 | + |
| 47 | + const parentRef = React.useRef<HTMLDivElement>(null); |
| 48 | + |
| 49 | + const [hoveredIcon, setHoveredIcon] = React.useState<string | null>(null); |
| 50 | + const filteredIcons = filterIcons(search); |
| 51 | + const rowVirtualizer = useVirtualizer({ |
| 52 | + count: Math.ceil(filteredIcons.length / 6), |
| 53 | + getScrollElement: () => parentRef.current, |
| 54 | + estimateSize: () => 72, // height of each row (48px) + gap (8px) |
| 55 | + overscan: 5, |
| 56 | + }); |
| 57 | + |
| 58 | + const rowItems = rowVirtualizer.getVirtualItems(); |
| 59 | + |
| 60 | + return ( |
| 61 | + <div |
| 62 | + ref={parentRef} |
| 63 | + className="h-[400px] overflow-auto relative scrollbar-hide" |
| 64 | + style={{ |
| 65 | + contain: "strict", |
| 66 | + }} |
| 67 | + > |
| 68 | + <div |
| 69 | + style={{ |
| 70 | + height: `${rowVirtualizer.getTotalSize()}px`, |
| 71 | + width: "100%", |
| 72 | + position: "relative", |
| 73 | + }} |
| 74 | + > |
| 75 | + {rowItems.map((virtualRow: VirtualItem) => { |
| 76 | + const startIndex = virtualRow.index * 6; |
| 77 | + const rowIcons = filteredIcons.slice(startIndex, startIndex + 6); |
| 78 | + |
| 79 | + return ( |
| 80 | + <div |
| 81 | + key={virtualRow.index} |
| 82 | + className="absolute top-0 left-0 w-full grid grid-cols-6 gap-2" |
| 83 | + style={{ |
| 84 | + transform: `translateY(${virtualRow.start}px)`, |
| 85 | + }} |
| 86 | + > |
| 87 | + {rowIcons.length > 0 && |
| 88 | + rowIcons.map((iconName) => { |
| 89 | + const Icon = icons[ |
| 90 | + iconName as keyof typeof icons |
| 91 | + ] as LucideIcon; |
| 92 | + const isSelected = selectedIcon === iconName; |
| 93 | + const isHovered = hoveredIcon === iconName; |
| 94 | + |
| 95 | + if (!Icon) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + return ( |
| 100 | + <Button |
| 101 | + variant="outline" |
| 102 | + key={iconName} |
| 103 | + className={cn( |
| 104 | + "h-16 w-16", |
| 105 | + isSelected && "bg-accent text-accent-foreground border-primary", |
| 106 | + isHovered && "bg-accent/50" |
| 107 | + )} |
| 108 | + onClick={() => { |
| 109 | + onSelect(iconName); |
| 110 | + setOpen(false); |
| 111 | + }} |
| 112 | + onMouseEnter={() => setHoveredIcon(iconName)} |
| 113 | + onMouseLeave={() => setHoveredIcon(null)} |
| 114 | + > |
| 115 | + <Icon className="size-8 text-primary" strokeWidth={isSelected ? 1.5 : 1} /> |
| 116 | + </Button> |
| 117 | + ); |
| 118 | + })} |
| 119 | + </div> |
| 120 | + ); |
| 121 | + })} |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +}; |
| 126 | +export function IconPickerDialog({ |
| 127 | + onSelect, |
| 128 | + selectedIcon, |
| 129 | +}: IconPickerDialogProps) { |
| 130 | + const [open, setOpen] = React.useState(false); |
| 131 | + const [search, setSearch] = React.useState(""); |
| 132 | + const [selectedIconText, setSelectedIconText] = React.useState<string>(); |
| 133 | + const [selectedIconIcon, setSelectedIconIcon] = React.useState<LucideIcon>(); |
| 134 | + |
| 135 | + useEffect(() => { |
| 136 | + setSelectedIconText(selectedIcon || ""); |
| 137 | + setSelectedIconIcon(icons[selectedIcon as keyof typeof icons] as LucideIcon); |
| 138 | + }, [selectedIcon]); |
| 139 | + |
| 140 | + const onIconSelect = (iconName: string) => { |
| 141 | + setSelectedIconText(iconName); |
| 142 | + setSelectedIconIcon(icons[iconName as keyof typeof icons] as LucideIcon); |
| 143 | + onSelect(iconName); |
| 144 | + setOpen(false); |
| 145 | + }; |
| 146 | + |
| 147 | + const onIconDeselect = () => { |
| 148 | + setSelectedIconText(""); |
| 149 | + setSelectedIconIcon(undefined); |
| 150 | + onSelect(""); |
| 151 | + setOpen(false); |
| 152 | + }; |
| 153 | + |
| 154 | + const Icon = selectedIconIcon ? selectedIconIcon : null; |
| 155 | + |
| 156 | + return ( |
| 157 | + <Dialog open={open} onOpenChange={setOpen}> |
| 158 | + <div |
| 159 | + className={cn( |
| 160 | + "flex flex-row items-center gap-2 w-full", |
| 161 | + buttonVariants({ variant: "outline", size: "sm" }), |
| 162 | + "text-sm font-normal text-left" |
| 163 | + )} |
| 164 | + > |
| 165 | + <DialogTrigger asChild> |
| 166 | + <div className="flex items-center justify-between w-full"> |
| 167 | + {Icon ? <Icon className="size-4" /> : "Pick an Icon"} |
| 168 | + </div> |
| 169 | + </DialogTrigger> |
| 170 | + {selectedIconText && ( |
| 171 | + <div className="flex items-center justify-center" onClick={onIconDeselect}> |
| 172 | + <X className="size-4 text-muted-foreground opacity-50"/> |
| 173 | + </div> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + <DialogContent className="sm:max-w-[480px]"> |
| 177 | + <DialogHeader> |
| 178 | + <DialogTitle>Pick an Icon</DialogTitle> |
| 179 | + </DialogHeader> |
| 180 | + <div className="relative"> |
| 181 | + <Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" /> |
| 182 | + <Input |
| 183 | + placeholder="Search icons..." |
| 184 | + className="pl-8" |
| 185 | + value={search} |
| 186 | + onChange={(e) => setSearch(e.target.value)} |
| 187 | + /> |
| 188 | + </div> |
| 189 | + <Suspense fallback={<div>Loading...</div>}> |
| 190 | + <IconListGrid |
| 191 | + search={search} |
| 192 | + onSelect={onIconSelect} |
| 193 | + setOpen={setOpen} |
| 194 | + selectedIcon={selectedIcon} |
| 195 | + /> |
| 196 | + <div |
| 197 | + className={cn( |
| 198 | + "absolute inset-x-0 bottom-6 h-12 z-10 bg-gradient-from-transparent bg-gradient-to-t from-white to-transparent" |
| 199 | + )} |
| 200 | + /> |
| 201 | + </Suspense> |
| 202 | + </DialogContent> |
| 203 | + </Dialog> |
| 204 | + ); |
| 205 | +} |
0 commit comments