{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hold-to-confirm",
  "title": "Hold to confirm",
  "description": "Press-and-hold button for destructive actions: a fill sweeps across with the label flipping color at its edge, early release rewinds it, works with Space and Enter.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json",
    "https://goodui.dev/r/sounds.json"
  ],
  "files": [
    {
      "path": "registry/components/buttons/hold-to-confirm.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { animate, motion, useAnimate, useMotionValue, useReducedMotion, useTransform, type AnimationPlaybackControls } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport type { SoundName } from \"@/lib/goodui-sounds\"\nimport { GooduiTokens, spring } from \"@/lib/goodui-motion\"\n\n// The destructive text and fill are darkened just enough to keep 4.5:1 contrast in light and dark.\nconst VARIANTS = {\n  destructive: {\n    base: \"border border-destructive/30 bg-destructive/10 text-[color-mix(in_oklab,var(--destructive)_85%,black)] dark:bg-destructive/20 dark:text-destructive\",\n    fill: \"bg-destructive text-white dark:bg-[color-mix(in_oklab,var(--destructive)_75%,black)]\",\n  },\n  default: {\n    base: \"border bg-muted text-foreground\",\n    fill: \"bg-primary text-primary-foreground\",\n  },\n  outline: {\n    base: \"border bg-background text-foreground shadow-xs dark:bg-input/30\",\n    fill: \"bg-foreground text-background\",\n  },\n} as const\n\nconst SIZES = {\n  sm: \"h-8 rounded-md px-3 text-xs gap-1.5 [&_svg]:size-3.5\",\n  md: \"h-10 rounded-lg px-4 text-sm gap-2 [&_svg]:size-4\",\n  lg: \"h-12 rounded-xl px-6 text-base gap-2 [&_svg]:size-5\",\n} as const\n\nexport type HoldToConfirmLabels = {\n  /** sr-only instructions linked with aria-describedby; gets the hold time in seconds. */\n  instructions: (seconds: number) => string\n  /** Announced after confirming when confirmedLabel is not a string. */\n  confirmed: string\n  /** Default visible text after confirming; the confirmedLabel prop overrides it. */\n  confirmedLabel: string\n  /** Default hint after a quick tap; the tapHint prop overrides it. */\n  tapHint: string\n}\nconst DEFAULT_LABELS: HoldToConfirmLabels = {\n  instructions: (seconds) => `Press and hold for ${seconds} seconds to confirm.`,\n  confirmed: \"Confirmed\",\n  confirmedLabel: \"Done\",\n  tapHint: \"Press and hold\",\n}\n\nexport type HoldToConfirmProps = Omit<\n  React.ComponentProps<\"button\">,\n  \"onAnimationStart\" | \"onDrag\" | \"onDragStart\" | \"onDragEnd\" | \"onClick\"\n> & {\n  /** Milliseconds the button must be held. */\n  duration?: number\n  /** Called once when the hold completes. There is no onClick: a tap only shows the hint. */\n  onConfirm?: () => void\n  /** \"destructive\" | \"default\" | \"outline\": resting look and fill color (red, primary, foreground). */\n  variant?: keyof typeof VARIANTS\n  /** \"sm\" | \"md\" | \"lg\": height, padding and corner radius. */\n  size?: keyof typeof SIZES\n  /** Label while holding. */\n  holdingLabel?: React.ReactNode\n  /** Label after confirming. */\n  confirmedLabel?: React.ReactNode\n  /** Shown briefly when the button is only tapped. */\n  tapHint?: React.ReactNode\n  /** Milliseconds before returning to idle after confirming; 0 stays confirmed. */\n  resetAfter?: number\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<HoldToConfirmLabels>\n  /** Plays a \"tick\" when the hold starts and \"confirm\" when it completes, when the visitor has sounds on (lib/goodui-sounds); a sound name replaces the confirm. Off by default. */\n  sound?: boolean | SoundName\n}\n\n// Optional UI sound: the sound module is loaded only when `sound` is on, so silent apps pay nothing.\nfunction useCue(sound: boolean | SoundName | undefined) {\n  React.useEffect(() => {\n    if (sound) void import(\"@/lib/goodui-sounds\")\n  }, [sound])\n  return React.useCallback(\n    (fallback: SoundName, o?: { rate?: number; exact?: boolean }) => {\n      if (!sound) return\n      void import(\"@/lib/goodui-sounds\").then((m) => m.play(sound === true || o?.exact ? fallback : sound, { rate: o?.rate }))\n    },\n    [sound],\n  )\n}\n\n/**\n * Press-and-hold button for destructive actions: a fill sweeps across while held, releasing early rewinds it.\n * Works with pointer, touch and keyboard (hold Space or Enter).\n */\nexport function HoldToConfirm({\n  duration = 1500,\n  onConfirm,\n  variant = \"destructive\",\n  size = \"md\",\n  holdingLabel,\n  confirmedLabel: confirmedLabelProp,\n  tapHint: tapHintProp,\n  resetAfter = 2000,\n  labels,\n  sound,\n  disabled,\n  className,\n  children,\n  onPointerDown,\n  onPointerUp,\n  onPointerLeave,\n  onPointerCancel,\n  onKeyDown,\n  onKeyUp,\n  onBlur,\n  onContextMenu,\n  ...props\n}: HoldToConfirmProps) {\n  const l = useLabels(\"hold-to-confirm\", DEFAULT_LABELS, labels)\n  const confirmedLabel = confirmedLabelProp ?? l.confirmedLabel\n  const tapHint = tapHintProp ?? l.tapHint\n  const reduce = useReducedMotion() ?? false\n  const cue = useCue(sound)\n  const progress = useMotionValue(0)\n  const [scope, animateBtn] = useAnimate<HTMLButtonElement>()\n  // The fill sweeps from the inline start: left to right, or right to left under dir=\"rtl\".\n  const [rtl, setRtl] = React.useState(false)\n  React.useLayoutEffect(() => {\n    if (scope.current) setRtl(getComputedStyle(scope.current).direction === \"rtl\")\n  }, [scope])\n  const clip = useTransform(progress, (p) => (rtl ? `inset(0 0 0 ${(1 - p) * 100}%)` : `inset(0 ${(1 - p) * 100}% 0 0)`))\n  const [phase, setPhaseState] = React.useState<\"idle\" | \"holding\" | \"confirmed\">(\"idle\")\n  const [hint, setHint] = React.useState(false)\n  const run = React.useRef<AnimationPlaybackControls | null>(null)\n  // Pointer events can arrive before a re-render, so the current phase also lives in a ref.\n  const phaseRef = React.useRef(phase)\n  const setPhase = (p: \"idle\" | \"holding\" | \"confirmed\") => {\n    phaseRef.current = p\n    setPhaseState(p)\n  }\n  const timers = React.useRef<number[]>([])\n  const uid = React.useId()\n  const v = VARIANTS[variant]\n\n  React.useEffect(() => {\n    const list = timers.current\n    return () => {\n      list.forEach((t) => window.clearTimeout(t))\n      run.current?.stop()\n    }\n  }, [])\n\n  const confirm = () => {\n    setPhase(\"confirmed\")\n    cue(\"confirm\")\n    if (typeof navigator !== \"undefined\" && \"vibrate\" in navigator) navigator.vibrate?.(25)\n    if (!reduce && scope.current) animateBtn(scope.current, { scale: [0.97, 1.04, 1] }, { duration: 0.35, ease: \"easeOut\" })\n    onConfirm?.()\n    if (resetAfter > 0) {\n      timers.current.push(\n        window.setTimeout(() => {\n          setPhase(\"idle\")\n          run.current = animate(progress, 0, { duration: reduce ? 0 : 0.4, ease: \"easeOut\" })\n        }, resetAfter),\n      )\n    }\n  }\n\n  const start = () => {\n    if (disabled || phaseRef.current !== \"idle\") return\n    setPhase(\"holding\")\n    setHint(false)\n    cue(\"tick\", { exact: true })\n    if (!reduce && scope.current) animateBtn(scope.current, { scale: 0.97 }, spring.ui)\n    run.current?.stop()\n    const left = 1 - progress.get()\n    run.current = animate(progress, 1, { duration: (duration * left) / 1000, ease: \"linear\", onComplete: confirm })\n  }\n\n  const cancel = () => {\n    if (phaseRef.current !== \"holding\") return\n    const p = progress.get()\n    run.current?.stop()\n    setPhase(\"idle\")\n    if (scope.current) animateBtn(scope.current, { scale: 1 }, reduce ? { duration: 0 } : spring.play)\n    run.current = animate(progress, 0, reduce ? { duration: 0 } : spring.morph)\n    // A quick tap teaches the gesture.\n    if (p < 0.2) {\n      setHint(true)\n      if (!reduce && scope.current) animateBtn(scope.current, { x: [0, -4, 4, -2, 2, 0] }, { duration: 0.35 })\n      timers.current.push(window.setTimeout(() => setHint(false), 1400))\n    }\n  }\n\n  const label = phase === \"confirmed\" ? confirmedLabel : phase === \"holding\" && holdingLabel ? holdingLabel : hint ? tapHint : children\n\n  return (\n    <>\n      <GooduiTokens />\n      <motion.button\n        ref={scope}\n        type=\"button\"\n        data-phase={phase}\n        disabled={disabled}\n        aria-describedby={`${uid}-how`}\n        onPointerDown={(e) => {\n          if (e.button === 0) start()\n          onPointerDown?.(e)\n        }}\n        onPointerUp={(e) => {\n          cancel()\n          onPointerUp?.(e)\n        }}\n        onPointerLeave={(e) => {\n          cancel()\n          onPointerLeave?.(e)\n        }}\n        onPointerCancel={(e) => {\n          cancel()\n          onPointerCancel?.(e)\n        }}\n        onKeyDown={(e) => {\n          if (e.key === \" \" || e.key === \"Enter\") {\n            e.preventDefault()\n            if (!e.repeat) start()\n          }\n          onKeyDown?.(e)\n        }}\n        onKeyUp={(e) => {\n          if (e.key === \" \" || e.key === \"Enter\") {\n            e.preventDefault()\n            cancel()\n          }\n          onKeyUp?.(e)\n        }}\n        onBlur={(e) => {\n          cancel()\n          onBlur?.(e)\n        }}\n        onContextMenu={(e) => {\n          e.preventDefault()\n          onContextMenu?.(e)\n        }}\n        className={cn(\n          \"pc-press relative inline-flex shrink-0 touch-manipulation items-center justify-center overflow-hidden font-medium whitespace-nowrap transition-[background-color,box-shadow,color] outline-none select-none [-webkit-touch-callout:none] focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 [&_svg]:shrink-0\",\n          v.base,\n          SIZES[size],\n          className,\n        )}\n        {...props}\n      >\n        <span className=\"inline-flex items-center gap-[inherit]\">{label}</span>\n        {/* The fill carries its own copy of the label, so text color flips exactly at the fill edge. */}\n        <motion.span\n          aria-hidden=\"true\"\n          className={cn(\"pointer-events-none absolute inset-0 flex items-center justify-center gap-[inherit]\", v.fill)}\n          style={{ clipPath: clip, WebkitClipPath: clip }}\n        >\n          <span className=\"inline-flex items-center gap-[inherit]\">{label}</span>\n        </motion.span>\n      </motion.button>\n      <span id={`${uid}-how`} className=\"sr-only\">\n        {l.instructions(Math.round(duration / 100) / 10)}\n      </span>\n      <span role=\"status\" aria-live=\"assertive\" className=\"sr-only\">\n        {phase === \"confirmed\" ? (typeof confirmedLabel === \"string\" ? confirmedLabel : l.confirmed) : \"\"}\n      </span>\n    </>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/hold-to-confirm.tsx"
    }
  ],
  "categories": [
    "components",
    "buttons"
  ],
  "type": "registry:component"
}