Hold to confirm: a button you have to mean

Build a press-and-hold confirm button in React drawn as a rubber stamp: a ring that fills while held, falls back if let go, and an uneven red imprint that lands on the document.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

Some actions deserve more than a click: approving a purchase, deleting a project. A hold-to-confirm button asks the visitor to mean it, without a modal.

Progress that climbs and falls

While the button is held, progress climbs; let go early and it falls back quickly. Only a full hold confirms. The same logic works with the keyboard by holding Space or Enter.

src/registry/interactive/stamp/use-hold.ts

"use client";

import { useRef, type KeyboardEvent } from "react";
import { animate, useMotionValue, type AnimationPlaybackControls, type MotionValue } from "motion/react";

// How quickly the progress falls back when the hold is let go early, in seconds.
const RELEASE_SECONDS = 0.25;

interface HoldOptions {
  // How long the control must be held, in seconds.
  seconds: number;
  onComplete: () => void;
  isDisabled?: boolean;
}

interface Hold {
  // 0 at rest, 1 when the hold is complete.
  progress: MotionValue<number>;
  reset: () => void;
  // Spread onto the button: the pointer, the space bar and Enter all hold it.
  handlers: {
    onPointerDown: () => void;
    onPointerUp: () => void;
    onPointerLeave: () => void;
    onPointerCancel: () => void;
    onKeyDown: (event: KeyboardEvent) => void;
    onKeyUp: (event: KeyboardEvent) => void;
  };
}

const isHoldKey = (event: KeyboardEvent) => event.key === " " || event.key === "Enter";

// Press and hold: progress climbs while the control is held and falls back if it is let go before the end, so the
// action can't happen by accident. It continues from where it is, so a quick second press isn't a fresh start.
export function useHold({ seconds, onComplete, isDisabled = false }: HoldOptions): Hold {
  const progress = useMotionValue(0);
  const playback = useRef<AnimationPlaybackControls | null>(null);

  const start = () => {
    if (isDisabled || progress.get() >= 1) return;
    playback.current?.stop();
    playback.current = animate(progress, 1, { duration: seconds * (1 - progress.get()), ease: "linear", onComplete });
  };
  const release = () => {
    if (progress.get() >= 1) return;
    playback.current?.stop();
    playback.current = animate(progress, 0, { duration: RELEASE_SECONDS });
  };
  const reset = () => {
    playback.current?.stop();
    progress.set(0);
  };

  return {
    progress,
    reset,
    handlers: {
      onPointerDown: start,
      onPointerUp: release,
      onPointerLeave: release,
      onPointerCancel: release,
      onKeyDown: (event) => {
        if (!isHoldKey(event) || event.repeat) return;
        event.preventDefault();
        start();
      },
      onKeyUp: (event) => {
        if (isHoldKey(event)) release();
      },
    },
  };
}
The hold hook

A stamp, not a spinner

The button is a round red stamp seen from above, its ring filling while held. When the ring closes, an imprint lands on the purchase order a few degrees off level, a different few each time, with an SVG noise filter eating small gaps out of the ink, the way a real stamp never inks evenly.

src/registry/interactive/stamp/stamp-mark.tsx

"use client";

import { useId } from "react";
import { motion } from "motion/react";
import { SPRING_SNAPPY } from "@/lib/motion";
import { cn } from "@/lib/utils";

interface StampMarkProps {
  // The big word, and the two small lines under it: who and when.
  word: string;
  by: string;
  at: string;
  // Degrees off level: a stamp never lands square.
  tilt: number;
  isReduced: boolean;
  className?: string;
}

// The imprint a rubber stamp leaves: a double-ruled box, one heavy word and two small lines, in red ink. A noise
// filter eats small gaps out of it, the way a stamp never inks evenly. It lands large and settles on a spring.
export function StampMark({ word, by, at, tilt, isReduced, className }: StampMarkProps) {
  const ink = useId();
  return (
    <motion.svg
      aria-hidden
      viewBox="0 0 240 120"
      initial={isReduced ? false : { scale: 1.7, opacity: 0, rotate: tilt - 6 }}
      animate={{ scale: 1, opacity: 0.85, rotate: tilt }}
      transition={SPRING_SNAPPY}
      className={cn("overflow-visible text-red-600", className)}
    >
      <defs>
        <filter id={ink} x="-10%" y="-10%" width="120%" height="120%">
          <feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="2" seed="7" result="grain" />
          <feColorMatrix
            in="grain"
            type="matrix"
            values="0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 -1.3 1.25"
            result="gaps"
          />
          <feComposite in="SourceGraphic" in2="gaps" operator="in" />
        </filter>
      </defs>
      <g filter={`url(#${ink})`} className="fill-none stroke-current">
        <rect x="4" y="4" width="232" height="112" rx="12" strokeWidth="3.5" />
        <rect x="14" y="14" width="212" height="92" rx="6" strokeWidth="1.5" />
        <text
          x="120"
          y="60"
          textAnchor="middle"
          className="fill-current stroke-none text-3xl font-extrabold tracking-widest uppercase"
        >
          {word}
        </text>
        <path d="M30 74 H210" strokeWidth="1.5" />
        <text
          x="120"
          y="93"
          textAnchor="middle"
          className="fill-current stroke-none text-xs font-bold tracking-wide uppercase"
        >
          {by} · {at}
        </text>
      </g>
    </motion.svg>
  );
}
The imprint

More articles