{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hold-to-confirm","type":"registry:ui","title":"Hold to confirm","description":"Hold to confirm, part of the Actions family.","dependencies":["clsx","tailwind-merge"],"registryDependencies":["https://caliperui.site/r/styles.json"],"files":[{"path":"ui/hold-to-confirm.tsx","type":"registry:ui","target":"components/ui/hold-to-confirm.tsx","content":"\"use client\";\n\nimport {\n  useEffect,\n  useId,\n  useRef,\n  useState,\n  type KeyboardEvent,\n  type PointerEvent,\n  type ReactNode,\n} from \"react\";\nimport { cn } from \"./lib/cn\";\nimport \"./shared/action-label.css\";\nimport \"./hold-to-confirm.css\";\n\nfunction usePrefersReducedMotion() {\n  const [reduced, setReduced] = useState(false);\n  useEffect(() => {\n    const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const update = () => setReduced(query.matches);\n    update();\n    query.addEventListener(\"change\", update);\n    return () => query.removeEventListener(\"change\", update);\n  }, []);\n  return reduced;\n}\n\n/**\n * An irreversible action that needs a deliberate gesture. The fill soaks out\n * from where the press landed, a countdown says how long is left, and letting\n * go drains it back. Space or Enter can be held from the keyboard.\n */\nexport function HoldToConfirm({\n  children,\n  label = \"Hold to confirm\",\n  doneLabel = \"Confirmed\",\n  duration = 1500,\n  onConfirm,\n  className,\n}: {\n  children?: ReactNode;\n  /** Accessible name and visible label while idle. */\n  label?: string;\n  /** Label after the hold completes. */\n  doneLabel?: string;\n  /** How long to hold, in milliseconds. */\n  duration?: number;\n  onConfirm: () => void;\n  className?: string;\n}) {\n  const hint = useId();\n  const reduced = usePrefersReducedMotion();\n  const [progress, setProgress] = useState(0);\n  const [phase, setPhase] = useState<\"idle\" | \"holding\" | \"draining\" | \"done\">(\"idle\");\n  const [origin, setOrigin] = useState<{ x: number; size: number } | null>(null);\n  const [announcement, setAnnouncement] = useState(\"\");\n  const frame = useRef<number | null>(null);\n  const started = useRef(0);\n  const holding = useRef(false);\n  const finished = useRef(false);\n\n  useEffect(\n    () => () => {\n      if (frame.current != null) cancelAnimationFrame(frame.current);\n    },\n    [],\n  );\n\n  function tick(now: number) {\n    if (!holding.current) return;\n    const next = Math.min(1, (now - started.current) / duration);\n    setProgress(next);\n    if (next >= 1) {\n      holding.current = false;\n      finished.current = true;\n      setPhase(\"done\");\n      setAnnouncement(doneLabel);\n      onConfirm();\n      return;\n    }\n    frame.current = requestAnimationFrame(tick);\n  }\n\n  function begin(x: number, width: number) {\n    if (finished.current || holding.current) return;\n    holding.current = true;\n    started.current = performance.now();\n    setOrigin({ x, size: width * 2.4 });\n    setPhase(\"holding\");\n    setAnnouncement(\"\");\n    frame.current = requestAnimationFrame(tick);\n  }\n\n  function release() {\n    window.removeEventListener(\"pointerup\", release);\n    window.removeEventListener(\"pointercancel\", release);\n    if (!holding.current || finished.current) return;\n    holding.current = false;\n    if (frame.current != null) cancelAnimationFrame(frame.current);\n    setPhase(\"draining\");\n    setProgress(0);\n    setAnnouncement(\"Released early. Nothing happened.\");\n  }\n\n  function onPointerDown(event: PointerEvent<HTMLButtonElement>) {\n    if (event.button !== 0) return;\n    const rect = event.currentTarget.getBoundingClientRect();\n    try {\n      event.currentTarget.setPointerCapture(event.pointerId);\n    } catch {\n      // A stale pointer cannot be captured; the window listener still ends the hold.\n    }\n    window.addEventListener(\"pointerup\", release);\n    window.addEventListener(\"pointercancel\", release);\n    begin(event.clientX - rect.left, rect.width);\n  }\n\n  function onKeyDown(event: KeyboardEvent<HTMLButtonElement>) {\n    if (event.key !== \" \" && event.key !== \"Enter\") return;\n    event.preventDefault();\n    if (event.repeat) return;\n    const width = event.currentTarget.offsetWidth;\n    begin(width / 2, width);\n  }\n\n  const done = phase === \"done\";\n  const remaining = Math.max(0, ((1 - progress) * duration) / 1000).toFixed(1);\n  const lit = done ? 4 : Math.floor(progress * 4);\n\n  return (\n    <button\n      type=\"button\"\n      data-slot=\"hold-to-confirm\"\n      data-phase={phase}\n      disabled={done}\n      aria-label={done ? doneLabel : label}\n      aria-describedby={hint}\n      onPointerDown={onPointerDown}\n      onPointerUp={release}\n      onPointerCancel={release}\n      onContextMenu={(event) => event.preventDefault()}\n      onKeyDown={onKeyDown}\n      onKeyUp={(event) => {\n        if (event.key === \" \" || event.key === \"Enter\") release();\n      }}\n      onTransitionEnd={() => {\n        if (phase === \"draining\") setPhase(\"idle\");\n      }}\n      className={cn(\n        \"relative isolate inline-flex h-11 min-w-40 touch-none items-center justify-center gap-2 overflow-hidden rounded-touch bg-danger-fill px-[18px] text-[15px] font-medium text-danger-fill-foreground outline-none select-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring\",\n        className,\n      )}\n    >\n      {!reduced && origin && (\n        <span\n          aria-hidden=\"true\"\n          data-slot=\"hold-fill\"\n          style={{\n            left: origin.x,\n            width: origin.size,\n            height: origin.size,\n            marginLeft: -origin.size / 2,\n            marginTop: -origin.size / 2,\n            transform: `scale(${done ? 1 : progress})`,\n          }}\n        />\n      )}\n      <span data-slot=\"action-labels\" className=\"relative z-10\" aria-hidden=\"true\">\n        <span data-slot=\"action-label\" data-active={!done && phase !== \"holding\"}>\n          {children ?? label}\n        </span>\n        <span data-slot=\"action-label\" data-active={phase === \"holding\"}>\n          Keep holding\n          <span data-slot=\"hold-countdown\">{remaining}s</span>\n        </span>\n        <span data-slot=\"action-label\" data-active={done}>\n          {doneLabel}\n        </span>\n      </span>\n      {reduced && (\n        <span aria-hidden=\"true\" data-slot=\"hold-notches\">\n          {[0, 1, 2, 3].map((index) => (\n            <i key={index} data-on={index < lit} />\n          ))}\n        </span>\n      )}\n      <span id={hint} className=\"sr-only\">\n        Press and hold for {(duration / 1000).toFixed(1)} seconds, or hold Space. Releasing early cancels it.\n      </span>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {announcement}\n      </span>\n    </button>\n  );\n}\n"},{"path":"ui/lib/cn.ts","type":"registry:ui","target":"components/ui/lib/cn.ts","content":"import { clsx, type ClassValue } from \"clsx\";\nimport { extendTailwindMerge } from \"tailwind-merge\";\n\nconst twMerge = extendTailwindMerge({\n  extend: { theme: { radius: [\"control\", \"surface\", \"overlay\"], shadow: [\"overlay\"] } },\n});\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n"},{"path":"ui/shared/action-label.css","type":"registry:file","target":"components/ui/shared/action-label.css","content":"/* Shared by action-button.tsx, copy-button.tsx, hold-to-confirm.tsx; each imports it. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  /* Action button: every phase's label shares one grid cell, so the button\n     is always as wide as its widest label and never shifts. */\n  [data-slot=\"action-labels\"] {\n    display: inline-grid;\n    justify-items: center;\n  }\n  [data-slot=\"action-label\"] {\n    grid-area: 1 / 1;\n    display: inline-flex;\n    align-items: center;\n    gap: 8px;\n    visibility: hidden;\n    opacity: 0;\n    transition: opacity var(--duration-fast) var(--ease-out), visibility 0s linear var(--duration-fast);\n  }\n  [data-slot=\"action-label\"][data-active=\"true\"] {\n    visibility: visible;\n    opacity: 1;\n    transition: opacity var(--duration-fast) var(--ease-out), visibility 0s;\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"action-label\"],\n    [data-slot=\"action-label\"][data-active=\"true\"] {\n      transition: none !important;\n    }\n  }\n}\n"},{"path":"ui/hold-to-confirm.css","type":"registry:file","target":"components/ui/hold-to-confirm.css","content":"/* hold-to-confirm.tsx's styles; it imports this file. Edit freely. */\n@layer theme, base, components, utilities;\n\n@layer components {\n  /* Hold to confirm: the fill soaks out from the press point at the hold's\n     own pace (driven per frame), then drains back on the settle curve. */\n  [data-slot=\"hold-to-confirm\"] {\n    box-shadow: var(--shadow-danger);\n    transition: translate var(--duration-normal) var(--ease-settle);\n  }\n  [data-slot=\"hold-to-confirm\"][data-phase=\"holding\"] {\n    translate: 0 var(--motion-press-travel);\n    box-shadow: var(--shadow-danger-press);\n    transition-duration: 90ms;\n  }\n  [data-slot=\"hold-to-confirm\"]:disabled {\n    cursor: default;\n  }\n  [data-slot=\"hold-fill\"] {\n    position: absolute;\n    top: 50%;\n    z-index: 0;\n    border-radius: 50%;\n    background: rgb(0 0 0 / 34%);\n    pointer-events: none;\n  }\n  [data-slot=\"hold-to-confirm\"][data-phase=\"draining\"] [data-slot=\"hold-fill\"] {\n    transition: transform 380ms var(--ease-settle);\n  }\n  [data-slot=\"hold-countdown\"] {\n    font-variant-numeric: tabular-nums;\n    font-size: 12px;\n    font-variant-numeric: tabular-nums;\n    opacity: 0.8;\n  }\n  [data-slot=\"hold-notches\"] {\n    position: absolute;\n    inset-inline: 14px;\n    bottom: 6px;\n    z-index: 10;\n    display: grid;\n    grid-template-columns: repeat(4, 1fr);\n    gap: 4px;\n  }\n  [data-slot=\"hold-notches\"] i {\n    height: 3px;\n    border-radius: 2px;\n    background: rgb(255 255 255 / 28%);\n  }\n  [data-slot=\"hold-notches\"] i[data-on=\"true\"] {\n    background: #ffffff;\n  }\n\n  @media (prefers-reduced-motion: reduce) {\n    [data-slot=\"hold-to-confirm\"],\n    [data-slot=\"hold-fill\"] {\n      transition: none !important;\n    }\n  }\n}\n"}],"categories":["actions"],"meta":{"tier":"free","license":"MIT","family":"actions","docs":"/docs/components/hold-to-confirm"}}