{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lens",
  "title": "Lens",
  "description": "A magnifier over any content: a round lens, a lifted loupe, a zoomed panel beside it or a full-width band, with a second copy that focus and assistive tech never reach.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/lens.tsx",
      "content": "import type * as React from \"react\"\r\nimport { useLayoutEffect } from \"react\"\r\nimport { useLens, type LensEffect, type LensVariant } from \"@/registry/miseui/ui/lens-state\"\r\nimport type { Point } from \"@/registry/miseui/ui/lens-math\"\r\nimport { cn } from \"@/registry/miseui/lib/utils\"\r\n\r\nconst DEFAULT_SIZE: Record<LensVariant, number> = { circle: 128, square: 112, panel: 0, strip: 64 }\r\n\r\nconst GLASS_CLASS: Record<Exclude<LensVariant, \"panel\">, string> = {\r\n  circle: \"rounded-full shadow-xl ring-1 shadow-black/25 ring-foreground/10\",\r\n  square: \"rounded-md ring-1 ring-foreground/40\",\r\n  strip: \"shadow-[0_-1px_0_var(--border),0_1px_0_var(--border),0_8px_24px_-8px_rgb(0_0_0/0.25)]\",\r\n}\r\n\r\nconst ROOT_CLASS = \"@container min-w-0 rounded-md outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50\"\r\nconst PRESS_CLASS = \"touch-none [-webkit-touch-callout:none] pointer-coarse:select-none\"\r\nconst REGION_CLASS = \"pointer-events-none absolute top-0 left-0 rounded-sm bg-foreground/5 ring-1 ring-foreground/50\"\r\nconst PANEL_CLASS = \"relative aspect-[2/1] min-w-0 overflow-hidden rounded-lg border bg-background @md:aspect-auto\"\r\nconst FADE_CLASS = \"transition-[opacity,scale] duration-(--duration-fast) ease-(--ease-mise) motion-reduce:transition-none\"\r\n\r\ninterface CopyProps {\r\n  copyRef: React.RefObject<HTMLDivElement | null>\r\n  width: number\r\n  height: number\r\n  children: React.ReactNode\r\n}\r\n\r\n/**\r\n * Hidden and inert, so focus and assistive tech never reach the second copy. Ids and form names are stripped as well:\r\n * two equal ids break label and aria references, and an inert radio with the same name would still take its group's selection.\r\n */\r\nfunction LensCopy({ copyRef, width, height, children }: CopyProps) {\r\n  useLayoutEffect(() => {\r\n    copyRef.current?.querySelectorAll(\"[id], [name]\").forEach((element) => {\r\n      element.removeAttribute(\"id\")\r\n      element.removeAttribute(\"name\")\r\n    })\r\n  })\r\n  return (\r\n    <div\r\n      ref={copyRef}\r\n      aria-hidden=\"true\"\r\n      inert\r\n      data-slot=\"lens-copy\"\r\n      className=\"pointer-events-none absolute top-0 left-0 origin-top-left select-none\"\r\n      style={{ width, height }}\r\n    >\r\n      {children}\r\n    </div>\r\n  )\r\n}\r\n\r\nfunction SquareMarks({ zoom }: { zoom: number }) {\r\n  return (\r\n    <>\r\n      <span aria-hidden=\"true\" data-slot=\"lens-crosshair\" className=\"absolute top-1/2 left-1/2 size-4 -translate-x-1/2 -translate-y-1/2 [background:linear-gradient(var(--foreground),var(--foreground))_center/100%_1px_no-repeat,linear-gradient(var(--foreground),var(--foreground))_center/1px_100%_no-repeat] opacity-50\" />\r\n      <span aria-hidden=\"true\" data-slot=\"lens-zoom\" className=\"absolute right-1 bottom-1 rounded-sm bg-background/85 px-1 text-xs text-foreground tabular-nums\">\r\n        {zoom}x\r\n      </span>\r\n    </>\r\n  )\r\n}\r\n\r\ntype LensProps = Omit<React.ComponentProps<\"div\">, \"aria-label\"> & {\r\n  /** Names the magnifier and says how to use it, for keyboard and screen reader users. */\r\n  label?: string\r\n  /** `circle` a round lens on the pointer, `square` a loupe lifted off the point it reads, `panel` a zoomed view beside the content, `strip` a full-width band. */\r\n  variant?: LensVariant\r\n  /** `press` shows it only while pressed, and a finger can drag it. */\r\n  effect?: LensEffect\r\n  /** How many times larger. At least 1. */\r\n  zoom?: number\r\n  /** Diameter of `circle`, side of `square`, height of `strip`, in px. `panel` takes its size from the layout. */\r\n  size?: number\r\n  /** Where the lens rests until someone moves it, as fractions of the content. It stays visible there. */\r\n  defaultPoint?: Point\r\n  /** For the view behind the zoomed content, when the content sits on something other than the page background. */\r\n  lensClassName?: string\r\n}\r\n\r\nfunction Lens({ label = \"Magnifier. Arrow keys move it, Escape hides it.\", variant = \"circle\", effect, zoom = 2, size, defaultPoint, lensClassName, className, children, ...props }: LensProps) {\r\n  const { contentRef, panelRef, bodyRef, copyRef, regionRef, box, view, zoom: shownZoom, shown, armed, pointer, keys } = useLens({ variant, effect, zoom, size: size ?? DEFAULT_SIZE[variant], defaultPoint })\r\n  const panel = variant === \"panel\"\r\n  const fade = shown ? \"scale-100 opacity-100\" : \"scale-95 opacity-0\"\r\n  const copy =\r\n    armed && box ? (\r\n      <LensCopy copyRef={copyRef} width={box.width} height={box.height}>\r\n        {children}\r\n      </LensCopy>\r\n    ) : null\r\n  const stage = (\r\n    // Only `press` owns the gesture. Without it a swipe over the content has to keep scrolling the page.\r\n    <div data-slot=\"lens-stage\" className={cn(\"relative isolate min-w-0 pointer-fine:cursor-zoom-in\", effect === \"press\" && PRESS_CLASS)} {...pointer}>\r\n      <div ref={contentRef} data-slot=\"lens-content\" className=\"min-w-0\">\r\n        {children}\r\n      </div>\r\n      {panel ? (\r\n        <div ref={regionRef} aria-hidden=\"true\" data-slot=\"lens-region\" className={cn(REGION_CLASS, FADE_CLASS, shown ? \"opacity-100\" : \"opacity-0\")} />\r\n      ) : (\r\n        <div ref={bodyRef} aria-hidden=\"true\" data-slot=\"lens-view\" className=\"pointer-events-none absolute top-0 left-0 z-10\" style={view ?? undefined}>\r\n          <div className={cn(\"relative size-full overflow-hidden bg-background\", GLASS_CLASS[variant as Exclude<LensVariant, \"panel\">], FADE_CLASS, fade, lensClassName)}>\r\n            {copy}\r\n            {variant === \"square\" ? <SquareMarks zoom={shownZoom} /> : null}\r\n          </div>\r\n        </div>\r\n      )}\r\n    </div>\r\n  )\r\n  return (\r\n    <div role=\"group\" tabIndex={0} aria-label={label} data-slot=\"lens\" data-variant={variant} data-effect={effect} data-shown={shown ? \"true\" : \"false\"} className={cn(ROOT_CLASS, className)} {...keys} {...props}>\r\n      {panel ? (\r\n        <div className=\"grid grid-cols-[minmax(0,1fr)] gap-3 @md:grid-cols-2\">\r\n          {stage}\r\n          <div ref={panelRef} aria-hidden=\"true\" data-slot=\"lens-panel\" className={cn(PANEL_CLASS, lensClassName)}>\r\n            {copy}\r\n          </div>\r\n        </div>\r\n      ) : (\r\n        stage\r\n      )}\r\n    </div>\r\n  )\r\n}\r\n\r\nexport { Lens, type LensProps, type LensVariant, type LensEffect }\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/lens-gestures.ts",
      "content": "import type * as React from \"react\"\r\nimport { useRef } from \"react\"\r\nimport { useReducedMotion } from \"motion/react\"\r\nimport { lensNudge, lensPointFromFraction, type Point, type Size } from \"@/registry/miseui/ui/lens-math\"\r\nimport { isFinePointer } from \"@/registry/miseui/ui/pointer-fine\"\r\n\r\nconst KEY_STEP_PX = 16\r\nconst KEY_STEP_LARGE_PX = 48\r\n/** Past this a press is a drag: only then is the pointer captured, because capture retargets the click and a link inside would lose it. */\r\nconst DRAG_SLOP_PX = 4\r\n\r\nfunction focusVisible(element: Element) {\r\n  try {\r\n    return element.matches(\":focus-visible\")\r\n  } catch {\r\n    return false\r\n  }\r\n}\r\n\r\ninterface PointerOptions {\r\n  contentRef: React.RefObject<HTMLElement | null>\r\n  press: boolean\r\n  shown: boolean\r\n  show: (point?: Point) => void\r\n  hide: (dismiss?: boolean) => void\r\n}\r\n\r\nexport function useLensPointer({ contentRef, press, shown, show, hide }: PointerOptions) {\r\n  const reduceMotion = useReducedMotion() ?? false\r\n  const pressStart = useRef<Point | null>(null)\r\n  const pressedRef = useRef(false)\r\n  const pointAt = (event: React.PointerEvent) => {\r\n    const rect = contentRef.current?.getBoundingClientRect()\r\n    return rect ? { x: event.clientX - rect.left, y: event.clientY - rect.top } : undefined\r\n  }\r\n  const follows = (event: React.PointerEvent) => event.pointerType !== \"touch\" && !reduceMotion && isFinePointer()\r\n  const moved = (event: React.PointerEvent) => {\r\n    const start = pressStart.current\r\n    return !start || Math.hypot(event.clientX - start.x, event.clientY - start.y) > DRAG_SLOP_PX\r\n  }\r\n  return {\r\n    onPointerDown: (event: React.PointerEvent<HTMLElement>) => {\r\n      pressStart.current = { x: event.clientX, y: event.clientY }\r\n      if (!press) return\r\n      pressedRef.current = true\r\n      show(pointAt(event))\r\n    },\r\n    onPointerMove: (event: React.PointerEvent<HTMLElement>) => {\r\n      if (pressedRef.current) {\r\n        if (event.pointerType !== \"touch\" && moved(event) && !event.currentTarget.hasPointerCapture?.(event.pointerId)) event.currentTarget.setPointerCapture?.(event.pointerId)\r\n        show(pointAt(event))\r\n      } else if (!press && follows(event)) show(pointAt(event))\r\n    },\r\n    onPointerUp: (event: React.PointerEvent<HTMLElement>) => {\r\n      if (pressedRef.current) {\r\n        pressedRef.current = false\r\n        hide()\r\n      } else if (!press && !follows(event) && !moved(event)) {\r\n        // No hover to follow, so a tap places the lens and a second tap takes it away. The click still reaches what was tapped.\r\n        if (shown) hide(true)\r\n        else show(pointAt(event))\r\n      }\r\n    },\r\n    onPointerCancel: () => {\r\n      if (!pressedRef.current) return\r\n      pressedRef.current = false\r\n      hide()\r\n    },\r\n    onPointerLeave: (event: React.PointerEvent<HTMLElement>) => {\r\n      if (!press && follows(event)) hide()\r\n    },\r\n  }\r\n}\r\n\r\ninterface KeyOptions {\r\n  box: Size | null\r\n  pointRef: React.RefObject<Point | null>\r\n  defaultPoint?: Point\r\n  show: (point?: Point) => void\r\n  hide: (dismiss?: boolean) => void\r\n}\r\n\r\nexport function useLensKeys({ box, pointRef, defaultPoint, show, hide }: KeyOptions) {\r\n  const current = (size: Size) => pointRef.current ?? lensPointFromFraction(defaultPoint, size)\r\n  return {\r\n    onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {\r\n      // Only on the root itself: an arrow in a field inside the content belongs to that field.\r\n      if (event.target !== event.currentTarget || !box) return\r\n      if (event.key === \"Escape\") return hide(true)\r\n      const next = lensNudge(current(box), event.key, box, event.shiftKey ? KEY_STEP_LARGE_PX : KEY_STEP_PX)\r\n      if (!next) return\r\n      event.preventDefault()\r\n      show(next)\r\n    },\r\n    onFocus: (event: React.FocusEvent<HTMLElement>) => {\r\n      if (event.target === event.currentTarget && box && focusVisible(event.target)) show(current(box))\r\n    },\r\n    onBlur: (event: React.FocusEvent<HTMLElement>) => {\r\n      if (event.target === event.currentTarget) hide()\r\n    },\r\n  }\r\n}\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/lens-math.ts",
      "content": "export interface Size {\n  width: number\n  height: number\n}\n\nexport interface Point {\n  x: number\n  y: number\n}\n\nexport type LensAnchor = \"centre\" | \"above\" | \"band\"\n\nconst clamp = (value: number, min: number, max: number) => (max < min ? (min + max) / 2 : Math.min(max, Math.max(min, value)))\n\n/** What the view can be at most: a lens larger than its content would show the edge of nothing. */\nexport function lensViewSize(anchor: LensAnchor, size: number, box: Size): Size {\n  return { width: anchor === \"band\" ? box.width : Math.min(size, box.width), height: Math.min(size, box.height) }\n}\n\n/**\n * The content point in the middle of the view. It stops short of the edges by half the sampled region,\n * which is what keeps empty space out of the view. Content smaller than the region stays centred.\n */\nexport function lensFocus(point: Point, box: Size, view: Size, zoom: number): Point {\n  const halfWidth = view.width / (2 * zoom)\n  const halfHeight = view.height / (2 * zoom)\n  return { x: clamp(point.x, halfWidth, box.width - halfWidth), y: clamp(point.y, halfHeight, box.height - halfHeight) }\n}\n\n/** Where the zoomed copy goes, with its origin at the top left, so the focus lands in the middle of the view. */\nexport function lensCopyOffset(focus: Point, view: Size, zoom: number): Point {\n  return { x: view.width / 2 - focus.x * zoom, y: view.height / 2 - focus.y * zoom }\n}\n\n/** Top left of the lens itself. `above` lifts it off the point it reads, and drops under the point when there is no room above. */\nexport function lensBodyPosition(point: Point, box: Size, view: Size, anchor: LensAnchor, gap = 16): Point {\n  const maxX = box.width - view.width\n  const maxY = box.height - view.height\n  if (anchor === \"band\") return { x: 0, y: clamp(point.y - view.height / 2, 0, maxY) }\n  const x = clamp(point.x - view.width / 2, 0, maxX)\n  if (anchor === \"centre\") return { x, y: clamp(point.y - view.height / 2, 0, maxY) }\n  const above = point.y - view.height - gap\n  return { x, y: clamp(above >= 0 ? above : point.y + gap, 0, maxY) }\n}\n\n/** The region of the content a panel is showing, for the frame drawn over the original. */\nexport function lensRegion(focus: Point, view: Size, zoom: number) {\n  const width = view.width / zoom\n  const height = view.height / zoom\n  return { x: focus.x - width / 2, y: focus.y - height / 2, width, height }\n}\n\nconst KEY_STEP: Readonly<Record<string, Point>> = {\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\n/** The next point for an arrow key, inside the content, or null for any other key. */\nexport function lensNudge(point: Point, key: string, box: Size, step: number): Point | null {\n  const direction = KEY_STEP[key]\n  if (!direction) return null\n  return { x: clamp(point.x + direction.x * step, 0, box.width), y: clamp(point.y + direction.y * step, 0, box.height) }\n}\n\n/** A point given as fractions of the content. */\nexport function lensPointFromFraction(fraction: Point | undefined, box: Size): Point {\n  return { x: (fraction?.x ?? 0.5) * box.width, y: (fraction?.y ?? 0.5) * box.height }\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/lens-state.ts",
      "content": "import type * as React from \"react\"\r\nimport { useCallback, useLayoutEffect, useMemo, useRef, useState } from \"react\"\r\nimport { useReducedMotion } from \"motion/react\"\r\nimport { useLensKeys, useLensPointer } from \"@/registry/miseui/ui/lens-gestures\"\r\nimport {\r\n  lensBodyPosition,\r\n  lensCopyOffset,\r\n  lensFocus,\r\n  lensPointFromFraction,\r\n  lensRegion,\r\n  lensViewSize,\r\n  type LensAnchor,\r\n  type Point,\r\n  type Size,\r\n} from \"@/registry/miseui/ui/lens-math\"\r\n\r\nexport type LensVariant = \"circle\" | \"square\" | \"panel\" | \"strip\"\r\nexport type LensEffect = \"press\"\r\n\r\nconst GLIDE = \"transform var(--duration-slow) var(--ease-mise)\"\r\nconst ANCHOR: Record<Exclude<LensVariant, \"panel\">, LensAnchor> = { circle: \"centre\", square: \"above\", strip: \"band\" }\r\n\r\nfunction useMeasuredSize(ref: React.RefObject<HTMLElement | null>, watching: boolean) {\r\n  const [size, setSize] = useState<Size | null>(null)\r\n  useLayoutEffect(() => {\r\n    const element = ref.current\r\n    if (!watching || !element) return\r\n    const read = () => {\r\n      const { width, height } = element.getBoundingClientRect()\r\n      setSize((previous) => (previous?.width === width && previous?.height === height ? previous : { width, height }))\r\n    }\r\n    read()\r\n    if (typeof ResizeObserver === \"undefined\") return\r\n    const observer = new ResizeObserver(read)\r\n    observer.observe(element)\r\n    return () => observer.disconnect()\r\n  }, [ref, watching])\r\n  return size\r\n}\r\n\r\nexport interface LensOptions {\r\n  variant: LensVariant\r\n  effect?: LensEffect\r\n  zoom: number\r\n  size: number\r\n  defaultPoint?: Point\r\n}\r\n\r\ninterface LensLayers {\r\n  body: HTMLElement | null\r\n  copy: HTMLElement | null\r\n  region: HTMLElement | null\r\n}\r\n\r\nfunction paintLens(layers: LensLayers, variant: LensVariant, point: Point, box: Size, view: Size, zoom: number) {\r\n  const focus = lensFocus(point, box, view, zoom)\r\n  const offset = lensCopyOffset(focus, view, zoom)\r\n  if (layers.copy) layers.copy.style.transform = `translate(${offset.x}px, ${offset.y}px) scale(${zoom})`\r\n  if (variant === \"panel\") {\r\n    const region = lensRegion(focus, view, zoom)\r\n    if (layers.region) {\r\n      layers.region.style.width = `${region.width}px`\r\n      layers.region.style.height = `${region.height}px`\r\n      layers.region.style.transform = `translate(${region.x}px, ${region.y}px)`\r\n    }\r\n    return\r\n  }\r\n  const body = lensBodyPosition(point, box, view, ANCHOR[variant])\r\n  if (layers.body) layers.body.style.transform = `translate(${body.x}px, ${body.y}px)`\r\n}\r\n\r\ninterface VisibilityOptions {\r\n  paint: () => void\r\n  layers: () => readonly (HTMLElement | null)[]\r\n  pointRef: React.RefObject<Point | null>\r\n  resting: boolean\r\n  initiallyArmed: boolean\r\n}\r\n\r\nfunction useLensVisibility({ paint, layers, pointRef, resting, initiallyArmed }: VisibilityOptions) {\r\n  const reduceMotion = useReducedMotion() ?? false\r\n  const [shown, setShown] = useState(resting)\r\n  const [armed, setArmed] = useState(initiallyArmed)\r\n  const glide = (on: boolean) => {\r\n    const value = on && !reduceMotion ? GLIDE : \"\"\r\n    layers().forEach((layer) => {\r\n      if (layer) layer.style.transition = value\r\n    })\r\n  }\r\n  const show = (point?: Point) => {\r\n    if (point) pointRef.current = point\r\n    glide(false)\r\n    setArmed(true)\r\n    setShown(true)\r\n    paint()\r\n  }\r\n  /** Leaving glides a lens with a resting place back to it. Otherwise it fades where it stands, and Escape always hides it. */\r\n  const hide = (dismiss = false) => {\r\n    setShown(resting && !dismiss)\r\n    if (!resting) return\r\n    pointRef.current = null\r\n    glide(true)\r\n    paint()\r\n  }\r\n  return { shown, armed, show, hide }\r\n}\r\n\r\n/** Sizes and shown state live in React because they rarely change. Position is written straight to the layers. */\r\nexport function useLens({ variant, effect, zoom: zoomProp, size, defaultPoint }: LensOptions) {\r\n  const panel = variant === \"panel\"\r\n  const press = effect === \"press\"\r\n  const zoom = Math.max(1, zoomProp)\r\n  const contentRef = useRef<HTMLDivElement>(null)\r\n  const panelRef = useRef<HTMLDivElement>(null)\r\n  const bodyRef = useRef<HTMLDivElement>(null)\r\n  const copyRef = useRef<HTMLDivElement>(null)\r\n  const regionRef = useRef<HTMLDivElement>(null)\r\n  const pointRef = useRef<Point | null>(null)\r\n  const box = useMeasuredSize(contentRef, true)\r\n  const panelSize = useMeasuredSize(panelRef, panel)\r\n  const resting = defaultPoint !== undefined && !press\r\n  const view = useMemo(() => {\r\n    if (!box) return null\r\n    return panel ? panelSize : lensViewSize(ANCHOR[variant as Exclude<LensVariant, \"panel\">], size, box)\r\n  }, [box, panel, panelSize, size, variant])\r\n\r\n  const paint = useCallback(() => {\r\n    if (!box || !view) return\r\n    const point = pointRef.current ?? lensPointFromFraction(defaultPoint, box)\r\n    paintLens({ body: bodyRef.current, copy: copyRef.current, region: regionRef.current }, variant, point, box, view, zoom)\r\n  }, [box, defaultPoint, variant, view, zoom])\r\n\r\n  const { shown, armed, show, hide } = useLensVisibility({ paint, layers: () => [bodyRef.current, copyRef.current, regionRef.current], pointRef, resting, initiallyArmed: panel || resting })\r\n  useLayoutEffect(paint, [paint, armed, shown])\r\n  const pointer = useLensPointer({ contentRef, press, shown, show, hide })\r\n  const keys = useLensKeys({ box, pointRef, defaultPoint, show, hide })\r\n  return { contentRef, panelRef, bodyRef, copyRef, regionRef, box, view, zoom, shown, armed: armed && box !== null, pointer, keys }\r\n}\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/pointer-fine.ts",
      "content": "/**\n * Asked when an event arrives, never during render, so a server render hydrates and a laptop with a touch screen\n * answers for the input it is using right now.\n */\nexport function isFinePointer() {\n  return typeof window !== \"undefined\" && window.matchMedia(\"(hover: hover) and (pointer: fine)\").matches\n}\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "interactions"
  ],
  "type": "registry:ui"
}