# Ripple

> Zero-dependency tactile press ripple and hover state layer powered by the Web Animations API.

Canonical URL: https://taut-ui.com/docs/lib/ripple

A lightweight Client Component that renders a radial press ripple and hover state layer inside any interactive parent element using the Web Animations API. Used across `Button`, `Accordion`, `Card`, `Command`, `Drawer`, and `Sidebar`.

## Installation [#installation]

**CLI**

```bash
npx shadcn@latest add https://taut-ui.com/r/ripple.json
```

**Manual**

- Copy and paste the following code into your project.

```tsx title="lib/ripple.tsx"
"use client";

import * as React from "react";

type PressOptions = Required<
  Pick<RippleProps, "color" | "duration" | "opacity" | "minimumPressDuration">
>;

interface Press {
  release: () => void;
  cancel: () => void;
}

const EASE = "cubic-bezier(0.2, 0, 0, 1)";

function startPress(
  container: HTMLElement,
  x: number | undefined,
  y: number | undefined,
  { color, duration, opacity, minimumPressDuration }: PressOptions,
  onDone: () => void,
): Press | null {
  const rect = container.getBoundingClientRect();
  if (!rect.width || !rect.height) return null;

  const maxDim = Math.max(rect.width, rect.height);
  const size = Math.max(Math.floor(maxDim * 0.2), 1);
  const scale = (Math.hypot(rect.width, rect.height) * 1.35 + 24) / size;

  const end = { x: (rect.width - size) / 2, y: (rect.height - size) / 2 };
  const start =
    x != null && y != null
      ? { x: x - rect.left - size / 2, y: y - rect.top - size / 2 }
      : end;

  const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

  const el = document.createElement("span");
  Object.assign(el.style, {
    background: `radial-gradient(closest-side, ${color} 80%, transparent 100%)`,
    borderRadius: "50%",
    height: `${size}px`,
    left: "0",
    opacity: "0",
    pointerEvents: "none",
    position: "absolute",
    top: "0",
    width: `${size}px`,
    willChange: "transform, opacity",
  });
  container.appendChild(el);

  const grow = el.animate(
    [
      { transform: `translate(${start.x}px, ${start.y}px) scale(1)` },
      { transform: `translate(${end.x}px, ${end.y}px) scale(${scale})` },
    ],
    { duration: reduced ? 0 : duration, easing: EASE, fill: "forwards" },
  );
  el.animate([{ opacity: 0 }, { opacity }], { duration: 75, fill: "forwards" });

  const startedAt = performance.now();
  let released = false;
  let timer: number | undefined;

  const cleanup = (): void => {
    window.clearTimeout(timer);
    grow.cancel();
    el.remove();
    onDone();
  };

  return {
    cancel: cleanup,
    release: (): void => {
      if (released) return;
      released = true;
      const wait = Math.max(
        0,
        minimumPressDuration - (performance.now() - startedAt),
      );
      timer = window.setTimeout((): void => {
        const fade = el.animate([{ opacity }, { opacity: 0 }], {
          duration: 375,
          fill: "forwards",
        });
        fade.onfinish = cleanup;
      }, wait);
    },
  };
}

function Ripple({
  color = "currentColor",
  duration = 450,
  opacity = 0.1,
  hoverOpacity = 0.08,
  hoverDuration = 150,
  minimumPressDuration = 225,
  disabled = false,
  className,
  style,
  ...props
}: RippleProps): React.JSX.Element {
  const containerRef = React.useRef<HTMLSpanElement>(null);
  const hoverRef = React.useRef<HTMLSpanElement>(null);

  const optionsRef = React.useRef({
    color,
    duration,
    hoverOpacity,
    minimumPressDuration,
    opacity,
  });
  optionsRef.current = {
    color,
    duration,
    hoverOpacity,
    minimumPressDuration,
    opacity,
  };

  React.useEffect((): (() => void) | undefined => {
    const container = containerRef.current;
    const parent = container?.parentElement;
    if (!container || !parent || disabled) return;
    if (typeof container.animate !== "function") return;

    if (getComputedStyle(parent).position === "static") {
      parent.style.position = "relative";
    }

    const presses = new Set<Press>();
    let current: Press | null = null;

    const isDisabled = (): boolean =>
      (parent as HTMLButtonElement).disabled === true ||
      parent.getAttribute("aria-disabled") === "true" ||
      parent.hasAttribute("data-disabled");

    const setHover = (on: boolean): void => {
      if (hoverRef.current) {
        hoverRef.current.style.opacity = on
          ? `${optionsRef.current.hoverOpacity}`
          : "0";
      }
    };

    const endPress = (): void => {
      current?.release();
      current = null;
    };

    const begin = (x?: number, y?: number): void => {
      endPress();
      const press = startPress(container, x, y, optionsRef.current, () =>
        presses.delete(press as Press),
      );
      if (press) {
        presses.add(press);
        current = press;
      }
    };

    const onEnter = (e: PointerEvent): void => {
      if (e.pointerType !== "touch" && !isDisabled()) setHover(true);
    };
    const onLeave = (): void => {
      setHover(false);
      endPress();
    };
    const onDown = (e: PointerEvent): void => {
      if (!e.isPrimary || e.button !== 0 || isDisabled()) return;
      begin(e.clientX, e.clientY);
    };
    const onKeyDown = (e: KeyboardEvent): void => {
      if (e.repeat || isDisabled() || (e.key !== "Enter" && e.key !== " "))
        return;
      begin();
    };
    const onKeyUp = (e: KeyboardEvent): void => {
      if (e.key === "Enter" || e.key === " ") endPress();
    };

    const listeners: [string, EventListener][] = [
      ["pointerenter", onEnter as EventListener],
      ["pointerleave", onLeave],
      ["pointerdown", onDown as EventListener],
      ["pointerup", endPress],
      ["pointercancel", endPress],
      ["keydown", onKeyDown as EventListener],
      ["keyup", onKeyUp as EventListener],
      ["blur", endPress],
    ];
    for (const [name, fn] of listeners) parent.addEventListener(name, fn);

    return () => {
      for (const [name, fn] of listeners) parent.removeEventListener(name, fn);
      for (const press of [...presses]) press.cancel();
      setHover(false);
    };
  }, [disabled]);

  return (
    <span
      aria-hidden="true"
      className={className}
      data-slot="ripple"
      ref={containerRef}
      style={{
        borderRadius: "inherit",
        inset: 0,
        overflow: "hidden",
        pointerEvents: "none",
        position: "absolute",
        ...style,
      }}
      {...props}
    >
      <span
        data-slot="ripple-hover"
        ref={hoverRef}
        style={{
          backgroundColor: color,
          inset: 0,
          opacity: 0,
          position: "absolute",
          transition: hoverDuration > 0 ? `opacity ${hoverDuration}ms` : "none",
        }}
      />
    </span>
  );
}

export interface RippleProps extends React.HTMLAttributes<HTMLSpanElement> {
  color?: string;
  duration?: number;
  opacity?: number;
  hoverOpacity?: number;
  hoverDuration?: number;
  minimumPressDuration?: number;
  disabled?: boolean;
}

export { Ripple };
export default Ripple;
```

