{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "morph-lightbox",
  "title": "Morph Lightbox",
  "description": "An image lightbox that grows out of the thumbnail you clicked. The square crop opens up to the photo's true aspect ratio over a page-coloured scrim, slides ride native scroll snapping, and pulling the photo down shrinks it back into the cell of the photo you are on.",
  "dependencies": [
    "@rexa-developer/tiks",
    "motion",
    "web-haptics"
  ],
  "files": [
    {
      "path": "src/registry/components/morph-lightbox/morph-lightbox.tsx",
      "content": "\"use client\";\n\nimport { useTiks } from \"@rexa-developer/tiks/react\";\nimport {\n\tAnimatePresence,\n\ttype AnimationPlaybackControls,\n\tanimate,\n\ttype MotionValue,\n\tmotion,\n\tuseMotionValue,\n\tuseReducedMotion,\n} from \"motion/react\";\nimport {\n\ttype ComponentProps,\n\tcreateContext,\n\ttype KeyboardEvent,\n\ttype MouseEvent,\n\ttype PointerEvent,\n\ttype ReactNode,\n\tuseCallback,\n\tuseContext,\n\tuseEffect,\n\tuseId,\n\tuseLayoutEffect,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from \"react\";\nimport { createPortal, flushSync } from \"react-dom\";\nimport { useWebHaptics } from \"web-haptics/react\";\nimport { cn } from \"@/lib/utils\";\n\n// Motion. The photo itself rides one 0 → 1 progress per flight; the scrim and\n// the chrome (toolbar, neighbours, caption, filmstrip) fade on their own clocks.\nconst openSpring = { type: \"spring\", stiffness: 300, damping: 32 } as const;\nconst closeSpring = { type: \"spring\", duration: 0.32, bounce: 0 } as const;\nconst returnSpring = { type: \"spring\", stiffness: 380, damping: 34 } as const;\nconst slideSpring = { type: \"spring\", duration: 0.42, bounce: 0 } as const;\nconst filmstripSpring = { type: \"spring\", duration: 0.36, bounce: 0 } as const;\n/** Off-screen thumbnails get a short scale and fade instead of a morph. */\nconst offscreenTween = {\n\ttype: \"tween\",\n\tduration: 0.22,\n\tease: [0.2, 0, 0, 1],\n} as const;\nconst SCRIM_IN = 0.24;\nconst SCRIM_OUT = 0.22;\nconst CHROME_DELAY = 0.12;\nconst CHROME_IN = 0.2;\nconst CHROME_OUT = 0.12;\n/** Reduced motion opens, closes, and dismisses with this cross-fade. */\nconst REDUCED_FADE = 0.15;\nconst ROLL_DURATION = 0.26;\nconst ROLL_STAGGER = 0.03;\nconst ROLL_EASE = [0.3, 0.7, 0.2, 1] as const;\n\n// Shape.\n/** Corner radius of an open slide, in px. Thumbnails keep their own. */\nconst SLIDE_RADIUS = 6;\n/** Used when a thumbnail's own radius can't be read. */\nconst THUMB_RADIUS = 12;\n/** Scale an off-screen flight starts from or ends at. */\nconst OFFSCREEN_SCALE = 0.92;\n\n// Pull to dismiss.\n/** Pointer travel, in px, before a press picks an axis. */\nconst AXIS_SLOP = 8;\n/** Downward travel, in px, past which a release closes the viewer. */\nconst DISMISS_DISTANCE = 120;\n/** Downward release speed, in px/s, that closes the viewer from anywhere. */\nconst DISMISS_VELOCITY = 600;\n/** The photo shrinks to this scale by `SHRINK_RANGE` of the viewport height. */\nconst MIN_SCALE = 0.75;\nconst SHRINK_RANGE = 0.4;\n/** Scrim opacity at full shrink. */\nconst MIN_SCRIM = 0.2;\n/** Share of an upward drag the photo follows. */\nconst RUBBER_BAND = 0.25;\n/** Ms without a wheel event before a trackpad pull counts as released. */\nconst WHEEL_IDLE = 140;\n/** Ms of samples used for the release speed. */\nconst VELOCITY_WINDOW = 100;\n/** Ms after the last scroll event that counts as settled, where `scrollend` is missing. */\nconst SCROLL_IDLE = 120;\n\n/** A soft breath of air as the photo lifts out of the grid. */\nconst LIFT_SOUND = {\n\tname: \"morph-lightbox-lift\",\n\tbaseFreq: 440,\n\tnoiseColor: \"pink\",\n\toscType: \"sine\",\n\tfilterFreq: 900,\n\tfilterQ: 0.7,\n\tattack: 0.004,\n\tdecay: 1.4,\n\tbrightness: 2000,\n} as const;\n/** A muted tap as the photo settles back into its cell. */\nconst SETTLE_SOUND = {\n\tname: \"morph-lightbox-settle\",\n\tbaseFreq: 440,\n\tnoiseColor: \"pink\",\n\toscType: \"sine\",\n\tfilterFreq: 1500,\n\tfilterQ: 1.2,\n\tattack: 0.002,\n\tdecay: 0.8,\n\tbrightness: 2000,\n} as const;\n\nexport interface MorphLightboxItem {\n\t/** Full-size image shown in the viewer. */\n\tsrc: string;\n\t/** Describes the photo. It names the slide and the thumbnail button. */\n\talt: string;\n\t/** Intrinsic width; with `height` it sets the slide's aspect ratio. */\n\twidth: number;\n\theight: number;\n\t/** Shown under the slide and read as the slide's description. */\n\tcaption?: ReactNode;\n\t/** Smaller image for thumbnails and the filmstrip. Defaults to `src`. */\n\tthumbnailSrc?: string;\n}\n\nexport interface MorphLightboxProps {\n\titems: MorphLightboxItem[];\n\t/** Controlled slide index. */\n\tindex?: number;\n\t/** Slide shown first when uncontrolled. */\n\tdefaultIndex?: number;\n\t/** Called when a swipe, key, or click lands on another slide. */\n\tonIndexChange?: (index: number) => void;\n\t/** Controlled open state. */\n\topen?: boolean;\n\t/** Initial open state when uncontrolled. Opens without the morph. */\n\tdefaultOpen?: boolean;\n\tonOpenChange?: (open: boolean) => void;\n\t/** Wrap from the last slide to the first with keys and buttons. */\n\tloop?: boolean;\n\t/** Show a strip of thumbnails under the slide. */\n\tfilmstrip?: boolean;\n\t/** Play a soft lift on open and a settle when the photo lands on close. */\n\tsound?: boolean;\n\t/** Trigger haptics on supported devices. */\n\thaptics?: boolean;\n\t/** Classes for the viewer, the full-screen dialog. */\n\tclassName?: string;\n\t/** Your grid of `MorphLightboxTrigger`s, or any other content. */\n\tchildren?: ReactNode;\n}\n\ntype Rect = { x: number; y: number; width: number; height: number };\ntype Phase =\n\t| \"closed\"\n\t| \"opening\"\n\t| \"open\"\n\t| \"dragging\"\n\t| \"returning\"\n\t| \"closing\";\ntype Drag = {\n\t/** The slide's rect when the drag began. */\n\tbase: Rect;\n\t/** Point the photo scales around: the grab point, or the slide centre. */\n\toriginX: number;\n\toriginY: number;\n\tdx: number;\n\tdy: number;\n\twheel: boolean;\n\tpast: boolean;\n\tsamples: { t: number; y: number }[];\n};\ntype PointerGesture = {\n\tpointerId: number;\n\tstartX: number;\n\tstartY: number;\n\tdecided: \"dismiss\" | \"scroll\" | null;\n};\ntype Flight = {\n\tfrom: Rect;\n\tto: Rect;\n\tfromRadius: number;\n\ttoRadius: number;\n\tfromOpacity?: number;\n\ttoOpacity?: number;\n\ttransition:\n\t\t| typeof openSpring\n\t\t| typeof closeSpring\n\t\t| typeof returnSpring\n\t\t| typeof offscreenTween;\n\tvelocity?: number;\n\tonComplete: () => void;\n};\n\ntype MorphLightboxContextValue = {\n\titems: MorphLightboxItem[];\n\thiddenIndex: number | null;\n\tregister: (index: number, node: HTMLElement | null) => void;\n\topenAt: (index: number) => void;\n\tpreload: (index: number) => void;\n};\n\nconst MorphLightboxContext = createContext<MorphLightboxContextValue | null>(\n\tnull,\n);\n\nfunction useMorphLightbox() {\n\tconst context = useContext(MorphLightboxContext);\n\tif (!context) {\n\t\tthrow new Error(\n\t\t\t\"MorphLightboxTrigger must be rendered inside <MorphLightbox>.\",\n\t\t);\n\t}\n\treturn context;\n}\n\nfunction clamp(value: number, min: number, max: number) {\n\treturn Math.min(Math.max(value, min), max);\n}\n\nfunction mix(from: number, to: number, amount: number) {\n\treturn from + (to - from) * amount;\n}\n\nfunction mixRect(from: Rect, to: Rect, amount: number): Rect {\n\treturn {\n\t\tx: mix(from.x, to.x, amount),\n\t\ty: mix(from.y, to.y, amount),\n\t\twidth: mix(from.width, to.width, amount),\n\t\theight: mix(from.height, to.height, amount),\n\t};\n}\n\nfunction scaleRect(rect: Rect, scale: number): Rect {\n\tconst width = rect.width * scale;\n\tconst height = rect.height * scale;\n\treturn {\n\t\tx: rect.x + (rect.width - width) / 2,\n\t\ty: rect.y + (rect.height - height) / 2,\n\t\twidth,\n\t\theight,\n\t};\n}\n\nfunction rectOf(element: Element): Rect {\n\tconst { left, top, width, height } = element.getBoundingClientRect();\n\treturn { x: left, y: top, width, height };\n}\n\nfunction centerX(element: Element) {\n\tconst { left, width } = element.getBoundingClientRect();\n\treturn left + width / 2;\n}\n\nfunction onScreen(rect: Rect) {\n\treturn (\n\t\trect.width > 0 &&\n\t\trect.height > 0 &&\n\t\trect.x + rect.width > 0 &&\n\t\trect.y + rect.height > 0 &&\n\t\trect.x < window.innerWidth &&\n\t\trect.y < window.innerHeight\n\t);\n}\n\nfunction radiusOf(element: Element) {\n\tconst radius = Number.parseFloat(\n\t\tgetComputedStyle(element).borderTopLeftRadius,\n\t);\n\treturn Number.isFinite(radius) ? radius : THUMB_RADIUS;\n}\n\n/** Px/s over the last `VELOCITY_WINDOW` ms of samples. */\nfunction velocityOf(samples: { t: number; y: number }[]) {\n\tconst last = samples[samples.length - 1];\n\tif (!last) return 0;\n\tconst first =\n\t\tsamples.find((sample) => last.t - sample.t <= VELOCITY_WINDOW) ?? last;\n\tconst elapsed = last.t - first.t;\n\treturn elapsed > 0 ? ((last.y - first.y) / elapsed) * 1000 : 0;\n}\n\n/** Locks page scroll, keeping the scrollbar's gutter so nothing shifts. */\nfunction lockScroll() {\n\tconst root = document.documentElement;\n\tconst { overflow, scrollbarGutter } = root.style;\n\tconst gutter = window.innerWidth - root.clientWidth;\n\tif (gutter > 0 && getComputedStyle(root).scrollbarGutter === \"auto\") {\n\t\troot.style.scrollbarGutter = \"stable\";\n\t}\n\troot.style.overflow = \"hidden\";\n\treturn () => {\n\t\troot.style.overflow = overflow;\n\t\troot.style.scrollbarGutter = scrollbarGutter;\n\t};\n}\n\n/** Makes everything but `keep` inert, so focus and clicks stay in the viewer. */\nfunction inertOthers(keep: Element) {\n\tconst changed: Element[] = [];\n\tfor (const element of Array.from(document.body.children)) {\n\t\tif (element === keep || element.hasAttribute(\"inert\")) continue;\n\t\tif (element.tagName === \"SCRIPT\" || element.tagName === \"STYLE\") continue;\n\t\telement.setAttribute(\"inert\", \"\");\n\t\tchanged.push(element);\n\t}\n\treturn () => {\n\t\tfor (const element of changed) element.removeAttribute(\"inert\");\n\t};\n}\n\nfunction silent() {\n\treturn document.visibilityState === \"hidden\";\n}\n\nfunction MorphLightbox({\n\titems,\n\tindex: indexProp,\n\tdefaultIndex = 0,\n\tonIndexChange,\n\topen: openProp,\n\tdefaultOpen = false,\n\tonOpenChange,\n\tloop = false,\n\tfilmstrip = true,\n\tsound = true,\n\thaptics = true,\n\tclassName,\n\tchildren,\n}: MorphLightboxProps) {\n\tconst count = items.length;\n\tconst lastIndex = Math.max(0, count - 1);\n\tconst [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);\n\tconst open = (openProp ?? uncontrolledOpen) && count > 0;\n\tconst [uncontrolledIndex, setUncontrolledIndex] = useState(defaultIndex);\n\tconst index = clamp(indexProp ?? uncontrolledIndex, 0, lastIndex);\n\n\t// Stays mounted through the close flight, then unmounts.\n\tconst [rendered, setRendered] = useState(open);\n\t/** The slide in view, read from the track's scroll position. */\n\tconst [current, setCurrent] = useState(index);\n\tconst userOpenRef = useRef(false);\n\tif (open && !rendered) {\n\t\tsetRendered(true);\n\t\tif (!userOpenRef.current) setCurrent(index);\n\t}\n\tconst shownIndex = clamp(current, 0, lastIndex);\n\n\t// The viewer portals into `document.body`, which only exists after hydration.\n\tconst [mounted, setMounted] = useState(false);\n\tuseEffect(() => setMounted(true), []);\n\n\tconst [announcement, setAnnouncement] = useState(\"\");\n\t/** Which way the counter rolls: 1 forward, -1 back. */\n\tconst [direction, setDirection] = useState(1);\n\tconst [viewer, setViewer] = useState<HTMLDivElement | null>(null);\n\n\tconst reducedMotion = useReducedMotion() ?? false;\n\tconst { trigger: haptic } = useWebHaptics();\n\t// Sound stays on under reduced motion; only movement is reduced.\n\tconst { swoosh: playLift } = useTiks({\n\t\ttheme: LIFT_SOUND,\n\t\trespectReducedMotion: false,\n\t});\n\tconst { click: playSettle } = useTiks({\n\t\ttheme: SETTLE_SOUND,\n\t\trespectReducedMotion: false,\n\t});\n\n\tconst ids = useId().replace(/[^\\w-]/g, \"\");\n\tconst slideId = (slide: number) => `morph-lightbox-${ids}-slide-${slide}`;\n\tconst captionId = (slide: number) => `morph-lightbox-${ids}-caption-${slide}`;\n\n\tconst scrim = useMotionValue(0);\n\tconst chrome = useMotionValue(0);\n\tconst fade = useMotionValue(1);\n\tconst outlineX = useMotionValue(0);\n\n\tconst triggersRef = useRef(new Map<number, HTMLElement>());\n\tconst slidesRef = useRef<(HTMLDivElement | null)[]>([]);\n\tconst framesRef = useRef<(HTMLDivElement | null)[]>([]);\n\tconst thumbsRef = useRef<(HTMLButtonElement | null)[]>([]);\n\tconst trackRef = useRef<HTMLDivElement | null>(null);\n\tconst filmstripRef = useRef<HTMLDivElement | null>(null);\n\tconst filmRowRef = useRef<HTMLDivElement | null>(null);\n\tconst flyerRef = useRef<HTMLDivElement | null>(null);\n\n\tconst phaseRef = useRef<Phase>(\"closed\");\n\tconst currentRef = useRef(current);\n\tconst rtlRef = useRef(false);\n\tconst instantOpenRef = useRef(open);\n\tconst releaseRef = useRef<(() => void) | null>(null);\n\tconst returnFocusRef = useRef<HTMLElement | null>(null);\n\tconst flightRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst scrimAnimRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst chromeAnimRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst fadeAnimRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst scrollAnimRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst filmAnimRef = useRef<AnimationPlaybackControls | null>(null);\n\t/** Where the flyer is drawn now, or null when the slide shows itself. */\n\tconst flyerStateRef = useRef<{\n\t\trect: Rect;\n\t\tradius: number;\n\t\topacity: number;\n\t} | null>(null);\n\tconst dragRef = useRef<Drag | null>(null);\n\tconst gestureRef = useRef<PointerGesture | null>(null);\n\tconst wheelTimerRef = useRef<number | undefined>(undefined);\n\tconst scrollTimerRef = useRef<number | undefined>(undefined);\n\t/** Slide a touch swipe started on, for the snap haptic. */\n\tconst swipeRef = useRef<number | null>(null);\n\tconst suppressClickRef = useRef(false);\n\t/** Slides that have been near the current one, so their images stay loaded. */\n\tconst seenRef = useRef(new Set<number>());\n\n\t// Latest values for the imperative handlers below, so they never re-bind.\n\tconst latest = useRef({\n\t\titems,\n\t\topen,\n\t\tindexProp,\n\t\tloop,\n\t\tsound,\n\t\thaptics,\n\t\treducedMotion,\n\t\tonIndexChange,\n\t\tonOpenChange,\n\t});\n\tuseLayoutEffect(() => {\n\t\tlatest.current = {\n\t\t\titems,\n\t\t\topen,\n\t\t\tindexProp,\n\t\t\tloop,\n\t\t\tsound,\n\t\t\thaptics,\n\t\t\treducedMotion,\n\t\t\tonIndexChange,\n\t\t\tonOpenChange,\n\t\t};\n\t});\n\n\tconst setPhase = useCallback(\n\t\t(phase: Phase) => {\n\t\t\tphaseRef.current = phase;\n\t\t\tif (viewer) viewer.dataset.phase = phase;\n\t\t},\n\t\t[viewer],\n\t);\n\n\tconst fadeTo = useCallback(\n\t\t(\n\t\t\tref: { current: AnimationPlaybackControls | null },\n\t\t\tvalue: MotionValue<number>,\n\t\t\tto: number,\n\t\t\tduration: number,\n\t\t\tdelay = 0,\n\t\t\tonComplete?: () => void,\n\t\t) => {\n\t\t\tref.current?.stop();\n\t\t\tref.current = animate(value, to, {\n\t\t\t\tduration,\n\t\t\t\tdelay,\n\t\t\t\tease: \"easeOut\",\n\t\t\t\tonComplete: () => {\n\t\t\t\t\tref.current = null;\n\t\t\t\t\tonComplete?.();\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\t[],\n\t);\n\n\tconst drawFlyer = useCallback((rect: Rect, radius: number, opacity = 1) => {\n\t\tconst flyer = flyerRef.current;\n\t\tif (!flyer) return;\n\t\tflyerStateRef.current = { rect, radius, opacity };\n\t\tflyer.style.visibility = \"visible\";\n\t\tflyer.style.transform = `translate(${rect.x}px, ${rect.y}px)`;\n\t\tflyer.style.width = `${rect.width}px`;\n\t\tflyer.style.height = `${rect.height}px`;\n\t\tflyer.style.borderRadius = `${radius}px`;\n\t\tflyer.style.opacity = String(opacity);\n\t}, []);\n\n\tconst hideFlyer = useCallback(() => {\n\t\tflightRef.current?.stop();\n\t\tflightRef.current = null;\n\t\tflyerStateRef.current = null;\n\t\tconst flyer = flyerRef.current;\n\t\tif (flyer) flyer.style.visibility = \"hidden\";\n\t}, []);\n\n\t/** Hides the real slide while the flyer stands in for it. */\n\tconst setSlideHidden = useCallback((slide: number, hidden: boolean) => {\n\t\tconst frame = framesRef.current[slide];\n\t\tif (frame) frame.style.visibility = hidden ? \"hidden\" : \"\";\n\t}, []);\n\n\tconst fly = useCallback(\n\t\t({\n\t\t\tfrom,\n\t\t\tto,\n\t\t\tfromRadius,\n\t\t\ttoRadius,\n\t\t\tfromOpacity = 1,\n\t\t\ttoOpacity = 1,\n\t\t\ttransition,\n\t\t\tvelocity = 0,\n\t\t\tonComplete,\n\t\t}: Flight) => {\n\t\t\tflightRef.current?.stop();\n\t\t\tdrawFlyer(from, fromRadius, fromOpacity);\n\t\t\tflightRef.current = animate(0, 1, {\n\t\t\t\t...transition,\n\t\t\t\t...(transition.type === \"spring\"\n\t\t\t\t\t? { velocity, restDelta: 0.001, restSpeed: 0.01 }\n\t\t\t\t\t: {}),\n\t\t\t\tonUpdate: (amount) =>\n\t\t\t\t\tdrawFlyer(\n\t\t\t\t\t\tmixRect(from, to, amount),\n\t\t\t\t\t\tmix(fromRadius, toRadius, amount),\n\t\t\t\t\t\tmix(fromOpacity, toOpacity, amount),\n\t\t\t\t\t),\n\t\t\t\tonComplete: () => {\n\t\t\t\t\tflightRef.current = null;\n\t\t\t\t\tonComplete();\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\t[drawFlyer],\n\t);\n\n\tconst stopScroll = useCallback(() => {\n\t\tscrollAnimRef.current?.stop();\n\t\tscrollAnimRef.current = null;\n\t\tif (trackRef.current) trackRef.current.style.scrollSnapType = \"\";\n\t}, []);\n\n\t/** Records a new current slide and tells whoever listens. */\n\tconst commitCurrent = useCallback(\n\t\t(next: number, step: number, report = true) => {\n\t\t\tif (next === currentRef.current) return;\n\t\t\tconst { items, indexProp, onIndexChange } = latest.current;\n\t\t\tcurrentRef.current = next;\n\t\t\tsetDirection(step);\n\t\t\tsetCurrent(next);\n\t\t\tsetAnnouncement(\n\t\t\t\t`Image ${next + 1} of ${items.length}: ${items[next]?.alt ?? \"\"}`,\n\t\t\t);\n\t\t\tif (indexProp === undefined) setUncontrolledIndex(next);\n\t\t\tif (report) onIndexChange?.(next);\n\t\t},\n\t\t[],\n\t);\n\n\t/** Picks the slide whose centre is nearest the track's centre. */\n\tconst nearestSlide = useCallback(() => {\n\t\tconst track = trackRef.current;\n\t\tif (!track) return currentRef.current;\n\t\tconst middle = centerX(track);\n\t\tlet nearest = currentRef.current;\n\t\tlet distance = Number.POSITIVE_INFINITY;\n\t\tslidesRef.current.forEach((slide, slideIndex) => {\n\t\t\tif (!slide) return;\n\t\t\tconst gap = Math.abs(centerX(slide) - middle);\n\t\t\tif (gap < distance) {\n\t\t\t\tdistance = gap;\n\t\t\t\tnearest = slideIndex;\n\t\t\t}\n\t\t});\n\t\treturn nearest;\n\t}, []);\n\n\tconst settleScroll = useCallback(() => {\n\t\twindow.clearTimeout(scrollTimerRef.current);\n\t\tif (scrollAnimRef.current) return;\n\t\tconst phase = phaseRef.current;\n\t\tif (phase !== \"open\") return;\n\t\tconst next = nearestSlide();\n\t\tconst started = swipeRef.current;\n\t\tswipeRef.current = null;\n\t\tcommitCurrent(next, next > currentRef.current ? 1 : -1);\n\t\tif (\n\t\t\tstarted !== null &&\n\t\t\tstarted !== next &&\n\t\t\tlatest.current.haptics &&\n\t\t\t!silent()\n\t\t) {\n\t\t\tvoid haptic(\"selection\");\n\t\t}\n\t}, [commitCurrent, haptic, nearestSlide]);\n\n\tconst scrollToSlide = useCallback(\n\t\t(slide: number, animated: boolean) => {\n\t\t\tconst track = trackRef.current;\n\t\t\tconst target = slidesRef.current[slide];\n\t\t\tif (!track || !target) return;\n\t\t\tstopScroll();\n\t\t\tconst delta = centerX(target) - centerX(track);\n\t\t\t// `snap-always` would stop a jump at the first slide on the way, and\n\t\t\t// mandatory snapping would pull every frame of a glide back to a slide.\n\t\t\ttrack.style.scrollSnapType = \"none\";\n\t\t\tif (!animated || Math.abs(delta) < 1) {\n\t\t\t\ttrack.scrollBy({ left: delta, behavior: \"instant\" });\n\t\t\t\ttrack.style.scrollSnapType = \"\";\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst from = track.scrollLeft;\n\t\t\tscrollAnimRef.current = animate(from, from + delta, {\n\t\t\t\t...slideSpring,\n\t\t\t\tonUpdate: (left) => {\n\t\t\t\t\ttrack.scrollLeft = left;\n\t\t\t\t},\n\t\t\t\tonComplete: () => {\n\t\t\t\t\tscrollAnimRef.current = null;\n\t\t\t\t\ttrack.style.scrollSnapType = \"\";\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\t[stopScroll],\n\t);\n\n\tconst goTo = useCallback(\n\t\t(requested: number, report = true) => {\n\t\t\tif (phaseRef.current !== \"open\") return;\n\t\t\tconst total = latest.current.items.length;\n\t\t\tif (total < 2) return;\n\t\t\tconst from = currentRef.current;\n\t\t\tconst next = latest.current.loop\n\t\t\t\t? (requested + total) % total\n\t\t\t\t: clamp(requested, 0, total - 1);\n\t\t\tif (next === from) return;\n\t\t\t// Wrapping past an end still rolls the way the user went.\n\t\t\tconst step =\n\t\t\t\trequested !== next ? (requested > from ? 1 : -1) : next > from ? 1 : -1;\n\t\t\tswipeRef.current = null;\n\t\t\tcommitCurrent(next, step, report);\n\t\t\tscrollToSlide(next, !latest.current.reducedMotion);\n\t\t},\n\t\t[commitCurrent, scrollToSlide],\n\t);\n\n\tconst setOpenState = useCallback((next: boolean) => {\n\t\tif (latest.current.open === next) return;\n\t\tsetUncontrolledOpen(next);\n\t\tlatest.current.onOpenChange?.(next);\n\t}, []);\n\n\tconst finishClose = useCallback(\n\t\t(cue: boolean) => {\n\t\t\tsetPhase(\"closed\");\n\t\t\thideFlyer();\n\t\t\tstopScroll();\n\t\t\treleaseRef.current?.();\n\t\t\treleaseRef.current = null;\n\t\t\tflushSync(() => setRendered(false));\n\t\t\tconst trigger = triggersRef.current.get(currentRef.current);\n\t\t\tconst target =\n\t\t\t\ttrigger?.isConnected && trigger.offsetParent !== null\n\t\t\t\t\t? trigger\n\t\t\t\t\t: returnFocusRef.current;\n\t\t\treturnFocusRef.current = null;\n\t\t\ttarget?.focus({ preventScroll: true });\n\t\t\tscrim.jump(0);\n\t\t\tchrome.jump(0);\n\t\t\tfade.jump(1);\n\t\t\tif (cue && latest.current.sound && !silent()) playSettle();\n\t\t},\n\t\t[chrome, fade, hideFlyer, playSettle, scrim, setPhase, stopScroll],\n\t);\n\n\tconst beginClose = useCallback(\n\t\t(cue: boolean, velocity = 0) => {\n\t\t\tconst phase = phaseRef.current;\n\t\t\tif (phase === \"closed\" || phase === \"closing\") return;\n\t\t\tsetPhase(\"closing\");\n\t\t\twindow.clearTimeout(wheelTimerRef.current);\n\t\t\tdragRef.current = null;\n\t\t\tgestureRef.current = null;\n\t\t\tstopScroll();\n\t\t\tconst slide = currentRef.current;\n\t\t\tfadeTo(chromeAnimRef, chrome, 0, CHROME_OUT);\n\t\t\tfadeTo(scrimAnimRef, scrim, 0, SCRIM_OUT);\n\t\t\tif (latest.current.reducedMotion) {\n\t\t\t\tfadeTo(fadeAnimRef, fade, 0, REDUCED_FADE, 0, () => finishClose(cue));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst frame = framesRef.current[slide];\n\t\t\tconst flying = flyerStateRef.current;\n\t\t\tconst from = flying?.rect ?? (frame ? rectOf(frame) : null);\n\t\t\tif (!from) {\n\t\t\t\tfinishClose(cue);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst fromRadius = flying?.radius ?? SLIDE_RADIUS;\n\t\t\tconst fromOpacity = flying?.opacity ?? 1;\n\t\t\tsetSlideHidden(slide, true);\n\t\t\tconst trigger = triggersRef.current.get(slide);\n\t\t\tconst to = trigger?.isConnected ? rectOf(trigger) : null;\n\t\t\tif (to && onScreen(to)) {\n\t\t\t\t// Carry the release speed into the flight, as progress per second.\n\t\t\t\tconst travel = to.y - from.y;\n\t\t\t\tconst progressVelocity =\n\t\t\t\t\tMath.abs(travel) > 1 && Math.sign(travel) === Math.sign(velocity)\n\t\t\t\t\t\t? clamp(velocity / travel, 0, 8)\n\t\t\t\t\t\t: 0;\n\t\t\t\tfly({\n\t\t\t\t\tfrom,\n\t\t\t\t\tto,\n\t\t\t\t\tfromRadius,\n\t\t\t\t\ttoRadius: radiusOf(trigger as HTMLElement),\n\t\t\t\t\tfromOpacity,\n\t\t\t\t\ttransition: closeSpring,\n\t\t\t\t\tvelocity: progressVelocity,\n\t\t\t\t\tonComplete: () => finishClose(cue),\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfly({\n\t\t\t\tfrom,\n\t\t\t\tto: scaleRect(from, OFFSCREEN_SCALE),\n\t\t\t\tfromRadius,\n\t\t\t\ttoRadius: fromRadius,\n\t\t\t\tfromOpacity,\n\t\t\t\ttoOpacity: 0,\n\t\t\t\ttransition: offscreenTween,\n\t\t\t\tonComplete: () => finishClose(cue),\n\t\t\t});\n\t\t},\n\t\t[\n\t\t\tchrome,\n\t\t\tfade,\n\t\t\tfadeTo,\n\t\t\tfinishClose,\n\t\t\tfly,\n\t\t\tscrim,\n\t\t\tsetPhase,\n\t\t\tsetSlideHidden,\n\t\t\tstopScroll,\n\t\t],\n\t);\n\n\t/** Closes from user input: plays the settle and reports the change. */\n\tconst requestClose = useCallback(\n\t\t(velocity = 0) => {\n\t\t\tconst phase = phaseRef.current;\n\t\t\tif (phase === \"closed\" || phase === \"closing\") return;\n\t\t\tsetOpenState(false);\n\t\t\tbeginClose(true, velocity);\n\t\t},\n\t\t[beginClose, setOpenState],\n\t);\n\n\tconst startOpen = useCallback(() => {\n\t\tconst root = viewer;\n\t\tif (!root) return;\n\t\tconst slide = currentRef.current;\n\t\tsetPhase(\"opening\");\n\t\tseenRef.current.clear();\n\n\t\t// The viewer is portaled out of the page, so it copies the page's direction.\n\t\tconst trigger = triggersRef.current.get(slide);\n\t\tconst source = trigger ?? triggersRef.current.values().next().value;\n\t\tconst dir = getComputedStyle(source ?? document.documentElement).direction;\n\t\troot.dir = dir === \"rtl\" ? \"rtl\" : \"ltr\";\n\t\trtlRef.current = dir === \"rtl\";\n\n\t\tconst active = document.activeElement;\n\t\treturnFocusRef.current =\n\t\t\tactive instanceof HTMLElement && active !== document.body ? active : null;\n\t\tconst unlock = lockScroll();\n\t\tconst restore = inertOthers(root);\n\t\treleaseRef.current = () => {\n\t\t\tunlock();\n\t\t\trestore();\n\t\t};\n\t\tscrollToSlide(slide, false);\n\t\troot.focus({ preventScroll: true });\n\n\t\tconst cue = userOpenRef.current;\n\t\tuserOpenRef.current = false;\n\t\tif (cue && latest.current.sound && !silent()) playLift();\n\n\t\tconst land = () => {\n\t\t\tsetSlideHidden(slide, false);\n\t\t\thideFlyer();\n\t\t\tsetPhase(\"open\");\n\t\t};\n\n\t\tif (instantOpenRef.current) {\n\t\t\tinstantOpenRef.current = false;\n\t\t\tscrim.jump(1);\n\t\t\tchrome.jump(1);\n\t\t\tfade.jump(1);\n\t\t\tsetPhase(\"open\");\n\t\t\treturn;\n\t\t}\n\t\tif (latest.current.reducedMotion) {\n\t\t\tscrim.jump(1);\n\t\t\tchrome.jump(1);\n\t\t\tfade.jump(0);\n\t\t\tfadeTo(fadeAnimRef, fade, 1, REDUCED_FADE, 0, () => setPhase(\"open\"));\n\t\t\treturn;\n\t\t}\n\n\t\tfade.jump(1);\n\t\tscrim.jump(0);\n\t\tchrome.jump(0);\n\t\tfadeTo(scrimAnimRef, scrim, 1, SCRIM_IN);\n\t\tfadeTo(chromeAnimRef, chrome, 1, CHROME_IN, CHROME_DELAY);\n\t\tconst frame = framesRef.current[slide];\n\t\tif (!frame) {\n\t\t\tsetPhase(\"open\");\n\t\t\treturn;\n\t\t}\n\t\tconst to = rectOf(frame);\n\t\tconst from = trigger?.isConnected ? rectOf(trigger) : null;\n\t\tsetSlideHidden(slide, true);\n\t\tif (from && onScreen(from)) {\n\t\t\tfly({\n\t\t\t\tfrom,\n\t\t\t\tto,\n\t\t\t\tfromRadius: radiusOf(trigger as HTMLElement),\n\t\t\t\ttoRadius: SLIDE_RADIUS,\n\t\t\t\ttransition: openSpring,\n\t\t\t\tonComplete: land,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tfly({\n\t\t\tfrom: scaleRect(to, OFFSCREEN_SCALE),\n\t\t\tto,\n\t\t\tfromRadius: SLIDE_RADIUS,\n\t\t\ttoRadius: SLIDE_RADIUS,\n\t\t\tfromOpacity: 0,\n\t\t\ttoOpacity: 1,\n\t\t\ttransition: offscreenTween,\n\t\t\tonComplete: land,\n\t\t});\n\t}, [\n\t\tchrome,\n\t\tfade,\n\t\tfadeTo,\n\t\tfly,\n\t\thideFlyer,\n\t\tplayLift,\n\t\tscrim,\n\t\tscrollToSlide,\n\t\tsetPhase,\n\t\tsetSlideHidden,\n\t\tviewer,\n\t]);\n\n\t// Open and close from state, whether it changed here or through props.\n\tuseLayoutEffect(() => {\n\t\tif (!viewer) return;\n\t\tconst phase = phaseRef.current;\n\t\tif (open) {\n\t\t\tif (phase === \"closed\") startOpen();\n\t\t\treturn;\n\t\t}\n\t\t// Closes that come through props are silent.\n\t\tif (phase !== \"closed\" && phase !== \"closing\") beginClose(false);\n\t}, [open, viewer, startOpen, beginClose]);\n\n\t// A controlled index that changes while open glides there.\n\tuseEffect(() => {\n\t\tif (indexProp === undefined || !viewer) return;\n\t\tconst next = clamp(indexProp, 0, lastIndex);\n\t\tif (next === currentRef.current) return;\n\t\tgoTo(next, false);\n\t}, [indexProp, lastIndex, viewer, goTo]);\n\n\t// Keep the filmstrip's current thumbnail centred and outlined.\n\tuseLayoutEffect(() => {\n\t\tif (!viewer) return;\n\t\tconst strip = filmstripRef.current;\n\t\tconst row = filmRowRef.current;\n\t\tconst thumb = thumbsRef.current[shownIndex];\n\t\tconst visual = thumb?.firstElementChild;\n\t\tif (!strip || !row || !thumb || !visual) return;\n\t\t// Arrow keys on the filmstrip carry focus along with the outline.\n\t\tconst active = document.activeElement;\n\t\tif (active !== thumb && thumbsRef.current.includes(active as never)) {\n\t\t\tthumb.focus({ preventScroll: true });\n\t\t}\n\t\tconst instant =\n\t\t\tphaseRef.current === \"opening\" || latest.current.reducedMotion;\n\t\tconst x =\n\t\t\tvisual.getBoundingClientRect().left - row.getBoundingClientRect().left;\n\t\tfilmAnimRef.current?.stop();\n\t\tconst delta = centerX(thumb) - centerX(strip);\n\t\tif (instant) {\n\t\t\toutlineX.jump(x);\n\t\t\tstrip.scrollBy({ left: delta, behavior: \"instant\" });\n\t\t\treturn;\n\t\t}\n\t\tanimate(outlineX, x, filmstripSpring);\n\t\tconst from = strip.scrollLeft;\n\t\tconst max = strip.scrollWidth - strip.clientWidth;\n\t\tconst to = rtlRef.current\n\t\t\t? clamp(from + delta, -max, 0)\n\t\t\t: clamp(from + delta, 0, max);\n\t\tif (Math.abs(to - from) < 1) return;\n\t\tfilmAnimRef.current = animate(from, to, {\n\t\t\t...filmstripSpring,\n\t\t\tonUpdate: (left) => {\n\t\t\t\tstrip.scrollLeft = left;\n\t\t\t},\n\t\t});\n\t}, [viewer, shownIndex, outlineX]);\n\n\t// Release everything if the lightbox unmounts while open.\n\tuseEffect(\n\t\t() => () => {\n\t\t\treleaseRef.current?.();\n\t\t\treleaseRef.current = null;\n\t\t\twindow.clearTimeout(wheelTimerRef.current);\n\t\t\twindow.clearTimeout(scrollTimerRef.current);\n\t\t},\n\t\t[],\n\t);\n\n\tconst beginDrag = useCallback(\n\t\t(drag: Drag) => {\n\t\t\tsetPhase(\"dragging\");\n\t\t\tstopScroll();\n\t\t\tswipeRef.current = null;\n\t\t\tdragRef.current = drag;\n\t\t\tif (!latest.current.reducedMotion) {\n\t\t\t\tsetSlideHidden(currentRef.current, true);\n\t\t\t\tdrawFlyer(drag.base, SLIDE_RADIUS);\n\t\t\t}\n\t\t\tfadeTo(chromeAnimRef, chrome, 0, CHROME_OUT);\n\t\t},\n\t\t[chrome, drawFlyer, fadeTo, setPhase, setSlideHidden, stopScroll],\n\t);\n\n\tconst updateDrag = useCallback(\n\t\t(drag: Drag, dx: number, dy: number, time: number) => {\n\t\t\tdrag.dx = dx;\n\t\t\tdrag.dy = dy;\n\t\t\tdrag.samples.push({ t: time, y: dy });\n\t\t\tif (drag.samples.length > 12) drag.samples.shift();\n\t\t\tconst travel = drag.wheel ? Math.abs(dy) : Math.max(dy, 0);\n\t\t\tconst amount = clamp(travel / (SHRINK_RANGE * window.innerHeight), 0, 1);\n\t\t\tscrim.set(1 - (1 - MIN_SCRIM) * amount);\n\t\t\tconst past = travel > DISMISS_DISTANCE;\n\t\t\tif (past && !drag.past && latest.current.haptics && !silent()) {\n\t\t\t\tvoid haptic(\"light\");\n\t\t\t}\n\t\t\tdrag.past = past;\n\t\t\tif (latest.current.reducedMotion) {\n\t\t\t\tfade.set(1 - 0.4 * amount);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// Shrink around the grab point, so the photo stays under the finger.\n\t\t\tconst scale = 1 - (1 - MIN_SCALE) * amount;\n\t\t\tconst y = !drag.wheel && dy < 0 ? dy * RUBBER_BAND : dy;\n\t\t\tconst { base, originX, originY } = drag;\n\t\t\tdrawFlyer(\n\t\t\t\t{\n\t\t\t\t\tx: originX + dx + (base.x - originX) * scale,\n\t\t\t\t\ty: originY + y + (base.y - originY) * scale,\n\t\t\t\t\twidth: base.width * scale,\n\t\t\t\t\theight: base.height * scale,\n\t\t\t\t},\n\t\t\t\tSLIDE_RADIUS,\n\t\t\t);\n\t\t},\n\t\t[drawFlyer, fade, haptic, scrim],\n\t);\n\n\tconst springBack = useCallback(() => {\n\t\tconst slide = currentRef.current;\n\t\tdragRef.current = null;\n\t\tsetPhase(\"returning\");\n\t\tfadeTo(scrimAnimRef, scrim, 1, CHROME_IN);\n\t\tfadeTo(chromeAnimRef, chrome, 1, CHROME_IN);\n\t\tif (latest.current.reducedMotion) {\n\t\t\tfadeTo(fadeAnimRef, fade, 1, REDUCED_FADE, 0, () => setPhase(\"open\"));\n\t\t\treturn;\n\t\t}\n\t\tconst frame = framesRef.current[slide];\n\t\tconst flying = flyerStateRef.current;\n\t\tif (!frame || !flying) {\n\t\t\tsetSlideHidden(slide, false);\n\t\t\thideFlyer();\n\t\t\tsetPhase(\"open\");\n\t\t\treturn;\n\t\t}\n\t\tfly({\n\t\t\tfrom: flying.rect,\n\t\t\tto: rectOf(frame),\n\t\t\tfromRadius: flying.radius,\n\t\t\ttoRadius: SLIDE_RADIUS,\n\t\t\ttransition: returnSpring,\n\t\t\tonComplete: () => {\n\t\t\t\tsetSlideHidden(slide, false);\n\t\t\t\thideFlyer();\n\t\t\t\tsetPhase(\"open\");\n\t\t\t},\n\t\t});\n\t}, [chrome, fade, fadeTo, fly, hideFlyer, scrim, setPhase, setSlideHidden]);\n\n\tconst releaseDrag = useCallback(\n\t\t(drag: Drag) => {\n\t\t\tconst velocity = velocityOf(drag.samples);\n\t\t\tconst travel = drag.wheel ? Math.abs(drag.dy) : drag.dy;\n\t\t\t// Wheel deltas arrive fast even for a short pull, and a real flick's\n\t\t\t// momentum carries it past the distance anyway, so wheels go by distance.\n\t\t\tif (\n\t\t\t\ttravel > DISMISS_DISTANCE ||\n\t\t\t\t(!drag.wheel && travel > 0 && velocity > DISMISS_VELOCITY)\n\t\t\t) {\n\t\t\t\trequestClose(velocity);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tspringBack();\n\t\t},\n\t\t[requestClose, springBack],\n\t);\n\n\t// Trackpads: a two-finger vertical swipe pulls the photo like a drag.\n\tuseEffect(() => {\n\t\tif (!viewer) return;\n\t\tconst onWheel = (event: WheelEvent) => {\n\t\t\tif (event.ctrlKey) return;\n\t\t\tlet drag = dragRef.current;\n\t\t\tif (!drag?.wheel) {\n\t\t\t\tif (phaseRef.current !== \"open\") {\n\t\t\t\t\tif (phaseRef.current !== \"closed\") event.preventDefault();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\t// Sideways swipes belong to the native scroller.\n\t\t\t\tif (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;\n\t\t\t\tconst frame = framesRef.current[currentRef.current];\n\t\t\t\tif (!frame) return;\n\t\t\t\tconst base = rectOf(frame);\n\t\t\t\tdrag = {\n\t\t\t\t\tbase,\n\t\t\t\t\toriginX: base.x + base.width / 2,\n\t\t\t\t\toriginY: base.y + base.height / 2,\n\t\t\t\t\tdx: 0,\n\t\t\t\t\tdy: 0,\n\t\t\t\t\twheel: true,\n\t\t\t\t\tpast: false,\n\t\t\t\t\tsamples: [{ t: event.timeStamp, y: 0 }],\n\t\t\t\t};\n\t\t\t\tbeginDrag(drag);\n\t\t\t}\n\t\t\tevent.preventDefault();\n\t\t\tconst unit =\n\t\t\t\tevent.deltaMode === 1\n\t\t\t\t\t? 16\n\t\t\t\t\t: event.deltaMode === 2\n\t\t\t\t\t\t? window.innerHeight\n\t\t\t\t\t\t: 1;\n\t\t\tupdateDrag(drag, 0, drag.dy - event.deltaY * unit, event.timeStamp);\n\t\t\twindow.clearTimeout(wheelTimerRef.current);\n\t\t\t// Close as soon as the pull is far enough; momentum would only lag it.\n\t\t\tif (Math.abs(drag.dy) > DISMISS_DISTANCE) {\n\t\t\t\trequestClose(velocityOf(drag.samples));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst pulled = drag;\n\t\t\twheelTimerRef.current = window.setTimeout(() => {\n\t\t\t\tif (dragRef.current === pulled) releaseDrag(pulled);\n\t\t\t}, WHEEL_IDLE);\n\t\t};\n\t\tconst onScrollEnd = () => settleScroll();\n\t\tviewer.addEventListener(\"wheel\", onWheel, { passive: false });\n\t\tconst track = trackRef.current;\n\t\ttrack?.addEventListener(\"scrollend\", onScrollEnd);\n\t\treturn () => {\n\t\t\tviewer.removeEventListener(\"wheel\", onWheel);\n\t\t\ttrack?.removeEventListener(\"scrollend\", onScrollEnd);\n\t\t};\n\t}, [viewer, beginDrag, updateDrag, releaseDrag, requestClose, settleScroll]);\n\n\tconst handleTrackScroll = () => {\n\t\tconst phase = phaseRef.current;\n\t\tif (scrollAnimRef.current || phase !== \"open\") return;\n\t\tconst next = nearestSlide();\n\t\tif (next !== currentRef.current) {\n\t\t\tcommitCurrent(next, next > currentRef.current ? 1 : -1);\n\t\t}\n\t\twindow.clearTimeout(scrollTimerRef.current);\n\t\tscrollTimerRef.current = window.setTimeout(settleScroll, SCROLL_IDLE);\n\t};\n\n\tconst handleTrackPointerDown = (event: PointerEvent<HTMLDivElement>) => {\n\t\tif (!event.isPrimary) return;\n\t\tif (event.pointerType === \"mouse\" && event.button !== 0) return;\n\t\tstopScroll();\n\t\tif (phaseRef.current !== \"open\") return;\n\t\tswipeRef.current =\n\t\t\tevent.pointerType === \"touch\" ? currentRef.current : null;\n\t\tgestureRef.current = {\n\t\t\tpointerId: event.pointerId,\n\t\t\tstartX: event.clientX,\n\t\t\tstartY: event.clientY,\n\t\t\tdecided: null,\n\t\t};\n\t};\n\n\tconst handleTrackPointerMove = (event: PointerEvent<HTMLDivElement>) => {\n\t\tconst gesture = gestureRef.current;\n\t\tif (!gesture || gesture.pointerId !== event.pointerId) return;\n\t\tconst dx = event.clientX - gesture.startX;\n\t\tconst dy = event.clientY - gesture.startY;\n\t\tif (gesture.decided === null) {\n\t\t\tif (Math.abs(dx) < AXIS_SLOP && Math.abs(dy) < AXIS_SLOP) return;\n\t\t\t// Axis lock: sideways belongs to native scrolling.\n\t\t\tif (Math.abs(dx) >= Math.abs(dy) || phaseRef.current !== \"open\") {\n\t\t\t\tgesture.decided = \"scroll\";\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst frame = framesRef.current[currentRef.current];\n\t\t\tif (!frame) return;\n\t\t\tgesture.decided = \"dismiss\";\n\t\t\tsuppressClickRef.current = true;\n\t\t\ttry {\n\t\t\t\tevent.currentTarget.setPointerCapture(event.pointerId);\n\t\t\t} catch {\n\t\t\t\t// The pointer may already be gone.\n\t\t\t}\n\t\t\twindow.getSelection()?.removeAllRanges();\n\t\t\tbeginDrag({\n\t\t\t\tbase: rectOf(frame),\n\t\t\t\toriginX: gesture.startX,\n\t\t\t\toriginY: gesture.startY,\n\t\t\t\tdx: 0,\n\t\t\t\tdy: 0,\n\t\t\t\twheel: false,\n\t\t\t\tpast: false,\n\t\t\t\tsamples: [{ t: event.timeStamp, y: 0 }],\n\t\t\t});\n\t\t}\n\t\tconst drag = dragRef.current;\n\t\tif (gesture.decided !== \"dismiss\" || !drag) return;\n\t\tupdateDrag(drag, dx, dy, event.timeStamp);\n\t};\n\n\tconst handleTrackPointerUp = (event: PointerEvent<HTMLDivElement>) => {\n\t\tconst gesture = gestureRef.current;\n\t\tif (!gesture || gesture.pointerId !== event.pointerId) return;\n\t\tgestureRef.current = null;\n\t\tconst drag = dragRef.current;\n\t\tif (gesture.decided !== \"dismiss\" || !drag) return;\n\t\tif (event.type === \"pointercancel\") {\n\t\t\tspringBack();\n\t\t\treturn;\n\t\t}\n\t\treleaseDrag(drag);\n\t};\n\n\tconst handleClick = (event: MouseEvent<HTMLDivElement>) => {\n\t\tif (suppressClickRef.current) {\n\t\t\tsuppressClickRef.current = false;\n\t\t\treturn;\n\t\t}\n\t\tconst phase = phaseRef.current;\n\t\tif (phase !== \"open\") return;\n\t\tconst target = event.target as HTMLElement;\n\t\t// A neighbour comes to the centre; the current photo stays put.\n\t\tconst photo = target.closest<HTMLElement>(\"[data-slide]\");\n\t\tif (photo) {\n\t\t\tconst slide = Number(photo.dataset.slide);\n\t\t\tif (slide !== currentRef.current) goTo(slide);\n\t\t\treturn;\n\t\t}\n\t\tif (target.closest(\"button, [data-slot='morph-lightbox-caption']\")) return;\n\t\t// Anywhere else is the backdrop.\n\t\trequestClose();\n\t};\n\n\tconst handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n\t\tconst phase = phaseRef.current;\n\t\tif (event.key === \"Escape\") {\n\t\t\tevent.preventDefault();\n\t\t\tevent.stopPropagation();\n\t\t\trequestClose();\n\t\t\treturn;\n\t\t}\n\t\tif (event.key === \"Tab\") {\n\t\t\t// Keep focus inside the dialog.\n\t\t\tconst root = event.currentTarget;\n\t\t\tconst focusable = Array.from(\n\t\t\t\troot.querySelectorAll<HTMLElement>(\n\t\t\t\t\t\"button:not([disabled]), [href], [tabindex]:not([tabindex='-1'])\",\n\t\t\t\t),\n\t\t\t).filter((element) => element.tabIndex >= 0);\n\t\t\tconst first = focusable[0];\n\t\t\tconst last = focusable[focusable.length - 1];\n\t\t\tif (!first || !last) {\n\t\t\t\tevent.preventDefault();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst active = document.activeElement;\n\t\t\tif (event.shiftKey && (active === first || active === root)) {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tlast.focus();\n\t\t\t} else if (!event.shiftKey && active === last) {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tfirst.focus();\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (phase !== \"open\") return;\n\t\tconst forward = rtlRef.current ? \"ArrowLeft\" : \"ArrowRight\";\n\t\tconst back = rtlRef.current ? \"ArrowRight\" : \"ArrowLeft\";\n\t\tconst targets: Record<string, number> = {\n\t\t\t[forward]: currentRef.current + 1,\n\t\t\t[back]: currentRef.current - 1,\n\t\t\tHome: 0,\n\t\t\tEnd: count - 1,\n\t\t};\n\t\tconst target = targets[event.key];\n\t\tif (target === undefined) return;\n\t\tevent.preventDefault();\n\t\tgoTo(target);\n\t};\n\n\tconst register = useCallback((slide: number, node: HTMLElement | null) => {\n\t\tif (node) triggersRef.current.set(slide, node);\n\t\telse triggersRef.current.delete(slide);\n\t}, []);\n\n\tconst openAt = useCallback(\n\t\t(slide: number) => {\n\t\t\tif (phaseRef.current !== \"closed\") return;\n\t\t\tconst { items, indexProp, onIndexChange } = latest.current;\n\t\t\tif (!items[slide]) return;\n\t\t\tuserOpenRef.current = true;\n\t\t\tcurrentRef.current = slide;\n\t\t\tsetCurrent(slide);\n\t\t\tsetDirection(1);\n\t\t\tif (indexProp === undefined) setUncontrolledIndex(slide);\n\t\t\tif (slide !== indexProp) onIndexChange?.(slide);\n\t\t\tsetOpenState(true);\n\t\t},\n\t\t[setOpenState],\n\t);\n\n\tconst preload = useCallback((slide: number) => {\n\t\tconst item = latest.current.items[slide];\n\t\tif (!item) return;\n\t\tconst image = new Image();\n\t\timage.decoding = \"async\";\n\t\timage.src = item.src;\n\t}, []);\n\n\t// The thumbnail of the photo in the viewer hides, so it's never seen twice.\n\tconst hiddenIndex = rendered ? shownIndex : null;\n\tconst contextValue = useMemo(\n\t\t() => ({ items, hiddenIndex, register, openAt, preload }),\n\t\t[items, hiddenIndex, register, openAt, preload],\n\t);\n\n\t// Opens that come through props jump straight to the slide. This render\n\t// still shows the old slide, so read the target from props.\n\tif (!rendered) {\n\t\tcurrentRef.current =\n\t\t\topen && !userOpenRef.current ? clamp(index, 0, lastIndex) : shownIndex;\n\t}\n\n\tconst multiple = count > 1;\n\tconst hasCaptions = items.some((item) => item.caption != null);\n\tconst currentItem = items[shownIndex];\n\tfor (let near = shownIndex - 1; near <= shownIndex + 1; near++) {\n\t\tif (near >= 0 && near < count) seenRef.current.add(near);\n\t}\n\t/** A slide's width: the photo's fitted width, at most the max slide width. */\n\tconst slideWidth = (item: MorphLightboxItem) =>\n\t\t`min(var(--morph-lightbox-max), calc(100cqh * ${item.width / item.height}))`;\n\tconst firstItem = items[0];\n\tconst lastItem = items[lastIndex];\n\n\tconst viewerNode =\n\t\trendered && mounted && currentItem && firstItem && lastItem ? (\n\t\t\t<div\n\t\t\t\tref={setViewer}\n\t\t\t\trole=\"dialog\"\n\t\t\t\taria-modal=\"true\"\n\t\t\t\taria-label=\"Image viewer\"\n\t\t\t\taria-describedby={\n\t\t\t\t\tcurrentItem.caption != null\n\t\t\t\t\t\t? `${slideId(shownIndex)} ${captionId(shownIndex)}`\n\t\t\t\t\t\t: slideId(shownIndex)\n\t\t\t\t}\n\t\t\t\ttabIndex={-1}\n\t\t\t\tdata-slot=\"morph-lightbox\"\n\t\t\t\tonKeyDown={handleKeyDown}\n\t\t\t\tonClick={handleClick}\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"fixed inset-0 z-50 select-none overflow-hidden overscroll-contain text-foreground outline-none data-[phase=closing]:pointer-events-none\",\n\t\t\t\t\tclassName,\n\t\t\t\t)}\n\t\t\t>\n\t\t\t\t<motion.div style={{ opacity: fade }} className=\"absolute inset-0\">\n\t\t\t\t\t<motion.div\n\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\tdata-slot=\"morph-lightbox-scrim\"\n\t\t\t\t\t\tstyle={{ opacity: scrim }}\n\t\t\t\t\t\tclassName=\"absolute inset-0 bg-background/96 backdrop-blur-[8px]\"\n\t\t\t\t\t/>\n\t\t\t\t\t<div className=\"relative flex h-full flex-col\">\n\t\t\t\t\t\t<motion.div\n\t\t\t\t\t\t\tstyle={{ opacity: chrome }}\n\t\t\t\t\t\t\tclassName=\"flex h-12 shrink-0 items-center justify-between gap-2 ps-4 pe-1 sm:h-14 sm:ps-5 sm:pe-2\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{multiple ? (\n\t\t\t\t\t\t\t\t<Counter\n\t\t\t\t\t\t\t\t\tvalue={shownIndex + 1}\n\t\t\t\t\t\t\t\t\ttotal={count}\n\t\t\t\t\t\t\t\t\tdirection={direction}\n\t\t\t\t\t\t\t\t\treduced={reducedMotion}\n\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t) : (\n\t\t\t\t\t\t\t\t<span />\n\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t<button\n\t\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\t\taria-label=\"Close\"\n\t\t\t\t\t\t\t\tdata-slot=\"morph-lightbox-close\"\n\t\t\t\t\t\t\t\tonClick={() => requestClose()}\n\t\t\t\t\t\t\t\tclassName=\"group/close grid size-11 shrink-0 cursor-pointer place-items-center rounded-full outline-none\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t<span className=\"grid size-8 place-items-center rounded-full bg-foreground/[0.06] text-foreground/80 transition-colors duration-150 group-hover/close:bg-foreground/10 group-hover/close:text-foreground group-focus-visible/close:ring-2 group-focus-visible/close:ring-foreground group-focus-visible/close:ring-offset-2 group-focus-visible/close:ring-offset-background\">\n\t\t\t\t\t\t\t\t\t<svg\n\t\t\t\t\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\t\t\t\t\tviewBox=\"0 0 16 16\"\n\t\t\t\t\t\t\t\t\t\tfill=\"none\"\n\t\t\t\t\t\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\t\t\t\t\t\tstrokeWidth=\"1.6\"\n\t\t\t\t\t\t\t\t\t\tstrokeLinecap=\"round\"\n\t\t\t\t\t\t\t\t\t\tclassName=\"size-3.5\"\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t<path d=\"M3.5 3.5l9 9M12.5 3.5l-9 9\" />\n\t\t\t\t\t\t\t\t\t</svg>\n\t\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t</button>\n\t\t\t\t\t\t</motion.div>\n\n\t\t\t\t\t\t<motion.div\n\t\t\t\t\t\t\tref={trackRef}\n\t\t\t\t\t\t\ttabIndex={-1}\n\t\t\t\t\t\t\tdata-slot=\"morph-lightbox-track\"\n\t\t\t\t\t\t\tstyle={{ opacity: chrome, touchAction: \"pan-x pinch-zoom\" }}\n\t\t\t\t\t\t\tonScroll={handleTrackScroll}\n\t\t\t\t\t\t\tonPointerDown={handleTrackPointerDown}\n\t\t\t\t\t\t\tonPointerMove={handleTrackPointerMove}\n\t\t\t\t\t\t\tonPointerUp={handleTrackPointerUp}\n\t\t\t\t\t\t\tonPointerCancel={handleTrackPointerUp}\n\t\t\t\t\t\t\tclassName=\"min-h-0 flex-1 snap-x snap-mandatory overflow-x-auto overflow-y-hidden overscroll-x-contain py-2 outline-none [container-type:size] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\tclassName=\"flex h-full w-max items-center gap-2 [--morph-lightbox-max:calc(100cqw-16px)] sm:gap-3 sm:[--morph-lightbox-max:80cqw]\"\n\t\t\t\t\t\t\t\tstyle={{\n\t\t\t\t\t\t\t\t\tpaddingInlineStart: `calc(50cqw - ${slideWidth(firstItem)} / 2)`,\n\t\t\t\t\t\t\t\t\tpaddingInlineEnd: `calc(50cqw - ${slideWidth(lastItem)} / 2)`,\n\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{items.map((item, slide) => {\n\t\t\t\t\t\t\t\t\tconst isCurrent = slide === shownIndex;\n\t\t\t\t\t\t\t\t\tconst near = seenRef.current.has(slide);\n\t\t\t\t\t\t\t\t\treturn (\n\t\t\t\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\t\t\t\tkey={slide}\n\t\t\t\t\t\t\t\t\t\t\tref={(node) => {\n\t\t\t\t\t\t\t\t\t\t\t\tslidesRef.current[slide] = node;\n\t\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t\t\taria-hidden={!isCurrent || undefined}\n\t\t\t\t\t\t\t\t\t\t\tclassName=\"flex h-full shrink-0 snap-center snap-always items-center\"\n\t\t\t\t\t\t\t\t\t\t\tstyle={{ width: slideWidth(item) }}\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\t\t\t\t\tref={(node) => {\n\t\t\t\t\t\t\t\t\t\t\t\t\tframesRef.current[slide] = node;\n\t\t\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t\t\t\tdata-slide={slide}\n\t\t\t\t\t\t\t\t\t\t\t\tdata-slot=\"morph-lightbox-slide\"\n\t\t\t\t\t\t\t\t\t\t\t\tdata-current={isCurrent}\n\t\t\t\t\t\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\"relative w-full overflow-hidden rounded-[6px] bg-center bg-cover bg-muted transition-opacity duration-200 ease-out data-[current=false]:cursor-pointer data-[current=false]:opacity-40\",\n\t\t\t\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t\t\t\t\tstyle={{\n\t\t\t\t\t\t\t\t\t\t\t\t\taspectRatio: `${item.width} / ${item.height}`,\n\t\t\t\t\t\t\t\t\t\t\t\t\tbackgroundImage: near\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t? `url(\"${item.thumbnailSrc ?? item.src}\")`\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t: undefined,\n\t\t\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t\t\t{/* biome-ignore lint/performance/noImgElement: plain images keep the component framework-agnostic; sizes come from the items */}\n\t\t\t\t\t\t\t\t\t\t\t\t<img\n\t\t\t\t\t\t\t\t\t\t\t\t\tid={slideId(slide)}\n\t\t\t\t\t\t\t\t\t\t\t\t\tsrc={near ? item.src : undefined}\n\t\t\t\t\t\t\t\t\t\t\t\t\talt={item.alt}\n\t\t\t\t\t\t\t\t\t\t\t\t\twidth={item.width}\n\t\t\t\t\t\t\t\t\t\t\t\t\theight={item.height}\n\t\t\t\t\t\t\t\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t\t\t\t\t\t\t\t\tdraggable={false}\n\t\t\t\t\t\t\t\t\t\t\t\t\tclassName=\"size-full object-cover\"\n\t\t\t\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t})}\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t</motion.div>\n\n\t\t\t\t\t\t{hasCaptions ? (\n\t\t\t\t\t\t\t<motion.div\n\t\t\t\t\t\t\t\tstyle={{ opacity: chrome }}\n\t\t\t\t\t\t\t\tclassName=\"grid min-h-9 shrink-0 place-items-center px-6 pt-1 text-center text-muted-foreground text-sm\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t<AnimatePresence initial={false}>\n\t\t\t\t\t\t\t\t\t{currentItem.caption != null ? (\n\t\t\t\t\t\t\t\t\t\t<motion.p\n\t\t\t\t\t\t\t\t\t\t\tkey={shownIndex}\n\t\t\t\t\t\t\t\t\t\t\tid={captionId(shownIndex)}\n\t\t\t\t\t\t\t\t\t\t\tdata-slot=\"morph-lightbox-caption\"\n\t\t\t\t\t\t\t\t\t\t\tinitial={{ opacity: 0 }}\n\t\t\t\t\t\t\t\t\t\t\tanimate={{ opacity: 1 }}\n\t\t\t\t\t\t\t\t\t\t\texit={{ opacity: 0 }}\n\t\t\t\t\t\t\t\t\t\t\ttransition={{ duration: 0.18, ease: \"easeOut\" }}\n\t\t\t\t\t\t\t\t\t\t\tclassName=\"col-start-1 row-start-1 max-w-prose text-balance\"\n\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t\t{currentItem.caption}\n\t\t\t\t\t\t\t\t\t\t</motion.p>\n\t\t\t\t\t\t\t\t\t) : null}\n\t\t\t\t\t\t\t\t</AnimatePresence>\n\t\t\t\t\t\t\t</motion.div>\n\t\t\t\t\t\t) : null}\n\n\t\t\t\t\t\t{multiple && filmstrip ? (\n\t\t\t\t\t\t\t<motion.div\n\t\t\t\t\t\t\t\tstyle={{ opacity: chrome }}\n\t\t\t\t\t\t\t\tclassName=\"shrink-0 pt-1 pb-[max(env(safe-area-inset-bottom),6px)]\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\t\tref={filmstripRef}\n\t\t\t\t\t\t\t\t\trole=\"group\"\n\t\t\t\t\t\t\t\t\taria-label=\"Thumbnails\"\n\t\t\t\t\t\t\t\t\tclassName=\"overflow-x-auto overscroll-x-contain [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\"\n\t\t\t\t\t\t\t\t\tstyle={{ touchAction: \"pan-x pinch-zoom\" }}\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<div\n\t\t\t\t\t\t\t\t\t\tref={filmRowRef}\n\t\t\t\t\t\t\t\t\t\tclassName=\"relative mx-auto flex w-max px-3\"\n\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t{items.map((item, slide) => (\n\t\t\t\t\t\t\t\t\t\t\t<button\n\t\t\t\t\t\t\t\t\t\t\t\tkey={slide}\n\t\t\t\t\t\t\t\t\t\t\t\tref={(node) => {\n\t\t\t\t\t\t\t\t\t\t\t\t\tthumbsRef.current[slide] = node;\n\t\t\t\t\t\t\t\t\t\t\t\t}}\n\t\t\t\t\t\t\t\t\t\t\t\ttype=\"button\"\n\t\t\t\t\t\t\t\t\t\t\t\ttabIndex={slide === shownIndex ? 0 : -1}\n\t\t\t\t\t\t\t\t\t\t\t\taria-label={`Show image ${slide + 1}: ${item.alt}`}\n\t\t\t\t\t\t\t\t\t\t\t\taria-current={slide === shownIndex || undefined}\n\t\t\t\t\t\t\t\t\t\t\t\tdata-slot=\"morph-lightbox-thumbnail\"\n\t\t\t\t\t\t\t\t\t\t\t\tonClick={() => goTo(slide)}\n\t\t\t\t\t\t\t\t\t\t\t\tclassName=\"group/thumb grid h-12 pointer-coarse:w-11 w-10 shrink-0 cursor-pointer place-items-center outline-none\"\n\t\t\t\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t\t\t\t<span className=\"block size-9 overflow-hidden rounded-[5px] bg-muted opacity-50 transition-opacity duration-200 group-hover/thumb:opacity-80 group-focus-visible/thumb:ring-2 group-focus-visible/thumb:ring-foreground group-focus-visible/thumb:ring-offset-2 group-focus-visible/thumb:ring-offset-background group-aria-[current=true]/thumb:opacity-100\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t{/* biome-ignore lint/performance/noImgElement: plain images keep the component framework-agnostic; sizes come from the items */}\n\t\t\t\t\t\t\t\t\t\t\t\t\t<img\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tsrc={item.thumbnailSrc ?? item.src}\n\t\t\t\t\t\t\t\t\t\t\t\t\t\talt=\"\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\twidth={item.width}\n\t\t\t\t\t\t\t\t\t\t\t\t\t\theight={item.height}\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tdraggable={false}\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tclassName=\"size-full object-cover\"\n\t\t\t\t\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t\t\t\t\t\t</button>\n\t\t\t\t\t\t\t\t\t\t))}\n\t\t\t\t\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\t\t\t\t\t\tstyle={{ x: outlineX }}\n\t\t\t\t\t\t\t\t\t\t\tclassName=\"pointer-events-none absolute top-1.5 left-0 size-9 rounded-[6px] outline-[1.5px] outline-foreground outline-solid outline-offset-[1.5px]\"\n\t\t\t\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t</motion.div>\n\t\t\t\t\t\t) : null}\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<div\n\t\t\t\t\t\tref={flyerRef}\n\t\t\t\t\t\taria-hidden\n\t\t\t\t\t\tdata-slot=\"morph-lightbox-flyer\"\n\t\t\t\t\t\tclassName=\"pointer-events-none invisible absolute top-0 left-0 overflow-hidden bg-center bg-cover bg-muted\"\n\t\t\t\t\t\tstyle={{\n\t\t\t\t\t\t\tbackgroundImage: `url(\"${currentItem.thumbnailSrc ?? currentItem.src}\")`,\n\t\t\t\t\t\t}}\n\t\t\t\t\t>\n\t\t\t\t\t\t{/* biome-ignore lint/performance/noImgElement: plain images keep the component framework-agnostic; sizes come from the items */}\n\t\t\t\t\t\t<img\n\t\t\t\t\t\t\tsrc={currentItem.src}\n\t\t\t\t\t\t\talt=\"\"\n\t\t\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\t\t\tdraggable={false}\n\t\t\t\t\t\t\tclassName=\"size-full object-cover\"\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t</motion.div>\n\t\t\t\t<p aria-live=\"polite\" aria-atomic className=\"sr-only\">\n\t\t\t\t\t{announcement}\n\t\t\t\t</p>\n\t\t\t</div>\n\t\t) : null;\n\n\treturn (\n\t\t<MorphLightboxContext.Provider value={contextValue}>\n\t\t\t{children}\n\t\t\t{viewerNode ? createPortal(viewerNode, document.body) : null}\n\t\t</MorphLightboxContext.Provider>\n\t);\n}\n\nconst rollVariants = {\n\tenter: (step: number) => ({ y: step > 0 ? \"100%\" : \"-100%\" }),\n\tcenter: { y: \"0%\" },\n\texit: (step: number) => ({ y: step > 0 ? \"-100%\" : \"100%\" }),\n};\nconst swapVariants = {\n\tenter: { opacity: 0 },\n\tcenter: { opacity: 1 },\n\texit: { opacity: 0 },\n};\n\n/** \"3 / 8\". The current number rolls one digit at a time, the way you went. */\nfunction Counter({\n\tvalue,\n\ttotal,\n\tdirection,\n\treduced,\n}: {\n\tvalue: number;\n\ttotal: number;\n\tdirection: number;\n\treduced: boolean;\n}) {\n\tconst digits = String(value).split(\"\");\n\treturn (\n\t\t// Numbers read left to right in RTL text too.\n\t\t<span\n\t\t\taria-hidden\n\t\t\tdir=\"ltr\"\n\t\t\tdata-slot=\"morph-lightbox-counter\"\n\t\t\tclassName=\"inline-flex items-center font-medium text-foreground/80 text-sm tabular-nums\"\n\t\t>\n\t\t\t{digits.map((digit, order) => (\n\t\t\t\t<span\n\t\t\t\t\t// Places are keyed from the right, so units stay units.\n\t\t\t\t\tkey={`place-${digits.length - order}`}\n\t\t\t\t\tclassName=\"relative inline-block h-[1lh] overflow-hidden\"\n\t\t\t\t>\n\t\t\t\t\t<AnimatePresence initial={false} custom={direction} mode=\"popLayout\">\n\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\tkey={digit}\n\t\t\t\t\t\t\tcustom={direction}\n\t\t\t\t\t\t\tvariants={reduced ? swapVariants : rollVariants}\n\t\t\t\t\t\t\tinitial=\"enter\"\n\t\t\t\t\t\t\tanimate=\"center\"\n\t\t\t\t\t\t\texit=\"exit\"\n\t\t\t\t\t\t\ttransition={\n\t\t\t\t\t\t\t\treduced\n\t\t\t\t\t\t\t\t\t? { duration: REDUCED_FADE }\n\t\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\t\tduration: ROLL_DURATION,\n\t\t\t\t\t\t\t\t\t\t\tease: ROLL_EASE,\n\t\t\t\t\t\t\t\t\t\t\tdelay: order * ROLL_STAGGER,\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tclassName=\"block\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{digit}\n\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t</AnimatePresence>\n\t\t\t\t</span>\n\t\t\t))}\n\t\t\t<span className=\"px-1 text-muted-foreground\">/</span>\n\t\t\t<span className=\"text-muted-foreground\">{total}</span>\n\t\t</span>\n\t);\n}\n\nexport interface MorphLightboxTriggerProps extends ComponentProps<\"button\"> {\n\t/** Which item this thumbnail opens. */\n\tindex: number;\n\t/** Custom thumbnail content. Defaults to the item's thumbnail, cropped to fill. */\n\tchildren?: ReactNode;\n}\n\nfunction MorphLightboxTrigger({\n\tindex,\n\tclassName,\n\tchildren,\n\tonClick,\n\tonPointerEnter,\n\tonFocus,\n\t...props\n}: MorphLightboxTriggerProps) {\n\tconst { items, hiddenIndex, register, openAt, preload } = useMorphLightbox();\n\tconst item = items[index];\n\tif (!item) return null;\n\n\treturn (\n\t\t<button\n\t\t\tref={(node) => register(index, node)}\n\t\t\ttype=\"button\"\n\t\t\taria-label={`Open image: ${item.alt}`}\n\t\t\tdata-slot=\"morph-lightbox-trigger\"\n\t\t\tdata-hidden={hiddenIndex === index || undefined}\n\t\t\tonClick={(event) => {\n\t\t\t\tonClick?.(event);\n\t\t\t\tif (!event.defaultPrevented) openAt(index);\n\t\t\t}}\n\t\t\tonPointerEnter={(event) => {\n\t\t\t\tonPointerEnter?.(event);\n\t\t\t\tpreload(index);\n\t\t\t}}\n\t\t\tonFocus={(event) => {\n\t\t\t\tonFocus?.(event);\n\t\t\t\tpreload(index);\n\t\t\t}}\n\t\t\tclassName={cn(\n\t\t\t\t\"relative block aspect-square w-full cursor-zoom-in overflow-hidden rounded-xl bg-muted outline-none focus-visible:ring-2 focus-visible:ring-foreground focus-visible:ring-offset-2 focus-visible:ring-offset-background data-hidden:invisible\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t>\n\t\t\t{children ?? (\n\t\t\t\t// biome-ignore lint/performance/noImgElement: plain images keep the component framework-agnostic; sizes come from the items\n\t\t\t\t<img\n\t\t\t\t\tsrc={item.thumbnailSrc ?? item.src}\n\t\t\t\t\talt=\"\"\n\t\t\t\t\twidth={item.width}\n\t\t\t\t\theight={item.height}\n\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\tdecoding=\"async\"\n\t\t\t\t\tdraggable={false}\n\t\t\t\t\tclassName=\"size-full object-cover\"\n\t\t\t\t/>\n\t\t\t)}\n\t\t</button>\n\t);\n}\n\nexport { MorphLightbox, MorphLightboxTrigger };\n\nexport default MorphLightbox;\n",
      "type": "registry:component",
      "target": "@components/morph-lightbox.tsx"
    }
  ],
  "meta": {
    "createdAt": "2026-11-12",
    "image": "https://cdn.tentui.com/images/components/morph-lightbox?v=1",
    "video": "https://cdn.tentui.com/videos/components/morph-lightbox?v=1"
  },
  "docs": "https://tentui.com/components/morph-lightbox",
  "categories": [
    "overlays"
  ],
  "type": "registry:component"
}