|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useId } from "react"; |
| 4 | +import { useTranslation } from "@/components/providers/language-provider"; |
| 5 | +import { cn } from "@/utils/cn"; |
| 6 | +import { COMPARISON_PRESETS } from "../config/comparison-presets"; |
| 7 | + |
| 8 | +type Preset = (typeof COMPARISON_PRESETS)[number]; |
| 9 | + |
| 10 | +interface PresetPillsProps { |
| 11 | + onSelect: (preset: Preset) => void; |
| 12 | + loading: boolean; |
| 13 | + username1: string; |
| 14 | + username2: string; |
| 15 | +} |
| 16 | + |
| 17 | +export function PresetPills({ onSelect, loading, username1, username2 }: PresetPillsProps) { |
| 18 | + const { t } = useTranslation(); |
| 19 | + const headingId = useId(); |
| 20 | + |
| 21 | + return ( |
| 22 | + <section |
| 23 | + aria-labelledby={headingId} |
| 24 | + className="flex animate-fadeIn flex-col items-center gap-3" |
| 25 | + > |
| 26 | + <h2 id={headingId} className="text-sm text-muted-foreground"> |
| 27 | + {t("presets.title")} |
| 28 | + </h2> |
| 29 | + |
| 30 | + <ul className="flex flex-wrap justify-center gap-2" aria-busy={loading}> |
| 31 | + {COMPARISON_PRESETS.map((preset) => { |
| 32 | + const isActive = |
| 33 | + loading && username1 === preset.username1 && username2 === preset.username2; |
| 34 | + |
| 35 | + return ( |
| 36 | + <li key={preset.labelKey}> |
| 37 | + {/* Native <button>: Tab to focus, Enter/Space to activate */} |
| 38 | + <button |
| 39 | + type="button" |
| 40 | + disabled={loading} |
| 41 | + onClick={() => onSelect(preset)} |
| 42 | + className={cn( |
| 43 | + "inline-flex items-center gap-2 rounded-full border border-border bg-card px-4 py-2 text-sm text-foreground", |
| 44 | + "transition-all duration-200 hover:bg-accent", |
| 45 | + "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2", |
| 46 | + "disabled:cursor-not-allowed disabled:opacity-50 motion-reduce:transition-none", |
| 47 | + isActive && "opacity-100 ring-2 ring-ring", |
| 48 | + )} |
| 49 | + > |
| 50 | + {isActive && ( |
| 51 | + <span |
| 52 | + aria-hidden="true" |
| 53 | + className="h-3.5 w-3.5 animate-spin rounded-full border-2 border-current border-t-transparent" |
| 54 | + /> |
| 55 | + )} |
| 56 | + {t(preset.labelKey)} |
| 57 | + </button> |
| 58 | + </li> |
| 59 | + ); |
| 60 | + })} |
| 61 | + </ul> |
| 62 | + </section> |
| 63 | + ); |
| 64 | +} |
0 commit comments