{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "focus-trap",
  "title": "Focus Trap",
  "description": "Keeps keyboard focus inside a dialog, drawer or menu while it's open: Tab and Shift+Tab loop through its controls, focus starts where you choose and returns to where it was, Escape calls you back, and an optional ring glides to whatever has focus.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/focus-trap/focus-trap.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\nconst FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]):not([type=\"hidden\"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"]), [contenteditable=\"true\"]';\n\nfunction focusables(root: HTMLElement) {\n  return Array.from(root.querySelectorAll<HTMLElement>(FOCUSABLE)).filter((el) => el.offsetParent !== null || el === document.activeElement);\n}\n\nexport interface FocusTrapOptions {\n  active?: boolean;\n  /** Called on Escape while the trap is active. */\n  onEscape?: () => void;\n  /** Where focus goes when the trap turns on: a CSS selector inside it, or the first focusable element. */\n  initialFocus?: string;\n  /** Put focus back where it was when the trap turns off. */\n  returnFocus?: boolean;\n}\n\n/** Keeps Tab and Shift+Tab inside `ref` while active, and handles initial and returning focus. */\nexport function useFocusTrap(ref: React.RefObject<HTMLElement | null>, { active = true, onEscape, initialFocus, returnFocus = true }: FocusTrapOptions = {}) {\n  const escRef = React.useRef(onEscape);\n  React.useEffect(() => {\n    escRef.current = onEscape;\n  }, [onEscape]);\n\n  React.useEffect(() => {\n    const root = ref.current;\n    if (!active || !root) return;\n    const previous = document.activeElement as HTMLElement | null;\n    const first = (initialFocus && root.querySelector<HTMLElement>(initialFocus)) || focusables(root)[0] || root;\n    if (first === root && !root.hasAttribute(\"tabindex\")) root.setAttribute(\"tabindex\", \"-1\");\n    first.focus({ preventScroll: true });\n\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") {\n        escRef.current?.();\n        return;\n      }\n      if (e.key !== \"Tab\") return;\n      const list = focusables(root);\n      if (!list.length) {\n        e.preventDefault();\n        return;\n      }\n      const i = list.indexOf(document.activeElement as HTMLElement);\n      const next = e.shiftKey ? (i <= 0 ? list.length - 1 : i - 1) : i === list.length - 1 || i === -1 ? 0 : i + 1;\n      e.preventDefault();\n      list[next].focus();\n    };\n    // Pull focus back if something outside grabs it (a click on the page behind, for example).\n    const onFocusIn = (e: FocusEvent) => {\n      if (!root.contains(e.target as Node)) focusables(root)[0]?.focus({ preventScroll: true });\n    };\n    document.addEventListener(\"keydown\", onKey);\n    document.addEventListener(\"focusin\", onFocusIn);\n    return () => {\n      document.removeEventListener(\"keydown\", onKey);\n      document.removeEventListener(\"focusin\", onFocusIn);\n      if (returnFocus) previous?.focus?.({ preventScroll: true });\n    };\n  }, [active, initialFocus, returnFocus, ref]);\n}\n\nexport interface FocusTrapProps extends FocusTrapOptions {\n  children?: React.ReactNode;\n  /** A ring that glides to whichever element has keyboard focus inside the trap. */\n  showIndicator?: boolean;\n  indicatorColor?: string;\n  className?: string;\n}\n\nexport function FocusTrap({ children, active = true, onEscape, initialFocus, returnFocus = true, showIndicator = true, indicatorColor = \"#F5F4F1\", className }: FocusTrapProps) {\n  const reduce = Boolean(useReducedMotion());\n  const ref = React.useRef<HTMLDivElement>(null);\n  const [box, setBox] = React.useState<{ x: number; y: number; w: number; h: number; r: string } | null>(null);\n  useFocusTrap(ref, { active, onEscape, initialFocus, returnFocus });\n\n  React.useEffect(() => {\n    const root = ref.current;\n    if (!root || !showIndicator) return;\n    const place = () => {\n      const el = document.activeElement as HTMLElement | null;\n      if (!el || !root.contains(el) || el === root || !el.matches(\":focus-visible\")) return setBox(null);\n      const a = root.getBoundingClientRect();\n      const b = el.getBoundingClientRect();\n      setBox({ x: b.left - a.left - 4, y: b.top - a.top - 4, w: b.width + 8, h: b.height + 8, r: getComputedStyle(el).borderRadius });\n    };\n    const onOut = (e: FocusEvent) => {\n      if (!root.contains(e.relatedTarget as Node)) setBox(null);\n    };\n    root.addEventListener(\"focusin\", place);\n    root.addEventListener(\"focusout\", onOut);\n    return () => {\n      root.removeEventListener(\"focusin\", place);\n      root.removeEventListener(\"focusout\", onOut);\n    };\n  }, [showIndicator]);\n\n  return (\n    <div ref={ref} className={cn(\"relative\", className)}>\n      {children}\n      <AnimatePresence>\n        {showIndicator && box && (\n          <motion.span\n            key=\"ring\"\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute top-0 left-0 z-10\"\n            style={{ border: `2px solid ${indicatorColor}`, boxShadow: `0 0 0 4px color-mix(in srgb, ${indicatorColor} 18%, transparent)` }}\n            initial={{ opacity: 0, x: box.x, y: box.y, width: box.w, height: box.h, borderRadius: box.r }}\n            animate={{ opacity: 1, x: box.x, y: box.y, width: box.w, height: box.h, borderRadius: `calc(${box.r} + 4px)` }}\n            exit={{ opacity: 0 }}\n            transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 38, opacity: { duration: 0.15 } }}\n          />\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n\nexport default FocusTrap;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}