1"use client";
2
3import { XIcon } from "lucide-react";
4import {
5 type ComponentProps,
6 createContext,
7 type MouseEventHandler,
8 type ReactNode,
9 useContext,
10 useEffect,
11 useRef,
12 useState,
13} from "react";
14import { Badge } from "@/components/ui/badge";
15import { Button } from "@/components/ui/button";
16import {
17 Command,
18 CommandEmpty,
19 CommandGroup,
20 CommandInput,
21 CommandItem,
22 CommandList,
23} from "@/components/ui/command";
24import {
25 Popover,
26 PopoverContent,
27 PopoverTrigger,
28} from "@/components/ui/popover";
29import { cn } from "@/lib/utils";
30
31type TagsContextType = {
32 value?: string;
33 setValue?: (value: string) => void;
34 open: boolean;
35 onOpenChange: (open: boolean) => void;
36 width?: number;
37 setWidth?: (width: number) => void;
38};
39
40const TagsContext = createContext<TagsContextType>({
41 value: undefined,
42 setValue: undefined,
43 open: false,
44 onOpenChange: () => {},
45 width: undefined,
46 setWidth: undefined,
47});
48
49const useTagsContext = () => {
50 const context = useContext(TagsContext);
51
52 if (!context) {
53 throw new Error("useTagsContext must be used within a TagsProvider");
54 }
55
56 return context;
57};
58
59export type TagsProps = {
60 value?: string;
61 setValue?: (value: string) => void;
62 open?: boolean;
63 onOpenChange?: (open: boolean) => void;
64 children?: ReactNode;
65 className?: string;
66};
67
68export const Tags = ({
69 value,
70 setValue,
71 open: controlledOpen,
72 onOpenChange: controlledOnOpenChange,
73 children,
74 className,
75}: TagsProps) => {
76 const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
77 const [width, setWidth] = useState<number>();
78 const ref = useRef<HTMLDivElement>(null);
79
80 const open = controlledOpen ?? uncontrolledOpen;
81 const onOpenChange = controlledOnOpenChange ?? setUncontrolledOpen;
82
83 useEffect(() => {
84 if (!ref.current) {
85 return;
86 }
87
88 const resizeObserver = new ResizeObserver((entries) => {
89 setWidth(entries[0].contentRect.width);
90 });
91
92 resizeObserver.observe(ref.current);
93
94 return () => {
95 resizeObserver.disconnect();
96 };
97 }, []);
98
99 return (
100 <TagsContext.Provider
101 value={{ value, setValue, open, onOpenChange, width, setWidth }}
102 >
103 <Popover onOpenChange={onOpenChange} open={open}>
104 <div className={cn("relative w-full", className)} ref={ref}>
105 {children}
106 </div>
107 </Popover>
108 </TagsContext.Provider>
109 );
110};
111
112export type TagsTriggerProps = ComponentProps<typeof Button>;
113
114export const TagsTrigger = ({
115 className,
116 children,
117 ...props
118}: TagsTriggerProps) => (
119 <PopoverTrigger asChild>
120 <Button
121 className={cn("h-auto w-full justify-between p-2", className)}
122 // biome-ignore lint/a11y/useSemanticElements: "Required"
123 role="combobox"
124 variant="outline"
125 {...props}
126 >
127 <div className="flex flex-wrap items-center gap-1">
128 {children}
129 <span className="px-2 py-px text-muted-foreground">
130 Select a tag...
131 </span>
132 </div>
133 </Button>
134 </PopoverTrigger>
135);
136
137export type TagsValueProps = ComponentProps<typeof Badge>;
138
139export const TagsValue = ({
140 className,
141 children,
142 onRemove,
143 ...props
144}: TagsValueProps & { onRemove?: () => void }) => {
145 const handleRemove: MouseEventHandler<HTMLDivElement> = (event) => {
146 event.preventDefault();
147 event.stopPropagation();
148 onRemove?.();
149 };
150
151 return (
152 <Badge className={cn("flex items-center gap-2", className)} {...props}>
153 {children}
154 {onRemove && (
155 // biome-ignore lint/a11y/noStaticElementInteractions: "This is a clickable badge"
156 // biome-ignore lint/a11y/useKeyWithClickEvents: "This is a clickable badge"
157 <div
158 className="size-auto cursor-pointer hover:text-muted-foreground"
159 onClick={handleRemove}
160 >
161 <XIcon size={12} />
162 </div>
163 )}
164 </Badge>
165 );
166};
167
168export type TagsContentProps = ComponentProps<typeof PopoverContent>;
169
170export const TagsContent = ({
171 className,
172 children,
173 ...props
174}: TagsContentProps) => {
175 const { width } = useTagsContext();
176
177 return (
178 <PopoverContent
179 className={cn("p-0", className)}
180 style={{ width }}
181 {...props}
182 >
183 <Command>{children}</Command>
184 </PopoverContent>
185 );
186};
187
188export type TagsInputProps = ComponentProps<typeof CommandInput>;
189
190export const TagsInput = ({ className, ...props }: TagsInputProps) => (
191 <CommandInput className={cn("h-9", className)} {...props} />
192);
193
194export type TagsListProps = ComponentProps<typeof CommandList>;
195
196export const TagsList = ({ className, ...props }: TagsListProps) => (
197 <CommandList className={cn("max-h-[200px]", className)} {...props} />
198);
199
200export type TagsEmptyProps = ComponentProps<typeof CommandEmpty>;
201
202export const TagsEmpty = ({
203 children,
204 className,
205 ...props
206}: TagsEmptyProps) => (
207 <CommandEmpty {...props}>{children ?? "No tags found."}</CommandEmpty>
208);
209
210export type TagsGroupProps = ComponentProps<typeof CommandGroup>;
211
212export const TagsGroup = CommandGroup;
213
214export type TagsItemProps = ComponentProps<typeof CommandItem>;
215
216export const TagsItem = ({ className, ...props }: TagsItemProps) => (
217 <CommandItem
218 className={cn("cursor-pointer items-center justify-between", className)}
219 {...props}
220 />
221);
222