{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"image-comparison-slider","type":"registry:component","title":"Image Comparison Slider","description":"A before/after reveal with a glass grip on a spring-driven divider: press anywhere to glide it there, drag without a jump, use the arrow keys, or turn on hover mode. Corner labels fade as the divider reaches them, it splits left/right or top/bottom, and with no images it draws its own night-to-day lake scene. No dependencies.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/image-comparison-slider.tsx","content":"\"use client\";\n\n/*\n * ImageComparisonSlider: a before/after reveal with a spring-driven divider.\n *\n * Two images share one frame; the \"before\" layer is clipped by a CSS variable\n * (--icp, 0 to 100) that a single rAF loop writes straight to the DOM, so a drag\n * never re-renders React. Press anywhere to jump the divider there (it glides on\n * a soft spring, then follows the pointer on a stiff one), grab the glass grip to\n * drag without a jump, use the arrow keys, or switch on `hover` so it follows a\n * mouse without a press. The corner labels fade out as the divider reaches them.\n * Without images it draws a night/day mountain lake in SVG.\n */\n\nimport { useEffect, useId, useRef, type CSSProperties, type KeyboardEvent, type PointerEvent as ReactPointerEvent } from \"react\";\n\nexport type ImageComparisonSliderProps = {\n  /** Image URL for the \"before\" side. Omit for the built-in night scene. */\n  before?: string;\n  /** Image URL for the \"after\" side. Omit for the built-in day scene. */\n  after?: string;\n  /** Starting divider position, 0 to 100. */\n  initial?: number;\n  labelBefore?: string;\n  labelAfter?: string;\n  /** \"horizontal\" splits left/right, \"vertical\" splits top/bottom. */\n  orientation?: \"horizontal\" | \"vertical\";\n  /** Follow a mouse without pressing. Touch and pen still drag. */\n  hover?: boolean;\n  /** Called with the divider position (0 to 100) whenever the visitor moves it. */\n  onChange?: (position: number) => void;\n  /** Extra classes on the root. The defaults are h-64 w-full max-w-xl. */\n  className?: string;\n};\n\n/** Accepts http(s) and relative URLs only; strips control whitespace, rejects backslashes. */\nfunction safeSrc(value: unknown): string | null {\n  if (typeof value !== \"string\") return null;\n  const v = value.replace(/[\\t\\r\\n]/g, \"\").trim();\n  if (!v || v.includes(\"\\\\\")) return null;\n  const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(v);\n  if (scheme) return scheme[1]?.toLowerCase() === \"http\" || scheme[1]?.toLowerCase() === \"https\" ? v : null;\n  return v;\n}\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\n\n/* ---- Built-in scene: the same lake at night and at dawn ------------------ */\n\nconst SCENE_W = 800;\nconst SCENE_H = 500;\nconst HORIZON = 318;\n\n/** A smooth skyline through deterministic points. */\nfunction ridge(base: number, amp: number, seed: number, step: number): string {\n  const pts: Array<[number, number]> = [];\n  for (let x = -step; x <= SCENE_W + step * 2; x += step) {\n    const h =\n      Math.sin(x * 0.011 + seed) * 0.55 +\n      Math.sin(x * 0.027 + seed * 2.3) * 0.3 +\n      Math.sin(x * 0.061 + seed * 4.1) * 0.15;\n    pts.push([Math.round(x * 10) / 10, Math.round((base - (h * 0.5 + 0.5) * amp) * 10) / 10]);\n  }\n  const first = pts[0];\n  if (!first) return \"\";\n  let d = \"M\" + first[0] + \" \" + first[1];\n  for (let i = 1; i < pts.length - 1; i++) {\n    const p = pts[i];\n    const n = pts[i + 1];\n    if (!p || !n) continue;\n    d += \"Q\" + p[0] + \" \" + p[1] + \" \" + Math.round(((p[0] + n[0]) / 2) * 10) / 10 + \" \" + Math.round(((p[1] + n[1]) / 2) * 10) / 10;\n  }\n  return d + \"L\" + (SCENE_W + step * 2) + \" \" + HORIZON + \"L\" + -step + \" \" + HORIZON + \"Z\";\n}\n\nconst RIDGE_FAR = ridge(268, 110, 1.2, 36);\nconst RIDGE_MID = ridge(292, 92, 3.4, 30);\nconst RIDGE_NEAR = ridge(HORIZON + 4, 70, 5.9, 26);\n\nfunction pine(x: number, base: number, h: number): string {\n  const w = h * 0.34;\n  const t = (k: number) => Math.round(k * 10) / 10;\n  return (\n    \"M\" + t(x) + \" \" + t(base - h) +\n    \"L\" + t(x + w * 0.5) + \" \" + t(base - h * 0.62) + \"L\" + t(x + w * 0.28) + \" \" + t(base - h * 0.62) +\n    \"L\" + t(x + w * 0.74) + \" \" + t(base - h * 0.3) + \"L\" + t(x + w * 0.4) + \" \" + t(base - h * 0.3) +\n    \"L\" + t(x + w) + \" \" + t(base) + \"L\" + t(x - w) + \" \" + t(base) +\n    \"L\" + t(x - w * 0.4) + \" \" + t(base - h * 0.3) + \"L\" + t(x - w * 0.74) + \" \" + t(base - h * 0.3) +\n    \"L\" + t(x - w * 0.28) + \" \" + t(base - h * 0.62) + \"L\" + t(x - w * 0.5) + \" \" + t(base - h * 0.62) + \"Z\"\n  );\n}\n\nconst PINES: Array<[number, number]> = [\n  [34, 62], [70, 84], [108, 58], [146, 72], [660, 70], [700, 96], [742, 64], [782, 86], [820, 60], [622, 52],\n];\nconst TREE_PATH = PINES.map(([x, h]) => pine(x, HORIZON + 14, h)).join(\"\");\n\n/** Deterministic stars (a tiny LCG, so server and client agree). */\nconst STARS: Array<[number, number, number]> = (() => {\n  const out: Array<[number, number, number]> = [];\n  let s = 7;\n  for (let i = 0; i < 70; i++) {\n    s = (s * 1103515245 + 12345) % 2147483648;\n    const x = (s % 800) + 0;\n    s = (s * 1103515245 + 12345) % 2147483648;\n    const y = (s % 215) + 6;\n    s = (s * 1103515245 + 12345) % 2147483648;\n    out.push([x, y, 0.5 + (s % 100) / 100]);\n  }\n  return out;\n})();\n\nfunction Scene({ mode, label }: { mode: \"night\" | \"day\"; label: string }) {\n  const id = useId().replace(/[^a-zA-Z0-9]/g, \"\") + mode;\n  const night = mode === \"night\";\n  const sky = night ? [\"#03060f\", \"#0a1330\", \"#1d2c5a\"] : [\"#3b66b3\", \"#e89a8c\", \"#ffe3b3\"];\n  const far = night ? \"#18224a\" : \"#8c86bd\";\n  const mid = night ? \"#101836\" : \"#5d6a9e\";\n  const near = night ? \"#090e24\" : \"#2c3b66\";\n  const wood = night ? \"#050917\" : \"#14203a\";\n  const orbX = night ? 548 : 520;\n  const orbY = night ? 128 : HORIZON - 30;\n  return (\n    <svg\n      role=\"img\"\n      aria-label={label}\n      viewBox={\"0 0 \" + SCENE_W + \" \" + SCENE_H}\n      preserveAspectRatio=\"xMidYMid slice\"\n      className=\"absolute inset-0 h-full w-full\"\n    >\n      <defs>\n        <linearGradient id={id + \"s\"} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n          <stop offset=\"0\" stopColor={sky[0]} />\n          <stop offset=\"0.55\" stopColor={sky[1]} />\n          <stop offset=\"1\" stopColor={sky[2]} />\n        </linearGradient>\n        <radialGradient id={id + \"o\"}>\n          <stop offset=\"0\" stopColor={night ? \"#dfe8ff\" : \"#fff1c9\"} stopOpacity={night ? 0.5 : 0.95} />\n          <stop offset=\"1\" stopColor={night ? \"#7f9bff\" : \"#ffb27a\"} stopOpacity=\"0\" />\n        </radialGradient>\n        <linearGradient id={id + \"w\"} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n          <stop offset=\"0\" stopColor={night ? \"#10193c\" : \"#f3b79b\"} />\n          <stop offset=\"1\" stopColor={night ? \"#04070f\" : \"#3a4a7c\"} />\n        </linearGradient>\n        <linearGradient id={id + \"m\"} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n          <stop offset=\"0\" stopColor={night ? \"#8ea6ff\" : \"#fff0d0\"} stopOpacity=\"0\" />\n          <stop offset=\"0.5\" stopColor={night ? \"#8ea6ff\" : \"#fff0d0\"} stopOpacity={night ? 0.22 : 0.5} />\n          <stop offset=\"1\" stopColor={night ? \"#8ea6ff\" : \"#fff0d0\"} stopOpacity=\"0\" />\n        </linearGradient>\n        <clipPath id={id + \"c\"}>\n          <rect x=\"0\" y={HORIZON + 12} width={SCENE_W} height={SCENE_H - HORIZON} />\n        </clipPath>\n      </defs>\n      <rect width={SCENE_W} height={SCENE_H} fill={\"url(#\" + id + \"s)\"} />\n      {night &&\n        STARS.map(([x, y, r], i) => (\n          <circle key={i} cx={x} cy={y} r={r} fill=\"#fff\" opacity={Math.round((0.35 + (i % 5) * 0.12) * 100) / 100} />\n        ))}\n      <circle cx={orbX} cy={orbY} r={night ? 120 : 190} fill={\"url(#\" + id + \"o)\"} />\n      <circle cx={orbX} cy={orbY} r={night ? 22 : 30} fill={night ? \"#eef2ff\" : \"#fff6dc\"} />\n      {night && <circle cx={orbX + 9} cy={orbY - 4} r=\"19\" fill=\"#0a1330\" opacity=\"0.18\" />}\n      <path d={RIDGE_FAR} fill={far} />\n      <path d={RIDGE_MID} fill={mid} />\n      <rect x=\"0\" y={HORIZON - 52} width={SCENE_W} height=\"70\" fill={\"url(#\" + id + \"m)\"} />\n      <path d={RIDGE_NEAR} fill={near} />\n      {/* the lake and its reflections */}\n      <rect x=\"0\" y={HORIZON + 12} width={SCENE_W} height={SCENE_H - HORIZON} fill={\"url(#\" + id + \"w)\"} />\n      <g clipPath={\"url(#\" + id + \"c)\"}>\n        <g transform={\"translate(0 \" + (HORIZON * 2 + 24) + \") scale(1 -1)\"} opacity={night ? 0.5 : 0.4}>\n          <path d={RIDGE_MID} fill={mid} />\n          <path d={RIDGE_NEAR} fill={near} />\n        </g>\n        {[0, 1, 2, 3, 4, 5, 6].map((i) => (\n          <rect\n            key={i}\n            x={orbX - 46 + i * 5}\n            y={HORIZON + 30 + i * 21}\n            width={92 - i * 10}\n            height={night ? 3 : 4}\n            rx=\"2\"\n            fill={night ? \"#dfe8ff\" : \"#fff1c9\"}\n            opacity={Math.round((0.7 - i * 0.09) * 100) / 100}\n          />\n        ))}\n      </g>\n      {/* the near bank: pines and a cabin */}\n      <path d={TREE_PATH} fill={wood} />\n      <rect x=\"0\" y={HORIZON + 12} width=\"190\" height=\"8\" fill={wood} />\n      <rect x={SCENE_W - 210} y={HORIZON + 12} width=\"210\" height=\"8\" fill={wood} />\n      <g>\n        <path d={\"M296 \" + (HORIZON + 14) + \"V\" + (HORIZON - 18) + \"L328 \" + (HORIZON - 46) + \"L360 \" + (HORIZON - 18) + \"V\" + (HORIZON + 14) + \"Z\"} fill={night ? \"#0c1430\" : \"#16223f\"} />\n        <rect x=\"322\" y={HORIZON - 16} width=\"12\" height=\"14\" rx=\"1.5\" fill={night ? \"#ffcf6b\" : \"#c9d3f2\"} />\n        {night && <circle cx=\"328\" cy={HORIZON - 9} r=\"17\" fill=\"#ffcf6b\" opacity=\"0.14\" />}\n        <rect x=\"344\" y={HORIZON - 58} width=\"7\" height=\"20\" fill={night ? \"#0c1430\" : \"#16223f\"} />\n        {!night && <ellipse cx=\"348\" cy={HORIZON - 70} rx=\"9\" ry=\"6\" fill=\"#fff\" opacity=\"0.35\" />}\n      </g>\n    </svg>\n  );\n}\n\n/* ---- Component ------------------------------------------------------------ */\n\nconst EDGE = 12; // label inset from the frame\nconst FADE = 34; // px over which a label fades as the divider reaches it\n\nexport function ImageComparisonSlider({\n  before,\n  after,\n  initial = 50,\n  labelBefore = \"Before\",\n  labelAfter = \"After\",\n  orientation = \"horizontal\",\n  hover = false,\n  onChange,\n  className,\n}: ImageComparisonSliderProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const gripRef = useRef<HTMLDivElement>(null);\n  const labelBeforeRef = useRef<HTMLSpanElement>(null);\n  const labelAfterRef = useRef<HTMLSpanElement>(null);\n  const start = clamp(Number.isFinite(initial) ? initial : 50, 0, 100);\n  const vertical = orientation === \"vertical\";\n\n  // Everything the loop and the handlers read lives in refs, synced in effects.\n  const sim = useRef({ cur: start, vel: 0, target: start, drag: false, offset: 0, wake: () => {}, userSet: (p: number) => void p });\n  const live = useRef({ vertical, hover, label: labelBefore + \"|\" + labelAfter });\n  const touch = useRef<{ id: number; x: number; y: number; started: boolean } | null>(null);\n  const changeRef = useRef(onChange);\n\n  useEffect(() => {\n    changeRef.current = onChange;\n    live.current = { vertical, hover, label: labelBefore + \"|\" + labelAfter };\n    sim.current.wake();\n  }, [onChange, vertical, hover, labelBefore, labelAfter]);\n\n  // A new `initial` repositions the divider; first run is a no-op.\n  useEffect(() => {\n    const s = sim.current;\n    s.target = start;\n    s.cur = start;\n    s.vel = 0;\n    s.wake();\n  }, [start]);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    const grip = gripRef.current;\n    if (!root || !grip) return;\n    const s = sim.current;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let raf = 0;\n    let last = -1;\n    let lastAria = -1;\n    const size = { w: 0, h: 0, lw: 0, lh: 0, aw: 0, ah: 0 };\n\n    const measure = () => {\n      size.w = root.offsetWidth;\n      size.h = root.offsetHeight;\n      size.lw = labelBeforeRef.current?.offsetWidth ?? 0;\n      size.lh = labelBeforeRef.current?.offsetHeight ?? 0;\n      size.aw = labelAfterRef.current?.offsetWidth ?? 0;\n      size.ah = labelAfterRef.current?.offsetHeight ?? 0;\n    };\n\n    const write = () => {\n      const c = clamp(s.cur, 0, 100);\n      root.style.setProperty(\"--icp\", c.toFixed(3));\n      const v = live.current.vertical;\n      const at = (c / 100) * (v ? size.h : size.w);\n      const span = v ? size.h : size.w;\n      const ob = v ? at - (EDGE + size.lh) : at - (EDGE + size.lw);\n      const oa = v ? span - EDGE - size.ah - at : span - EDGE - size.aw - at;\n      root.style.setProperty(\"--icb\", clamp(ob / FADE, 0, 1).toFixed(3));\n      root.style.setProperty(\"--ica\", clamp(oa / FADE, 0, 1).toFixed(3));\n      const r = Math.round(clamp(s.target, 0, 100));\n      if (r !== lastAria) {\n        lastAria = r;\n        const [lb, la] = live.current.label.split(\"|\");\n        grip.setAttribute(\"aria-valuenow\", String(r));\n        grip.setAttribute(\"aria-valuetext\", r + \"% \" + (lb ?? \"Before\") + \", \" + (100 - r) + \"% \" + (la ?? \"After\"));\n      }\n    };\n\n    const frame = (t: number) => {\n      raf = 0;\n      const dt = last < 0 ? 1 / 60 : Math.min(0.05, (t - last) / 1000);\n      last = t;\n      // Stiff while held (tracks the pointer), soft when it jumps or glides.\n      const k = s.drag ? 1100 : 240;\n      const c = s.drag ? 66 : 26;\n      const n = Math.max(1, Math.ceil(dt / (1 / 240)));\n      const h = dt / n;\n      for (let i = 0; i < n; i++) {\n        s.vel += (-k * (s.cur - s.target) - c * s.vel) * h;\n        s.cur += s.vel * h;\n      }\n      if (Math.abs(s.cur - s.target) < 0.012 && Math.abs(s.vel) < 0.05) {\n        s.cur = s.target;\n        s.vel = 0;\n        last = -1;\n        write();\n        return;\n      }\n      write();\n      raf = requestAnimationFrame(frame);\n    };\n\n    s.wake = () => {\n      measure();\n      if (motion.matches) {\n        s.cur = s.target;\n        s.vel = 0;\n        write();\n        return;\n      }\n      if (!raf) raf = requestAnimationFrame(frame);\n    };\n\n    s.userSet = (p: number) => {\n      const v = clamp(p, 0, 100);\n      s.target = v;\n      s.wake();\n      changeRef.current?.(Math.round(v * 10) / 10);\n    };\n\n    const ro = new ResizeObserver(() => {\n      measure();\n      write();\n    });\n    ro.observe(root);\n    if (labelBeforeRef.current) ro.observe(labelBeforeRef.current);\n    if (labelAfterRef.current) ro.observe(labelAfterRef.current);\n    measure();\n    write();\n    return () => {\n      ro.disconnect();\n      if (raf) cancelAnimationFrame(raf);\n      s.wake = () => {};\n    };\n  }, []);\n\n  const place = (e: ReactPointerEvent<HTMLDivElement>) => {\n    const root = rootRef.current;\n    if (!root) return;\n    const rect = root.getBoundingClientRect();\n    const span = live.current.vertical ? rect.height : rect.width;\n    if (span <= 0) return;\n    const at = live.current.vertical ? e.clientY - rect.top : e.clientX - rect.left;\n    sim.current.userSet((at / span) * 100 + sim.current.offset);\n  };\n\n  const beginDrag = (e: ReactPointerEvent<HTMLDivElement>, grabbed: boolean) => {\n    const root = rootRef.current;\n    if (!root) return;\n    const s = sim.current;\n    s.drag = true;\n    root.dataset.drag = \"true\";\n    try {\n      root.setPointerCapture(e.pointerId);\n    } catch {\n      /* the pointer is already gone */\n    }\n    gripRef.current?.focus({ preventScroll: true });\n    // Grabbing the grip keeps the offset so it never jumps under the finger.\n    const rect = root.getBoundingClientRect();\n    const span = live.current.vertical ? rect.height : rect.width;\n    const at = live.current.vertical ? e.clientY - rect.top : e.clientX - rect.left;\n    s.offset = grabbed && span > 0 ? s.cur - (at / span) * 100 : 0;\n    place(e);\n  };\n\n  const endDrag = () => {\n    const root = rootRef.current;\n    const s = sim.current;\n    s.drag = false;\n    s.offset = 0;\n    if (root) root.dataset.drag = \"false\";\n    s.wake();\n  };\n\n  const onPointerDown = (e: ReactPointerEvent<HTMLDivElement>) => {\n    if (e.pointerType === \"mouse\" && e.button !== 0) return;\n    const grabbed = !!gripRef.current && gripRef.current.contains(e.target as Node);\n    if (e.pointerType === \"mouse\") {\n      beginDrag(e, grabbed);\n    } else if (grabbed) {\n      touch.current = null;\n      beginDrag(e, true);\n    } else {\n      // Touch: wait to see whether this is a scroll, a tap or a drag.\n      touch.current = { id: e.pointerId, x: e.clientX, y: e.clientY, started: false };\n    }\n  };\n\n  const onPointerMove = (e: ReactPointerEvent<HTMLDivElement>) => {\n    const s = sim.current;\n    const tp = touch.current;\n    if (tp && tp.id === e.pointerId && !tp.started) {\n      const dx = Math.abs(e.clientX - tp.x);\n      const dy = Math.abs(e.clientY - tp.y);\n      if (Math.max(dx, dy) < 6) return;\n      if ((live.current.vertical ? dy >= dx : dx >= dy)) {\n        tp.started = true;\n        beginDrag(e, false);\n      } else {\n        touch.current = null;\n      }\n      return;\n    }\n    if (s.drag) {\n      if (e.pointerType === \"mouse\" && e.buttons === 0) {\n        endDrag();\n        return;\n      }\n      place(e);\n    } else if (live.current.hover && e.pointerType === \"mouse\") {\n      place(e);\n    }\n  };\n\n  const onPointerUp = (e: ReactPointerEvent<HTMLDivElement>) => {\n    const tp = touch.current;\n    if (tp && tp.id === e.pointerId && !tp.started && e.type === \"pointerup\") {\n      sim.current.offset = 0;\n      place(e);\n    }\n    touch.current = null;\n    if (sim.current.drag) endDrag();\n  };\n\n  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n    const s = sim.current;\n    const step = e.shiftKey ? 10 : 2;\n    const back = live.current.vertical ? [\"ArrowUp\", \"ArrowLeft\"] : [\"ArrowLeft\", \"ArrowDown\"];\n    const fwd = live.current.vertical ? [\"ArrowDown\", \"ArrowRight\"] : [\"ArrowRight\", \"ArrowUp\"];\n    let next: number | null = null;\n    if (back.includes(e.key)) next = s.target - step;\n    else if (fwd.includes(e.key)) next = s.target + step;\n    else if (e.key === \"PageUp\") next = s.target - 10;\n    else if (e.key === \"PageDown\") next = s.target + 10;\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = 100;\n    if (next === null) return;\n    e.preventDefault();\n    s.userSet(next);\n  };\n\n  const beforeSrc = safeSrc(before);\n  const afterSrc = safeSrc(after);\n  const root: CSSProperties & Record<string, string | number> = {\n    \"--icp\": start,\n    \"--icb\": 1,\n    \"--ica\": 1,\n    touchAction: vertical ? \"pan-x\" : \"pan-y\",\n    cursor: vertical ? \"ns-resize\" : \"ew-resize\",\n    WebkitTapHighlightColor: \"transparent\",\n  };\n  const clip = vertical ? \"inset(0 0 calc(100% - var(--icp) * 1%) 0)\" : \"inset(0 calc(100% - var(--icp) * 1%) 0 0)\";\n  const pill =\n    \"pointer-events-none absolute z-[3] rounded-full px-2.5 py-1 text-[11px] font-semibold uppercase leading-none tracking-[0.12em] text-white\";\n  const pillStyle: CSSProperties = {\n    background: \"rgba(8,10,14,0.5)\",\n    boxShadow: \"inset 0 0 0 1px rgba(255,255,255,0.16)\",\n    backdropFilter: \"blur(10px)\",\n    WebkitBackdropFilter: \"blur(10px)\",\n  };\n\n  return (\n    <div\n      ref={rootRef}\n      data-drag=\"false\"\n      className={\n        \"group/ic relative isolate h-64 w-full max-w-xl select-none overflow-hidden rounded-2xl\" + (className ? \" \" + className : \"\")\n      }\n      style={root}\n      onPointerDown={onPointerDown}\n      onPointerMove={onPointerMove}\n      onPointerUp={onPointerUp}\n      onPointerCancel={onPointerUp}\n    >\n      {/* after: the base layer */}\n      <div className=\"pointer-events-none absolute inset-0\">\n        {afterSrc ? (\n          // eslint-disable-next-line @next/next/no-img-element\n          <img src={afterSrc} alt={labelAfter} draggable={false} className=\"absolute inset-0 h-full w-full object-cover\" />\n        ) : (\n          <Scene mode=\"day\" label={labelAfter} />\n        )}\n      </div>\n      {/* before: revealed up to the divider */}\n      <div className=\"pointer-events-none absolute inset-0\" style={{ clipPath: clip, WebkitClipPath: clip }}>\n        {beforeSrc ? (\n          // eslint-disable-next-line @next/next/no-img-element\n          <img src={beforeSrc} alt={labelBefore} draggable={false} className=\"absolute inset-0 h-full w-full object-cover\" />\n        ) : (\n          <Scene mode=\"night\" label={labelBefore} />\n        )}\n      </div>\n\n      {/* a soft shadow the divider casts onto the after side, and the line itself */}\n      <div\n        aria-hidden\n        className=\"pointer-events-none absolute z-[2]\"\n        style={\n          vertical\n            ? { left: 0, right: 0, top: \"calc(var(--icp) * 1%)\", height: 0 }\n            : { top: 0, bottom: 0, left: \"calc(var(--icp) * 1%)\", width: 0 }\n        }\n      >\n        <div\n          className=\"absolute\"\n          style={\n            vertical\n              ? { left: 0, right: 0, top: 0, height: 36, background: \"linear-gradient(to bottom, rgba(0,0,0,0.22), transparent)\" }\n              : { top: 0, bottom: 0, left: 0, width: 36, background: \"linear-gradient(to right, rgba(0,0,0,0.22), transparent)\" }\n          }\n        />\n        <div\n          className=\"absolute\"\n          style={\n            vertical\n              ? {\n                  left: 0,\n                  right: 0,\n                  top: -1,\n                  height: 2,\n                  background: \"linear-gradient(to right, transparent, #fff 14%, #fff 86%, transparent)\",\n                  boxShadow: \"0 0 12px rgba(255,255,255,0.55)\",\n                }\n              : {\n                  top: 0,\n                  bottom: 0,\n                  left: -1,\n                  width: 2,\n                  background: \"linear-gradient(to bottom, transparent, #fff 14%, #fff 86%, transparent)\",\n                  boxShadow: \"0 0 12px rgba(255,255,255,0.55)\",\n                }\n          }\n        />\n      </div>\n\n      <span ref={labelBeforeRef} className={pill} style={{ ...pillStyle, left: EDGE, [vertical ? \"top\" : \"bottom\"]: EDGE, opacity: \"var(--icb)\" }}>\n        {labelBefore}\n      </span>\n      <span\n        ref={labelAfterRef}\n        className={pill}\n        style={{ ...pillStyle, [vertical ? \"left\" : \"right\"]: EDGE, bottom: EDGE, opacity: \"var(--ica)\" }}\n      >\n        {labelAfter}\n      </span>\n\n      {/* the grip: clamped so it never leaves the frame */}\n      <div\n        className=\"absolute z-[4]\"\n        style={\n          vertical\n            ? { left: \"50%\", top: \"clamp(24px, calc(var(--icp) * 1%), calc(100% - 24px))\", transform: \"translate(-50%, -50%)\" }\n            : { top: \"50%\", left: \"clamp(24px, calc(var(--icp) * 1%), calc(100% - 24px))\", transform: \"translate(-50%, -50%)\" }\n        }\n      >\n        <div\n          ref={gripRef}\n          role=\"slider\"\n          tabIndex={0}\n          aria-label=\"Comparison position\"\n          aria-orientation={vertical ? \"vertical\" : \"horizontal\"}\n          aria-valuemin={0}\n          aria-valuemax={100}\n          aria-valuenow={Math.round(start)}\n          aria-valuetext={Math.round(start) + \"% \" + labelBefore + \", \" + (100 - Math.round(start)) + \"% \" + labelAfter}\n          onKeyDown={onKeyDown}\n          className=\"flex h-11 w-11 items-center justify-center rounded-full text-white outline-none focus-visible:!outline-2 focus-visible:!outline-offset-2 focus-visible:!outline-white focus-visible:!rounded-full focus-visible:shadow-[0_0_0_6px_rgba(0,0,0,0.45)]\"\n          style={{ cursor: vertical ? \"ns-resize\" : \"ew-resize\" }}\n        >\n          <span\n            aria-hidden\n            className=\"flex h-11 w-11 items-center justify-center rounded-full group-hover/ic:scale-105 group-data-[drag=true]/ic:!scale-90\"\n            style={{\n              background: \"rgba(255,255,255,0.16)\",\n              backdropFilter: \"blur(14px) saturate(1.4)\",\n              WebkitBackdropFilter: \"blur(14px) saturate(1.4)\",\n              boxShadow:\n                \"inset 0 0 0 1px rgba(255,255,255,0.55), inset 0 1px 0 rgba(255,255,255,0.4), 0 8px 24px rgba(0,0,0,0.35), 0 0 0 1px rgba(0,0,0,0.12)\",\n              transition: \"scale 240ms cubic-bezier(0.2, 0.8, 0.2, 1), transform 240ms cubic-bezier(0.2, 0.8, 0.2, 1)\",\n            }}\n          >\n            <svg\n              width=\"30\"\n              height=\"30\"\n              viewBox=\"0 0 30 30\"\n              fill=\"none\"\n              stroke=\"currentColor\"\n              strokeWidth=\"2.2\"\n              strokeLinecap=\"round\"\n              strokeLinejoin=\"round\"\n              style={{ transform: vertical ? \"rotate(90deg)\" : undefined, filter: \"drop-shadow(0 1px 2px rgba(0,0,0,0.35))\" }}\n            >\n              <path\n                d=\"M11.5 10 6.5 15l5 5\"\n                className=\"group-hover/ic:-translate-x-0.5 group-data-[drag=true]/ic:!-translate-x-1\"\n                style={{ transition: \"translate 240ms cubic-bezier(0.2, 0.8, 0.2, 1), transform 240ms cubic-bezier(0.2, 0.8, 0.2, 1)\" }}\n              />\n              <path\n                d=\"M18.5 10l5 5-5 5\"\n                className=\"group-hover/ic:translate-x-0.5 group-data-[drag=true]/ic:!translate-x-1\"\n                style={{ transition: \"translate 240ms cubic-bezier(0.2, 0.8, 0.2, 1), transform 240ms cubic-bezier(0.2, 0.8, 0.2, 1)\" }}\n              />\n            </svg>\n          </span>\n        </div>\n      </div>\n\n      {/* hairline frame over the images, so a bright photo never bleeds into the page */}\n      <div aria-hidden className=\"pointer-events-none absolute inset-0 z-[5] rounded-2xl\" style={{ boxShadow: \"inset 0 0 0 1px rgba(255,255,255,0.12)\" }} />\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/image-comparison-slider.tsx"}],"meta":{"tier":"free","tags":["image","slider","compare","drag","before after","vertical","hover","reveal"],"href":"https://viberdy.dev/library/image-comparison-slider","aiPrompt":"Create a single-file React + Tailwind component named ImageComparisonSlider (a \"use client\" file, React only, no animation library).\n\nProps (all optional): before and after (image URLs; when missing or rejected, draw a built-in SVG scene instead: before = night, after = day, the same mountain lake with a lit cabin window at night), initial 50 (0-100), labelBefore \"Before\", labelAfter \"After\", orientation \"horizontal\" | \"vertical\" (default horizontal), hover false (a mouse moves the divider with no press; touch and pen still need a drag), onChange(position), className (appended to the root). Validate both URLs with a safeSrc helper: strip tabs and newlines, trim, reject anything containing a backslash, reject any scheme other than http/https, allow relative URLs.\n\nStructure: root `relative isolate h-64 w-full max-w-xl select-none overflow-hidden rounded-2xl` (class group/ic, data-drag false/true), touch-action pan-y (pan-x when vertical), cursor ew-resize / ns-resize. Layers: the after image fills the frame (object-cover), then the before image, clipped with clip-path inset(0 calc(100% - var(--icp) * 1%) 0 0) (vertical: inset(0 0 calc(100% - var(--icp) * 1%) 0)). --icp is the divider position 0-100 and the only thing that moves. Divider: a 2px white line fading to transparent over the first and last 14%, with a 12px white glow, at left calc(var(--icp) * 1%); a 36px gradient from rgba(0,0,0,.22) to transparent on the after side as a cast shadow. Grip: a 44px circle centred on the line but clamped to stay 24px inside the frame (left: clamp(24px, calc(var(--icp) * 1%), calc(100% - 24px))): glass, rgba(255,255,255,.16) with backdrop blur 14px saturate 1.4, inset 1px white/55 ring, inset top highlight, drop shadow 0 8px 24px rgba(0,0,0,.35); two white chevrons (stroke 2.2) that spread 2px on hover and 4px while dragging (rotate the icon 90deg when vertical); the grip scales 1.05 on hover and 0.9 while dragging (transition 240ms cubic-bezier(.2,.8,.2,1)). Labels: two pills (11px uppercase semibold, tracking .12em, white on rgba(8,10,14,.5), blur 10px, 1px white/16 ring) at 12px insets, before bottom-left (vertical: top-left), after bottom-right (vertical: bottom-left); their opacity is a CSS variable (--icb, --ica) written by the loop: it reaches 0 as the divider closes within 34px of the label's edge (measure labels and root with a ResizeObserver). A 1px white/12 inset hairline sits over the frame.\n\nMotion: one requestAnimationFrame loop inside a mount effect runs a semi-implicit Euler spring (240 Hz substeps) from the current to the target position and writes --icp, --icb, --ica and the slider's aria-valuenow / aria-valuetext directly to the DOM (React renders initial values once). Soft spring k=240 c=26 when the target jumps (press, key, hover); stiff spring k=1100 c=66 while dragging. The loop sleeps once position error < 0.012 and speed < 0.05. A change of the `initial` prop snaps to the new value.\n\nPointer: pointerdown anywhere (mouse: primary button only) captures the pointer, focuses the slider with preventScroll and moves the target to the pointer along the active axis (fraction of the frame, via getBoundingClientRect). If the press lands on the grip, keep the offset so it does not jump. Touch pointers that land off the grip wait: after 6px of movement along the active axis it becomes a drag, along the other axis it is a page scroll and is ignored, and a pointerup without movement is a tap that jumps there. Drag ends on pointerup/cancel (or a mouse move with no buttons). In hover mode a mouse move without a press sets the target.\n\nKeyboard: the grip is role=slider tabIndex 0, aria-label \"Comparison position\", aria-orientation, aria-valuemin 0 / max 100 / now, aria-valuetext \"50% Before, 50% After\". Horizontal: ArrowLeft/ArrowDown -2, ArrowRight/ArrowUp +2; vertical: ArrowUp/ArrowLeft -2, ArrowDown/ArrowRight +2; Shift makes it 10; PageUp -10, PageDown +10, Home 0, End 100. Focus ring: important utilities, 2px white outline with offset 2 plus a 6px rgba(0,0,0,.45) halo, on a rounded-full 44px slider element.\n\nprefers-reduced-motion: no spring, the position snaps. Paint everything with plain colours (it must not depend on site tokens); it works on any page because the images are its own surface.","license":"https://viberdy.dev/license"}}