{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "language-switcher",
  "title": "Language Switcher",
  "description": "A language picker: a globe button whose name rolls to the new language, opening a searchable list of native names with codes and a check on the current one, or a segmented row of codes for two to four languages.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/language-switcher/language-switcher.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type LanguageSwitcherSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface Language {\n  /** BCP 47 code, e.g. \"en\", \"pt-BR\". Shown as the short badge. */\n  code: string;\n  /** The language's own name, e.g. \"Deutsch\". */\n  native: string;\n  /** The name in the interface language, e.g. \"German\". */\n  label?: string;\n}\n\nexport interface LanguageSwitcherProps {\n  languages?: Language[];\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (code: string) => void;\n  /** dropdown: a button with a searchable list. segmented: every code side by side (best for 2–4 languages). */\n  variant?: \"dropdown\" | \"segmented\";\n  /** Show a filter box in the list. Defaults to on when there are more than six languages. */\n  searchable?: boolean;\n  defaultOpen?: boolean;\n  align?: \"start\" | \"end\";\n  size?: LanguageSwitcherSize;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.14)\", bg: \"rgba(255,255,255,0.03)\", panel: \"#0E0E0E\", hover: \"rgba(255,255,255,0.07)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", chip: \"rgba(255,255,255,0.08)\", pill: \"#F5F4F1\", pillText: \"#0A0A0A\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.1)\", bg: \"#FFFFFF\", panel: \"#FFFFFF\", hover: \"rgba(10,10,10,0.05)\", border: \"rgba(10,10,10,0.14)\", borderHover: \"rgba(10,10,10,0.28)\", focus: \"rgba(10,10,10,0.85)\", chip: \"rgba(10,10,10,0.06)\", pill: \"#0A0A0A\", pillText: \"#FFFFFF\", shadow: \"0 20px 50px rgba(0,0,0,0.16)\" },\n};\n\nconst SIZES: Record<LanguageSwitcherSize, { height: number; font: number; small: number; radius: number; row: number }> = {\n  sm: { height: 36, font: 13, small: 11.5, radius: 11, row: 34 },\n  md: { height: 44, font: 14, small: 12, radius: 13, row: 38 },\n  lg: { height: 52, font: 15.5, small: 13, radius: 15, row: 42 },\n};\n\nconst DEFAULT_LANGUAGES: Language[] = [\n  { code: \"en\", native: \"English\", label: \"English\" },\n  { code: \"de\", native: \"Deutsch\", label: \"German\" },\n  { code: \"fr\", native: \"Français\", label: \"French\" },\n  { code: \"es\", native: \"Español\", label: \"Spanish\" },\n  { code: \"it\", native: \"Italiano\", label: \"Italian\" },\n  { code: \"pt-BR\", native: \"Português\", label: \"Portuguese (Brazil)\" },\n  { code: \"tr\", native: \"Türkçe\", label: \"Turkish\" },\n  { code: \"ja\", native: \"日本語\", label: \"Japanese\" },\n  { code: \"ar\", native: \"العربية\", label: \"Arabic\" },\n];\n\nconst short = (code: string) => code.split(\"-\")[0].toUpperCase();\n\nfunction Globe({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" aria-hidden=\"true\">\n      <circle cx=\"10\" cy=\"10\" r=\"7.25\" />\n      <path d=\"M2.75 10h14.5M10 2.75c2 2 3 4.4 3 7.25s-1 5.25-3 7.25c-2-2-3-4.4-3-7.25s1-5.25 3-7.25Z\" />\n    </svg>\n  );\n}\n\nexport function LanguageSwitcher({\n  languages = DEFAULT_LANGUAGES,\n  value,\n  defaultValue,\n  onValueChange,\n  variant = \"dropdown\",\n  searchable,\n  defaultOpen = false,\n  align = \"start\",\n  size = \"md\",\n  theme = \"dark\",\n  className,\n}: LanguageSwitcherProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  const listRef = React.useRef<HTMLUListElement>(null);\n  const [internal, setInternal] = React.useState(defaultValue ?? languages[0]?.code);\n  const current = value ?? internal;\n  const selected = languages.find((l) => l.code === current) ?? languages[0];\n  const [open, setOpen] = React.useState(defaultOpen);\n  const [query, setQuery] = React.useState(\"\");\n  const [hoverTrigger, setHoverTrigger] = React.useState(false);\n  const showSearch = searchable ?? languages.length > 6;\n\n  const q = query.trim().toLowerCase();\n  const filtered = q ? languages.filter((l) => [l.native, l.label ?? \"\", l.code].some((t) => t.toLowerCase().includes(q))) : languages;\n  const [activeIndex, setActiveIndex] = React.useState(0);\n\n  const choose = (code: string) => {\n    if (value === undefined) setInternal(code);\n    onValueChange?.(code);\n    setOpen(false);\n    setQuery(\"\");\n    rootRef.current?.querySelector<HTMLElement>(\"[data-ls-trigger]\")?.focus({ preventScroll: true });\n  };\n\n  const openList = () => {\n    setActiveIndex(Math.max(0, languages.findIndex((l) => l.code === current)));\n    setQuery(\"\");\n    setOpen(true);\n  };\n\n  React.useEffect(() => {\n    if (!open) return;\n    requestAnimationFrame(() => (showSearch ? inputRef.current : listRef.current)?.focus({ preventScroll: true }));\n    const onDown = (e: PointerEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);\n    };\n    document.addEventListener(\"pointerdown\", onDown);\n    return () => document.removeEventListener(\"pointerdown\", onDown);\n  }, [open, showSearch]);\n\n  // Keep the highlighted row visible while arrowing through a long list.\n  React.useEffect(() => {\n    if (!open) return;\n    listRef.current?.querySelector<HTMLElement>(`[data-ls-index=\"${activeIndex}\"]`)?.scrollIntoView({ block: \"nearest\" });\n  }, [activeIndex, open]);\n\n  const onListKey = (e: React.KeyboardEvent) => {\n    if (e.key === \"ArrowDown\" || e.key === \"ArrowUp\") {\n      e.preventDefault();\n      if (!filtered.length) return;\n      setActiveIndex((i) => (i + (e.key === \"ArrowDown\" ? 1 : -1) + filtered.length) % filtered.length);\n    } else if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault();\n      setActiveIndex(e.key === \"Home\" ? 0 : filtered.length - 1);\n    } else if (e.key === \"Enter\") {\n      e.preventDefault();\n      const l = filtered[activeIndex];\n      if (l) choose(l.code);\n    } else if (e.key === \"Escape\") {\n      e.preventDefault();\n      setOpen(false);\n      rootRef.current?.querySelector<HTMLElement>(\"[data-ls-trigger]\")?.focus({ preventScroll: true });\n    } else if (e.key === \"Tab\") {\n      setOpen(false);\n    }\n  };\n\n  if (variant === \"segmented\") {\n    return (\n      <div\n        role=\"radiogroup\"\n        aria-label=\"Language\"\n        className={cn(\"inline-flex items-center\", className)}\n        style={{ padding: 3, gap: 2, borderRadius: 999, background: p.bg, border: `1px solid ${p.border}`, fontFamily: \"Inter, sans-serif\" }}\n        onKeyDown={(e) => {\n          if (e.key !== \"ArrowRight\" && e.key !== \"ArrowLeft\") return;\n          e.preventDefault();\n          const i = languages.findIndex((l) => l.code === current);\n          const next = languages[(i + (e.key === \"ArrowRight\" ? 1 : -1) + languages.length) % languages.length];\n          if (value === undefined) setInternal(next.code);\n          onValueChange?.(next.code);\n          requestAnimationFrame(() => rootRef.current?.querySelector<HTMLElement>(`[data-ls-code=\"${next.code}\"]`)?.focus());\n        }}\n        ref={rootRef}\n      >\n        {languages.map((l) => {\n          const on = l.code === current;\n          return (\n            <button\n              key={l.code}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={on}\n              aria-label={l.native}\n              data-ls-code={l.code}\n              tabIndex={on ? 0 : -1}\n              onClick={() => {\n                if (value === undefined) setInternal(l.code);\n                onValueChange?.(l.code);\n              }}\n              className=\"relative cursor-pointer border-none bg-transparent font-semibold outline-none focus-visible:ring-2\"\n              style={{ height: s.height - 8, minWidth: s.height - 4, padding: \"0 10px\", borderRadius: 999, fontSize: s.small, letterSpacing: \"0.04em\", color: on ? p.pillText : p.muted, transition: \"color 0.18s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n            >\n              {on && <motion.span layoutId={reduce ? undefined : `${uid}-seg`} className=\"absolute inset-0\" style={{ borderRadius: 999, background: p.pill }} transition={{ type: \"spring\", stiffness: 500, damping: 38 }} />}\n              <span className=\"relative\">{short(l.code)}</span>\n            </button>\n          );\n        })}\n      </div>\n    );\n  }\n\n  return (\n    <div ref={rootRef} className={cn(\"relative inline-flex h-fit self-start\", className)} style={{ fontFamily: \"Inter, sans-serif\" }}>\n      <motion.button\n        type=\"button\"\n        data-ls-trigger=\"\"\n        aria-haspopup=\"listbox\"\n        aria-expanded={open}\n        aria-controls={open ? `${uid}-list` : undefined}\n        aria-label={`Language: ${selected?.native}`}\n        onClick={() => (open ? setOpen(false) : openList())}\n        onKeyDown={(e) => {\n          if (e.key === \"ArrowDown\" || e.key === \"Enter\" || e.key === \" \") {\n            e.preventDefault();\n            openList();\n          }\n        }}\n        onMouseEnter={() => setHoverTrigger(true)}\n        onMouseLeave={() => setHoverTrigger(false)}\n        whileTap={{ scale: 0.97 }}\n        className=\"flex cursor-pointer items-center font-semibold outline-none focus-visible:ring-2\"\n        style={{ height: s.height, padding: \"0 12px 0 11px\", gap: 8, borderRadius: s.radius, background: open || hoverTrigger ? p.hover : p.bg, border: `1px solid ${open ? p.focus : hoverTrigger ? p.borderHover : p.border}`, color: p.text, fontSize: s.font, transition: \"background 0.16s ease, border-color 0.16s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n      >\n        <span style={{ color: p.muted }}>\n          <Globe size={s.font + 3} />\n        </span>\n        {/* The name rolls to the new one when the language changes. */}\n        <span className=\"relative overflow-hidden\" style={{ height: s.font * 1.4 }}>\n          <AnimatePresence mode=\"popLayout\" initial={false}>\n            <motion.span\n              key={selected?.code}\n              className=\"block whitespace-nowrap\"\n              initial={reduce ? { opacity: 0 } : { y: \"100%\", opacity: 0 }}\n              animate={{ y: 0, opacity: 1 }}\n              exit={reduce ? { opacity: 0 } : { y: \"-100%\", opacity: 0 }}\n              transition={{ duration: 0.26, ease: [0.22, 1, 0.36, 1] }}\n              style={{ lineHeight: 1.4 }}\n            >\n              {selected?.native}\n            </motion.span>\n          </AnimatePresence>\n        </span>\n        <svg width=\"13\" height=\"13\" viewBox=\"0 0 16 16\" fill=\"none\" stroke={p.muted} strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" style={{ transform: `rotate(${open ? 180 : 0}deg)`, transition: \"transform 0.2s ease\" }}>\n          <path d=\"M4 6l4 4 4-4\" />\n        </svg>\n      </motion.button>\n\n      <AnimatePresence>\n        {open && (\n          <motion.div\n            key=\"panel\"\n            initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.96, y: -4 }}\n            animate={{ opacity: 1, scale: 1, y: 0 }}\n            exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.97, y: -4 }}\n            transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n            className=\"absolute z-50 flex flex-col\"\n            style={{ top: s.height + 8, [align === \"start\" ? \"left\" : \"right\"]: 0, width: 260, padding: 6, borderRadius: s.radius + 4, background: p.panel, border: `1px solid ${p.border}`, boxShadow: p.shadow, transformOrigin: align === \"start\" ? \"top left\" : \"top right\" }}\n            onKeyDown={onListKey}\n          >\n            {showSearch && (\n              <div className=\"flex items-center\" style={{ gap: 8, height: s.row, padding: \"0 10px\", marginBottom: 4, borderRadius: s.radius - 3, border: `1px solid ${p.faint}`, color: p.muted }}>\n                <svg width=\"14\" height=\"14\" viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" aria-hidden=\"true\">\n                  <circle cx=\"9\" cy=\"9\" r=\"5.75\" />\n                  <path d=\"m13.5 13.5 3.5 3.5\" />\n                </svg>\n                <input\n                  ref={inputRef}\n                  value={query}\n                  onChange={(e) => {\n                    setQuery(e.target.value);\n                    setActiveIndex(0);\n                  }}\n                  placeholder=\"Search languages\"\n                  aria-label=\"Search languages\"\n                  aria-controls={`${uid}-list`}\n                  aria-activedescendant={filtered[activeIndex] ? `${uid}-${filtered[activeIndex].code}` : undefined}\n                  className=\"min-w-0 flex-1 border-none bg-transparent p-0 font-medium outline-none\"\n                  style={{ color: p.text, fontSize: s.font - 0.5 }}\n                />\n              </div>\n            )}\n            <ul\n              ref={listRef}\n              id={`${uid}-list`}\n              role=\"listbox\"\n              aria-label=\"Languages\"\n              tabIndex={showSearch ? -1 : 0}\n              aria-activedescendant={!showSearch && filtered[activeIndex] ? `${uid}-${filtered[activeIndex].code}` : undefined}\n              className=\"m-0 list-none overflow-y-auto p-0 outline-none\"\n              style={{ maxHeight: s.row * 6.5 }}\n            >\n              {filtered.length === 0 && (\n                <li className=\"text-center\" style={{ padding: \"14px 10px\", color: p.muted, fontSize: s.small }}>\n                  No language matches “{query}”\n                </li>\n              )}\n              {filtered.map((l, i) => {\n                const on = l.code === current;\n                const hl = i === activeIndex;\n                return (\n                  <li\n                    key={l.code}\n                    id={`${uid}-${l.code}`}\n                    data-ls-index={i}\n                    role=\"option\"\n                    aria-selected={on}\n                    lang={l.code}\n                    onMouseEnter={() => setActiveIndex(i)}\n                    onClick={() => choose(l.code)}\n                    className=\"relative flex cursor-pointer items-center\"\n                    style={{ gap: 10, height: s.row, padding: \"0 10px\", borderRadius: s.radius - 3 }}\n                  >\n                    {hl && <motion.span layoutId={reduce ? undefined : `${uid}-hl`} className=\"absolute inset-0\" style={{ borderRadius: s.radius - 3, background: p.hover }} transition={{ type: \"spring\", stiffness: 700, damping: 45 }} />}\n                    <span className=\"relative flex shrink-0 items-center justify-center font-semibold\" style={{ width: 30, height: 20, borderRadius: 6, background: p.chip, color: p.muted, fontSize: 10, letterSpacing: \"0.04em\" }}>\n                      {short(l.code)}\n                    </span>\n                    <span className=\"relative shrink-0 font-medium\" style={{ color: p.text, fontSize: s.font - 0.5 }}>\n                      {l.native}\n                    </span>\n                    {l.label && l.label !== l.native && (\n                      <span className=\"relative min-w-0 flex-1 truncate text-right\" style={{ color: p.muted, fontSize: s.small }} lang=\"en\">\n                        {l.label}\n                      </span>\n                    )}\n                    <span className=\"relative ml-auto flex w-4 shrink-0 justify-end\">\n                      {on && (\n                        <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke={p.text} strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                          <motion.path d=\"M3.5 8.5 6.5 11.5 12.5 4.5\" initial={{ pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.22 }} />\n                        </svg>\n                      )}\n                    </span>\n                  </li>\n                );\n              })}\n            </ul>\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n\nexport default LanguageSwitcher;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}