1"use client";2
3import React, { useCallback, useId, useState } from "react";4import { AnimatePresence, LayoutGroup, motion, useReducedMotion } from "motion/react";5import { X } from "lucide-react";6import { cn } from "@/lib/utils";7
8export type FilterBarOption = {9 id: string;10 label: string;11 12 count?: number;13 icon?: React.ReactNode;14};15
16export type FilterBarProps = {17 options: FilterBarOption[];18 value?: string[];19 defaultValue?: string[];20 onChange?: (value: string[]) => void;21 22 multi?: boolean;23 24 clearable?: boolean;25 className?: string;26 "aria-label"?: string;27};28
29const SPRING = { type: "spring" as const, bounce: 0.18, duration: 0.42 };30
31function Count({ value, on }: { value: number; on: boolean }) {32 return (33 <span34 className={cn(35 "relative inline-flex min-w-[1.25rem] justify-center overflow-hidden rounded-full px-1.5 text-[11px] leading-[18px] font-medium tabular-nums transition-colors duration-200",36 on ? "bg-(--color-paper)/15 text-(--color-paper)" : "bg-(--color-paper-2) text-(--color-neutral)",37 )}38 >39 <AnimatePresence initial={false} mode="popLayout">40 <motion.span41 key={value}42 initial={{ y: 8, opacity: 0 }}43 animate={{ y: 0, opacity: 1 }}44 exit={{ y: -8, opacity: 0 }}45 transition={{ type: "spring", bounce: 0, duration: 0.3 }}46 >47 {value}48 </motion.span>49 </AnimatePresence>50 </span>51 );52}53
54export function FilterBar({55 options,56 value: valueProp,57 defaultValue = [],58 onChange,59 multi = true,60 clearable = true,61 className,62 "aria-label": ariaLabel = "Filters",63}: FilterBarProps) {64 const reduce = !!useReducedMotion();65 const uid = useId();66 const isControlled = valueProp !== undefined;67 const [uncontrolled, setUncontrolled] = useState(defaultValue);68 const selected = isControlled ? valueProp : uncontrolled;69 const [hovered, setHovered] = useState<string | null>(null);70 const spring = reduce ? { duration: 0 } : SPRING;71
72 const setSelected = useCallback(73 (next: string[]) => {74 if (!isControlled) setUncontrolled(next);75 onChange?.(next);76 },77 [isControlled, onChange],78 );79
80 const toggle = (id: string) => {81 if (multi) {82 83 const next = selected.includes(id) ? selected.filter((x) => x !== id) : [...selected, id];84 setSelected(options.map((o) => o.id).filter((o) => next.includes(o)));85 } else {86 setSelected(selected.includes(id) ? [] : [id]);87 }88 };89
90 91
92 if (!multi) {93 return (94 <LayoutGroup id={uid}>95 <div96 role="group"97 aria-label={ariaLabel}98 onPointerLeave={() => setHovered(null)}99 className={cn(100 "relative isolate inline-flex w-fit max-w-full flex-wrap items-center gap-0.5 rounded-full p-1",101 "bg-(--color-paper-2) shadow-[inset_0_0_0_1px_var(--color-rule)]",102 className,103 )}104 >105 {options.map((opt) => {106 const on = selected.includes(opt.id);107 return (108 <button109 key={opt.id}110 type="button"111 aria-pressed={on}112 onClick={() => toggle(opt.id)}113 onPointerEnter={() => setHovered(opt.id)}114 className={cn(115 "relative inline-flex h-8 items-center gap-1.5 rounded-full px-3.5 text-[13px] font-medium whitespace-nowrap outline-none",116 "transition-colors duration-200 active:scale-[0.97]",117 "focus-visible:ring-2 focus-visible:ring-(--color-focus) focus-visible:ring-offset-1 focus-visible:ring-offset-(--color-paper-2)",118 on ? "text-(--color-paper)" : "text-(--color-neutral) hover:text-(--color-ink)",119 )}120 >121 {hovered === opt.id && !on ? (122 <motion.span123 layoutId="hover"124 aria-hidden125 transition={spring}126 className="absolute inset-0 -z-10 rounded-full bg-(--color-ink)/[0.05]"127 />128 ) : null}129 {on ? (130 <motion.span131 layoutId="pill"132 aria-hidden133 transition={spring}134 className="absolute inset-0 -z-10 rounded-full bg-(--color-ink) shadow-[inset_0_1px_0_rgba(255,255,255,0.14),0_1px_2px_rgba(0,0,0,0.15)]"135 />136 ) : null}137 {opt.icon ? <span aria-hidden className="flex size-3.5 [&_svg]:size-full [&_svg]:stroke-[1.75]">{opt.icon}</span> : null}138 {opt.label}139 {opt.count !== undefined ? <Count value={opt.count} on={on} /> : null}140 </button>141 );142 })}143 </div>144 </LayoutGroup>145 );146 }147
148 149
150 return (151 <LayoutGroup id={uid}>152 <div role="group" aria-label={ariaLabel} className={cn("flex flex-wrap items-center gap-2", className)}>153 {options.map((opt) => {154 const on = selected.includes(opt.id);155 return (156 <motion.button157 key={opt.id}158 type="button"159 aria-pressed={on}160 onClick={() => toggle(opt.id)}161 layout={reduce ? false : true}162 transition={spring}163 whileTap={reduce ? undefined : { scale: 0.95 }}164 style={{ borderRadius: 999 }}165 className={cn(166 "relative inline-flex h-9 items-center overflow-hidden pr-3.5 text-[13px] font-medium whitespace-nowrap outline-none",167 "transition-[background-color,color,box-shadow] duration-200",168 "focus-visible:ring-2 focus-visible:ring-(--color-focus) focus-visible:ring-offset-2 focus-visible:ring-offset-(--color-paper)",169 on170 ? "bg-(--color-ink) pl-2.5 text-(--color-paper) shadow-[inset_0_1px_0_rgba(255,255,255,0.14),0_1px_2px_rgba(0,0,0,0.15)]"171 : "bg-(--color-paper) pl-3.5 text-(--color-ink-2) shadow-[inset_0_0_0_1px_var(--color-rule)] hover:bg-(--color-paper-2) hover:text-(--color-ink)",172 )}173 >174 <motion.span layout="position" transition={spring} className="inline-flex items-center gap-1.5">175 <AnimatePresence initial={false}>176 {on ? (177 <motion.svg178 key="check"179 viewBox="0 0 16 16"180 aria-hidden181 fill="none"182 stroke="currentColor"183 strokeWidth="2.4"184 strokeLinecap="round"185 strokeLinejoin="round"186 initial={{ width: 0, opacity: 0, marginRight: -6 }}187 animate={{ width: 14, opacity: 1, marginRight: 0 }}188 exit={{ width: 0, opacity: 0, marginRight: -6 }}189 transition={spring}190 className="h-3.5 shrink-0"191 >192 <motion.path193 d="M3.5 8.5l3 3 6-7"194 initial={{ pathLength: reduce ? 1 : 0 }}195 animate={{ pathLength: 1 }}196 transition={{ duration: 0.28, delay: 0.06 }}197 />198 </motion.svg>199 ) : null}200 </AnimatePresence>201 {opt.icon ? <span aria-hidden className="flex size-3.5 [&_svg]:size-full [&_svg]:stroke-[1.75]">{opt.icon}</span> : null}202 <span>{opt.label}</span>203 {opt.count !== undefined ? <Count value={opt.count} on={on} /> : null}204 </motion.span>205 </motion.button>206 );207 })}208
209 <AnimatePresence initial={false} mode="popLayout">210 {clearable && selected.length > 0 ? (211 <motion.button212 key="clear"213 type="button"214 layout={reduce ? false : "position"}215 onClick={() => setSelected([])}216 initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.8, filter: "blur(4px)" }}217 animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}218 exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.8, filter: "blur(4px)" }}219 transition={spring}220 aria-label={`Clear ${selected.length} ${selected.length === 1 ? "filter" : "filters"}`}221 className={cn(222 "inline-flex h-9 items-center gap-1 rounded-full px-3 text-[13px] font-medium text-(--color-neutral) outline-none",223 "transition-colors duration-150 hover:bg-(--color-paper-2) hover:text-(--color-ink) active:scale-[0.96]",224 "focus-visible:ring-2 focus-visible:ring-(--color-focus)",225 )}226 >227 <X className="size-3.5" strokeWidth={2} aria-hidden />228 Clear229 </motion.button>230 ) : null}231 </AnimatePresence>232 </div>233 </LayoutGroup>234 );235}