{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-lens",
  "title": "Lens",
  "description": "A round magnifier that follows the pointer over an image or any content, and moves with the arrow keys when focused.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/lens.tsx",
      "content": "'use client';\n\nimport {\n  animate,\n  motion,\n  useMotionTemplate,\n  useMotionValue,\n  useTransform,\n  type MotionValue,\n} from 'motion/react';\nimport { useEffect, useId, useRef, useState } from 'react';\n\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nexport type LensPosition = { x: number; y: number };\n\nconst EASE_OUT_STRONG = [0.23, 1, 0.32, 1] as const;\n\nconst SHOW = { duration: 0.18, ease: EASE_OUT_STRONG };\n\nconst HIDE = { duration: 0.12, ease: EASE_OUT_STRONG };\n\nconst GLIDE = { duration: 0.16, ease: EASE_OUT_STRONG };\n\nconst RESTING_SCALE = 0.9;\n\nconst KEY_STEP = 12;\n\nconst LARGE_KEY_STEP = 48;\n\nconst KEY_DIRECTIONS: Record<string, LensPosition> = {\n  ArrowLeft: { x: -1, y: 0 },\n  ArrowRight: { x: 1, y: 0 },\n  ArrowUp: { x: 0, y: -1 },\n  ArrowDown: { x: 0, y: 1 },\n};\n\nfunction clamp(value: number, min: number, max: number) {\n  const atLeastMin = Math.max(value, min);\n  return Math.min(atLeastMin, max);\n}\n\nfunction stepOf(isLarge: boolean) {\n  if (isLarge) {\n    return LARGE_KEY_STEP;\n  }\n  return KEY_STEP;\n}\n\nfunction transitionOf(isShown: boolean) {\n  if (isShown) {\n    return SHOW;\n  }\n  return HIDE;\n}\n\nfunction scaleOf(isShown: boolean) {\n  if (isShown) {\n    return 1;\n  }\n  return RESTING_SCALE;\n}\n\nfunction opennessOf(isShown: boolean) {\n  if (isShown) {\n    return 1;\n  }\n  return 0;\n}\n\nfunction isMouseLike(event: React.PointerEvent) {\n  return event.pointerType !== 'touch';\n}\n\nfunction moveTo(\n  value: MotionValue<number>,\n  target: number,\n  shouldGlide: boolean\n) {\n  if (shouldGlide) {\n    animate(value, target, GLIDE);\n    return;\n  }\n  value.jump(target);\n}\n\nexport type LensProps = {\n  children: React.ReactNode;\n  zoom?: number;\n  size?: number;\n  defaultPosition?: LensPosition;\n  alwaysVisible?: boolean;\n  label?: string;\n  className?: string;\n};\n\nexport function Lens({\n  children,\n  zoom = 2,\n  size = 160,\n  defaultPosition,\n  alwaysVisible = false,\n  label = 'Magnifier',\n  className,\n}: LensProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const hintId = useId();\n  const prefersReducedMotion = usePrefersReducedMotion();\n  const [isPointerInside, setIsPointerInside] = useState(false);\n  const [isKeyboardActive, setIsKeyboardActive] = useState(false);\n  const radius = size / 2;\n  const x = useMotionValue(defaultPosition?.x ?? 0);\n  const y = useMotionValue(defaultPosition?.y ?? 0);\n  const hasPlacedRef = useRef(defaultPosition !== undefined);\n  const openness = useMotionValue(opennessOf(alwaysVisible));\n  const restingRadius = radius * scaleOf(alwaysVisible);\n  const shownRadius = useMotionValue(restingRadius);\n  const ringScale = useMotionValue(scaleOf(alwaysVisible));\n  const ringX = useTransform(x, (value) => value - radius);\n  const ringY = useTransform(y, (value) => value - radius);\n  const clipPath = useMotionTemplate`circle(${shownRadius}px at ${x}px ${y}px)`;\n  const transformOrigin = useMotionTemplate`${x}px ${y}px`;\n\n  useEffect(() => {\n    const root = rootRef.current;\n    const hasPlaced = hasPlacedRef.current;\n    if (hasPlaced || !root) {\n      return;\n    }\n    const centreX = root.clientWidth / 2;\n    const centreY = root.clientHeight / 2;\n    hasPlacedRef.current = true;\n    x.jump(centreX);\n    y.jump(centreY);\n  }, [x, y]);\n\n  function setShown(isShown: boolean) {\n    const transition = transitionOf(isShown);\n    const scale = scaleOf(isShown);\n    const targetRadius = radius * scale;\n    const targetOpenness = opennessOf(isShown);\n    animate(openness, targetOpenness, transition);\n    if (prefersReducedMotion) {\n      shownRadius.jump(radius);\n      ringScale.jump(1);\n      return;\n    }\n    animate(shownRadius, targetRadius, transition);\n    animate(ringScale, scale, transition);\n  }\n\n  function followPointer(event: React.PointerEvent<HTMLDivElement>) {\n    const root = rootRef.current;\n    if (!root) {\n      return;\n    }\n    const box = root.getBoundingClientRect();\n    const pointerX = event.clientX - box.left;\n    const pointerY = event.clientY - box.top;\n    hasPlacedRef.current = true;\n    x.jump(pointerX);\n    y.jump(pointerY);\n  }\n\n  function enter(event: React.PointerEvent<HTMLDivElement>) {\n    if (!isMouseLike(event)) {\n      return;\n    }\n    followPointer(event);\n    setIsPointerInside(true);\n    setShown(true);\n  }\n\n  function leave() {\n    setIsPointerInside(false);\n    const staysShown = isKeyboardActive || alwaysVisible;\n    if (!staysShown) {\n      setShown(false);\n    }\n  }\n\n  function press(event: React.PointerEvent<HTMLDivElement>) {\n    followPointer(event);\n    setIsPointerInside(true);\n    setShown(true);\n  }\n\n  function showForKeyboard(event: React.FocusEvent<HTMLDivElement>) {\n    const isKeyboardFocus = event.currentTarget.matches(':focus-visible');\n    if (!isKeyboardFocus) {\n      return;\n    }\n    setIsKeyboardActive(true);\n    setShown(true);\n  }\n\n  function hideForKeyboard() {\n    setIsKeyboardActive(false);\n    const staysShown = isPointerInside || alwaysVisible;\n    if (!staysShown) {\n      setShown(false);\n    }\n  }\n\n  function moveWithKeys(event: React.KeyboardEvent<HTMLDivElement>) {\n    const root = rootRef.current;\n    if (event.key === 'Escape') {\n      hideForKeyboard();\n      return;\n    }\n    const direction = KEY_DIRECTIONS[event.key];\n    if (!direction || !root) {\n      return;\n    }\n    event.preventDefault();\n    const step = stepOf(event.shiftKey);\n    const movedX = x.get() + direction.x * step;\n    const movedY = y.get() + direction.y * step;\n    const nextX = clamp(movedX, 0, root.clientWidth);\n    const nextY = clamp(movedY, 0, root.clientHeight);\n    const shouldGlide = !prefersReducedMotion;\n    moveTo(x, nextX, shouldGlide);\n    moveTo(y, nextY, shouldGlide);\n    if (!isKeyboardActive) {\n      setIsKeyboardActive(true);\n      setShown(true);\n    }\n  }\n\n  return (\n    <div\n      ref={rootRef}\n      role='group'\n      aria-roledescription='magnifier'\n      aria-label={label}\n      aria-describedby={hintId}\n      tabIndex={0}\n      onPointerEnter={enter}\n      onPointerMove={followPointer}\n      onPointerLeave={leave}\n      onPointerDown={press}\n      onFocus={showForKeyboard}\n      onBlur={hideForKeyboard}\n      onKeyDown={moveWithKeys}\n      className={cn(\n        'focus-visible:outline-ring relative cursor-zoom-in touch-pan-y overflow-hidden rounded-xl outline-none focus-visible:outline-2 focus-visible:outline-offset-2',\n        className\n      )}\n    >\n      {children}\n      <motion.div\n        aria-hidden='true'\n        inert\n        className='pointer-events-none absolute inset-0'\n        style={{ clipPath, opacity: openness }}\n      >\n        <motion.div\n          className='size-full'\n          style={{ scale: zoom, transformOrigin }}\n        >\n          {children}\n        </motion.div>\n      </motion.div>\n      <motion.div\n        aria-hidden='true'\n        className='hairline-ring ring-card/80 pointer-events-none absolute top-0 left-0 rounded-full shadow-[0_8px_24px_-6px_rgb(0_0_0/0.3)]'\n        style={{\n          x: ringX,\n          y: ringY,\n          width: size,\n          height: size,\n          opacity: openness,\n          scale: ringScale,\n        }}\n      />\n      <span id={hintId} className='sr-only'>\n        Use the arrow keys to move the magnifier, and Escape to hide it\n      </span>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/lens.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}