{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ripple-click-effect","type":"registry:component","title":"Ripple Click Effect","description":"A Material-style ripple from the exact press point that grows to the farthest corner, holds while you hold and fades on release. A button, plus a wrapper for any card or list row. No dependencies.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/ripple-click-effect.tsx","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\nimport type {\n  ComponentPropsWithoutRef,\n  RefObject,\n  KeyboardEvent as ReactKeyboardEvent,\n  PointerEvent as ReactPointerEvent,\n} from \"react\";\n\nexport type RippleTone = \"light\" | \"dark\";\n\ntype RippleOptions = {\n  color: RippleTone;\n  rippleColor?: string;\n  duration: number;\n  centered: boolean;\n  disabled: boolean;\n};\n\n// Interactive things a key press can start a ripple from.\nconst INTERACTIVE =\n  \"button, a[href], summary, [role=button], [role=menuitem], [role=option], [role=tab], [role=switch], [role=checkbox]\";\n// rippleColor reaches a style attribute, so only plain CSS colour syntax is allowed through.\nconst SAFE_COLOR = /^[#a-zA-Z0-9(),.%\\s/-]+$/;\nconst MAX_RIPPLES = 14;\nconst MIN_SHOW = 110;\nconst GROW_EASE = \"cubic-bezier(0.2, 0, 0, 1)\";\nconst noop = () => {};\n\n/** Spawn one ripple inside `layer`. Returns the function that releases (fades) it. */\nfunction spawn(layer: HTMLElement, o: RippleOptions, point: { x: number; y: number } | null): () => void {\n  const w = layer.offsetWidth;\n  const h = layer.offsetHeight;\n  if (!w || !h) return noop;\n\n  // Layout pixels, not screen pixels: a transform-scaled parent shrinks getBoundingClientRect.\n  const rect = layer.getBoundingClientRect();\n  const k = rect.width ? w / rect.width : 1;\n  let x = w / 2;\n  let y = h / 2;\n  if (point && !o.centered) {\n    x = Math.min(w, Math.max(0, (point.x - rect.left) * k));\n    y = Math.min(h, Math.max(0, (point.y - rect.top) * k));\n  }\n\n  const custom = o.rippleColor && SAFE_COLOR.test(o.rippleColor) ? o.rippleColor : null;\n  const base = custom ?? (o.color === \"dark\" ? \"#000\" : \"#fff\");\n  const strength = custom ? 30 : o.color === \"dark\" ? 24 : 34;\n  const tint = (pct: number) => \"color-mix(in srgb, \" + base + \" \" + pct.toFixed(1) + \"%, transparent)\";\n\n  const reduce = typeof window.matchMedia === \"function\" && window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const el = document.createElement(\"span\");\n  el.style.position = \"absolute\";\n  el.style.opacity = \"0\";\n  el.style.pointerEvents = \"none\";\n\n  let fadeIn: Animation;\n  if (reduce) {\n    // Calm equivalent: the whole surface washes in and out, nothing travels.\n    el.style.inset = \"0\";\n    el.style.background = tint(strength * 0.7);\n    fadeIn = el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 140, easing: \"ease-out\", fill: \"forwards\" });\n  } else {\n    // Twice the distance to the farthest corner: the disc always covers the whole surface.\n    const size = Math.hypot(Math.max(x, w - x), Math.max(y, h - y)) * 2;\n    el.style.left = x - size / 2 + \"px\";\n    el.style.top = y - size / 2 + \"px\";\n    el.style.width = size + \"px\";\n    el.style.height = size + \"px\";\n    el.style.borderRadius = \"50%\";\n    el.style.willChange = \"transform, opacity\";\n    // A soft body with a slightly brighter leading rim, so it reads as a wave and not a flat disc.\n    el.style.background =\n      \"radial-gradient(circle closest-side, \" +\n      tint(strength * 0.82) + \" 0%, \" +\n      tint(strength * 0.82) + \" 66%, \" +\n      tint(strength * 1.2) + \" 93%, \" +\n      tint(0) + \" 100%)\";\n    el.animate([{ transform: \"scale(0)\" }, { transform: \"scale(1)\" }], {\n      duration: o.duration,\n      easing: GROW_EASE,\n      fill: \"forwards\",\n    });\n    fadeIn = el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 40, easing: \"linear\", fill: \"forwards\" });\n  }\n\n  while (layer.childElementCount >= MAX_RIPPLES) layer.firstElementChild?.remove();\n  layer.appendChild(el);\n\n  let released = false;\n  const born = performance.now();\n  const fade = () => {\n    const now = Number(getComputedStyle(el).opacity);\n    fadeIn.cancel();\n    const out = el.animate([{ opacity: Number.isFinite(now) ? now : 1 }, { opacity: 0 }], {\n      duration: reduce ? 320 : Math.max(320, o.duration * 0.62),\n      easing: \"ease-out\",\n      fill: \"forwards\",\n    });\n    out.onfinish = () => el.remove();\n  };\n  return () => {\n    if (released) return;\n    released = true;\n    // A tap that lifts instantly still shows a full ripple: hold at least MIN_SHOW ms before fading.\n    const wait = MIN_SHOW - (performance.now() - born);\n    if (wait > 0) window.setTimeout(fade, wait);\n    else fade();\n  };\n}\n\nfunction useRipple(layerRef: RefObject<HTMLSpanElement | null>, o: RippleOptions) {\n  const opts = useRef(o);\n  const live = useRef(new Set<() => void>());\n  const keyEnd = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = o;\n  });\n  useEffect(() => {\n    const set = live.current;\n    return () => {\n      set.forEach((release) => release());\n      set.clear();\n    };\n  }, []);\n\n  function onPointerDown(e: ReactPointerEvent<HTMLElement>) {\n    const layer = layerRef.current;\n    if (!layer || opts.current.disabled) return;\n    if (e.pointerType === \"mouse\" && e.button !== 0) return;\n    const release = spawn(layer, opts.current, { x: e.clientX, y: e.clientY });\n    const end = () => {\n      window.removeEventListener(\"pointerup\", end);\n      window.removeEventListener(\"pointercancel\", end);\n      live.current.delete(end);\n      release();\n    };\n    live.current.add(end);\n    window.addEventListener(\"pointerup\", end);\n    window.addEventListener(\"pointercancel\", end);\n  }\n\n  function onKeyDown(e: ReactKeyboardEvent<HTMLElement>) {\n    const layer = layerRef.current;\n    if (!layer || opts.current.disabled || e.repeat) return;\n    if (e.key !== \"Enter\" && e.key !== \" \") return;\n    const target = e.target instanceof Element ? e.target.closest(INTERACTIVE) : null;\n    if (!target) return;\n    const r = target.getBoundingClientRect();\n    keyEnd.current?.();\n    const release = spawn(layer, opts.current, { x: r.left + r.width / 2, y: r.top + r.height / 2 });\n    const end = () => {\n      live.current.delete(end);\n      keyEnd.current = null;\n      release();\n    };\n    live.current.add(end);\n    keyEnd.current = end;\n  }\n\n  function release() {\n    keyEnd.current?.();\n  }\n\n  return { onPointerDown, onKeyDown, release };\n}\n\nconst LAYER = \"pointer-events-none absolute inset-0 overflow-hidden rounded-[inherit]\";\n\nexport type RippleButtonProps = Omit<ComponentPropsWithoutRef<\"button\">, \"color\"> & {\n  label?: string;\n  /** \"light\" ripples a dark/colored button, \"dark\" ripples a light one. */\n  color?: RippleTone;\n  /** Any CSS colour; overrides `color` (e.g. \"oklch(0.7 0.2 250)\"). */\n  rippleColor?: string;\n  /** Milliseconds the ripple takes to reach the far corner. */\n  duration?: number;\n  /** Always start from the centre instead of the pointer. */\n  centered?: boolean;\n};\n\nexport function RippleButton({\n  label = \"Click me\",\n  color = \"light\",\n  rippleColor,\n  duration = 700,\n  centered = false,\n  className = \"\",\n  children,\n  disabled,\n  onPointerDown,\n  onKeyDown,\n  onKeyUp,\n  onBlur,\n  ...rest\n}: RippleButtonProps) {\n  const layerRef = useRef<HTMLSpanElement>(null);\n  const rp = useRipple(layerRef, { color, rippleColor, duration, centered, disabled: !!disabled });\n  return (\n    <button\n      {...rest}\n      disabled={disabled}\n      className={\n        \"relative overflow-hidden rounded-xl bg-red-500 px-8 py-4 font-semibold text-white transition-transform duration-150 ease-out active:scale-[0.985] focus-visible:!outline-2 focus-visible:!outline-offset-2 focus-visible:!outline-blue-500 disabled:cursor-not-allowed disabled:opacity-50 motion-reduce:transition-none motion-reduce:active:scale-100 \" +\n        className\n      }\n      onPointerDown={(e) => {\n        onPointerDown?.(e);\n        rp.onPointerDown(e);\n      }}\n      onKeyDown={(e) => {\n        onKeyDown?.(e);\n        rp.onKeyDown(e);\n      }}\n      onKeyUp={(e) => {\n        onKeyUp?.(e);\n        rp.release();\n      }}\n      onBlur={(e) => {\n        onBlur?.(e);\n        rp.release();\n      }}\n    >\n      {children ?? label}\n      <span ref={layerRef} aria-hidden=\"true\" className={LAYER} />\n    </button>\n  );\n}\n\nexport type RippleProps = Omit<ComponentPropsWithoutRef<\"div\">, \"color\"> & {\n  as?: \"div\" | \"span\" | \"li\" | \"section\" | \"article\";\n  color?: RippleTone;\n  rippleColor?: string;\n  duration?: number;\n  centered?: boolean;\n  disabled?: boolean;\n};\n\n/**\n * The same ripple on any surface: a card, a list row, an image tile. Pointer presses ripple from the\n * pointer; Enter or Space on a focusable control inside (or on the surface itself, with role=\"button\"\n * and tabIndex) ripples from that control. Give the surface its own radius; the ripple follows it.\n */\nexport function Ripple({\n  as = \"div\",\n  color = \"light\",\n  rippleColor,\n  duration = 700,\n  centered = false,\n  disabled = false,\n  className = \"\",\n  children,\n  onPointerDown,\n  onKeyDown,\n  onKeyUp,\n  onBlur,\n  ...rest\n}: RippleProps) {\n  const layerRef = useRef<HTMLSpanElement>(null);\n  const rp = useRipple(layerRef, { color, rippleColor, duration, centered, disabled });\n  const Tag = as as \"div\";\n  return (\n    <Tag\n      {...rest}\n      className={\"relative \" + className}\n      onPointerDown={(e) => {\n        onPointerDown?.(e);\n        rp.onPointerDown(e);\n      }}\n      onKeyDown={(e) => {\n        onKeyDown?.(e);\n        rp.onKeyDown(e);\n      }}\n      onKeyUp={(e) => {\n        onKeyUp?.(e);\n        rp.release();\n      }}\n      onBlur={(e) => {\n        onBlur?.(e);\n        rp.release();\n      }}\n    >\n      {children}\n      <span ref={layerRef} aria-hidden=\"true\" className={LAYER} />\n    </Tag>\n  );\n}\n","type":"registry:component","target":"components/viberdy/ripple-click-effect.tsx"}],"meta":{"tier":"free","tags":["ripple","button","click","material","touch feedback","card","list"],"href":"https://viberdy.dev/library/ripple-click-effect","aiPrompt":"Build a single self-contained \"use client\" React + Tailwind file with no dependencies (React only) that exports two components, RippleButton and Ripple, plus the types RippleTone, RippleButtonProps and RippleProps. It is a Material-style ripple done properly.\n\nShared engine (a useRipple hook plus a spawn function):\n- Ripples are plain span elements appended imperatively into an absolutely positioned layer (absolute inset-0, overflow-hidden, rounded-[inherit], pointer-events-none, aria-hidden). Animate with the Web Animations API, not state, so rapid presses never re-render.\n- On pointerdown (mouse primary button only; touch and pen too) spawn a ripple at the press point. Convert the pointer to LAYOUT pixels: (clientX - rect.left) * (layer.offsetWidth / rect.width), same for y, so it stays correct inside a transform-scaled parent. Clamp to the layer.\n- Size = Math.hypot(Math.max(x, w - x), Math.max(y, h - y)) * 2 where w,h are the layer's offsetWidth/offsetHeight, so the disc always covers the farthest corner. Center it with left/top = x - size/2, y - size/2.\n- Grow: transform scale(0) to scale(1) over `duration` ms (default 700) with cubic-bezier(0.2, 0, 0, 1), fill forwards. Fade in opacity 0 to 1 over 90ms. It keeps expanding while held and stays visible.\n- Release on window pointerup or pointercancel (register once per press, remove after): read the current computed opacity, cancel the fade-in, animate opacity from it to 0 over max(320, duration * 0.62) ms ease-out, then remove the span. Cap at 14 live ripples (remove the oldest first). Overlapping presses are independent.\n- Look: radial-gradient(circle closest-side) with color-mix(in srgb, BASE pct%, transparent) stops. Base is #fff for color \"light\" (strength 34%) and #000 for \"dark\" (24%); rippleColor (any CSS colour, validated against /^[#a-zA-Z0-9(),.%\\s/-]+$/ before it touches a style) overrides at strength 30%. Stops: 0 to 66% at strength*0.82, 93% at strength*1.2 (a slightly brighter rim), 100% at 0.\n- Keyboard: on keydown of Enter or Space (not repeat) when the event target (or an ancestor) matches button, a[href], summary or a role of button/menuitem/option/tab/switch/checkbox, spawn a ripple from the centre of that control and hold it; release on keyup or blur. Ignore inputs.\n- prefers-reduced-motion (check matchMedia when spawning): no travel and no scale. A full-surface wash (inset 0, strength*0.7) fades in over 140ms and out over 320ms.\n- On unmount release every live ripple and remove window listeners. User-supplied onPointerDown/onKeyDown/onKeyUp/onBlur props run first, then the ripple handlers.\n\nRippleButton props: label = \"Click me\", color: \"light\" | \"dark\" = \"light\", rippleColor?, duration = 700, centered = false (always start from the middle), plus every native button prop (children replace label). Default look: relative overflow-hidden rounded-xl bg-red-500 px-8 py-4 font-semibold text-white, pressed scale 0.985 (150ms ease-out, removed under motion-reduce), focus-visible:!outline-2 !outline-offset-2 !outline-blue-500, disabled: not-allowed + 50% opacity and no ripple. Append className after the defaults. Do not set type.\n\nRipple: the same engine on any element. Props as, color, rippleColor, duration, centered, disabled plus native div props. `as` is div | span | li | section | article, default div. Renders the host with class \"relative\" + className, children, then the ripple layer. The surface supplies its own radius. Document that a clickable surface needs role=\"button\" and tabIndex={0} or a real button inside.\n\nFonts and colours: inherit; no site tokens. Output only the file.","license":"https://viberdy.dev/license"}}