- Update the import paths to match your project setup.

## How It Works [#how-it-works]

- **Parent-attached listeners:** `Ripple` listens directly on its `parentElement` for pointer and keyboard (`Enter`, `Space`) events—no wrapper element required. If the parent has `position: static`, `Ripple` upgrades it to `relative` and clips via `border-radius: inherit` and `overflow: hidden`.
- **Origin-aware expansion:** Pointer presses originate at the click coordinates and travel smoothly toward the center of the element, scaling across its full diagonal (`Math.hypot`). Keyboard activations originate from the center.
- **Minimum press hold:** Releases wait until `minimumPressDuration` (`225ms` by default) has elapsed before fading out, preventing quick taps from flashing abruptly.
- **Touch & disabled awareness:** Touch pointers skip the hover layer (`pointerType !== "touch"`), disabled controls (`disabled`, `aria-disabled="true"`, `data-disabled`) ignore presses, and `prefers-reduced-motion: reduce` collapses the growth duration to `0ms`.

## Usage [#usage]

### Basic [#basic]

Place `<Ripple />` as a child inside any interactive trigger (`button`, `a`, or custom clickable container):

```tsx
import { Ripple } from "@/lib/ripple";

export function InteractiveTile({ children }: { children: React.ReactNode }) {
  return (
    <button
      className="relative overflow-hidden rounded-2xl border bg-card px-4 py-3 text-card-foreground"
      type="button"
    >
      <Ripple />
      <span className="relative z-10">{children}</span>
    </button>
  );
}
```

### Press-Only Ripple (Without Hover Overlay) [#press-only-ripple-without-hover-overlay]

For items that already apply their own hover background (such as menu or drawer items), set `hoverOpacity={0}`:

```tsx
<Ripple hoverOpacity={0} />
```

### Custom Color and Intensity [#custom-color-and-intensity]

By default `Ripple` uses `currentColor`, matching the parent text color. You can override `color`, `opacity`, and `duration`:

```tsx
<Ripple color="var(--color-primary)" duration={500} opacity={0.16} />
```

## API [#api]

### `RippleProps` [#rippleprops]

Extends `React.HTMLAttributes<HTMLSpanElement>`.

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `color` | `string` | `"currentColor"` | CSS color used for both the radial ripple and hover overlay. |
| `duration` | `number` | `450` | Ripple expansion duration in milliseconds. |
| `opacity` | `number` | `0.1` | Peak opacity of the active press ripple. |
| `hoverOpacity` | `number` | `0.08` | Opacity of the hover state layer (`0` disables hover tint). |
| `hoverDuration` | `number` | `150` | Fade duration in milliseconds for the hover state layer. |
| `minimumPressDuration` | `number` | `225` | Minimum milliseconds the ripple stays visible before fading out on release. |
| `disabled` | `boolean` | `false` | Disables pointer/keyboard listeners and hides the hover state layer. |
