{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hold-to-confirm",
  "title": "Hold to Confirm",
  "description": "Guard destructive actions behind a press-and-hold fill with rising sound, haptic ticks, and a checkmark.",
  "dependencies": [
    "lucide-react",
    "motion",
    "web-haptics"
  ],
  "files": [
    {
      "path": "src/registry/components/hold-to-confirm/hold-to-confirm.tsx",
      "content": "\"use client\";\n\nimport { Trash2Icon } from \"lucide-react\";\nimport {\n\tAnimatePresence,\n\ttype HTMLMotionProps,\n\tmotion,\n\tuseReducedMotion,\n\tuseTransform,\n} from \"motion/react\";\nimport { type ReactNode, useId } from \"react\";\nimport {\n\ttype UseHoldToConfirmOptions,\n\tuseHoldToConfirm,\n} from \"@/hooks/use-hold-to-confirm\";\nimport { cn } from \"@/lib/utils\";\n\nconst labelTransition = {\n\ttype: \"spring\",\n\tduration: 0.3,\n\tbounce: 0,\n} as const;\n\nfunction HoldToConfirmCheck() {\n\treturn (\n\t\t<svg\n\t\t\tviewBox=\"0 0 24 24\"\n\t\t\tfill=\"none\"\n\t\t\tstroke=\"currentColor\"\n\t\t\tstrokeWidth={2.5}\n\t\t\tstrokeLinecap=\"round\"\n\t\t\tstrokeLinejoin=\"round\"\n\t\t\taria-hidden\n\t\t\tdata-slot=\"confirmed-icon\"\n\t\t>\n\t\t\t<motion.path\n\t\t\t\td=\"M5 12.5l4.5 4.5L19 7.5\"\n\t\t\t\tinitial={{ pathLength: 0 }}\n\t\t\t\tanimate={{ pathLength: 1 }}\n\t\t\t\ttransition={{ duration: 0.35, delay: 0.08, ease: [0.23, 1, 0.32, 1] }}\n\t\t\t/>\n\t\t</svg>\n\t);\n}\n\ntype HeldHandler =\n\t| \"onPointerDown\"\n\t| \"onPointerUp\"\n\t| \"onPointerCancel\"\n\t| \"onLostPointerCapture\"\n\t| \"onKeyDown\"\n\t| \"onKeyUp\"\n\t| \"onBlur\"\n\t| \"onContextMenu\";\n\nexport type HoldToConfirmProps = Omit<\n\tHTMLMotionProps<\"button\">,\n\t\"children\" | HeldHandler\n> &\n\tUseHoldToConfirmOptions & {\n\t\t/** Label shown while idle and under the fill. */\n\t\tchildren?: ReactNode;\n\t\t/** Icon shown before the label. */\n\t\ticon?: ReactNode;\n\t\t/** Label shown after the hold completes. */\n\t\tconfirmedLabel?: ReactNode;\n\t\t/** Icon shown after the hold completes. Defaults to an animated checkmark. */\n\t\tconfirmedIcon?: ReactNode;\n\t\t/** Description announced to assistive technology. */\n\t\tinstructions?: string;\n\t\t/** Class name for the fill layer, e.g. to change its color. */\n\t\tfillClassName?: string;\n\t};\n\nexport function HoldToConfirm({\n\tclassName,\n\tchildren = \"Hold to delete\",\n\ticon = <Trash2Icon data-slot=\"idle-icon\" />,\n\tconfirmedLabel = \"Deleted\",\n\tconfirmedIcon,\n\tinstructions = \"Press and hold to confirm\",\n\tfillClassName,\n\tholdDuration,\n\tresetDelay,\n\tonConfirm,\n\tonCancel,\n\tsound,\n\thaptics,\n\tdisabled,\n\t...props\n}: HoldToConfirmProps) {\n\tconst { state, progress, fillOpacity, handlers } = useHoldToConfirm({\n\t\tholdDuration,\n\t\tresetDelay,\n\t\tonConfirm,\n\t\tonCancel,\n\t\tsound,\n\t\thaptics,\n\t\tdisabled,\n\t});\n\tconst shouldReduceMotion = useReducedMotion();\n\tconst descriptionId = useId();\n\tconst clipPath = useTransform(progress, (value) => {\n\t\tconst clamped = Math.min(Math.max(value, 0), 1);\n\t\treturn `inset(0 ${(1 - clamped) * 100}% 0 0)`;\n\t});\n\tconst isHolding = state === \"holding\";\n\tconst isConfirmed = state === \"confirmed\";\n\n\treturn (\n\t\t<>\n\t\t\t<motion.button\n\t\t\t\ttype=\"button\"\n\t\t\t\tdata-slot=\"hold-to-confirm\"\n\t\t\t\tdata-state={state}\n\t\t\t\tdisabled={disabled}\n\t\t\t\taria-describedby={descriptionId}\n\t\t\t\taria-disabled={isConfirmed || undefined}\n\t\t\t\tanimate={{ scale: isHolding && !shouldReduceMotion ? 0.97 : 1 }}\n\t\t\t\ttransition={{\n\t\t\t\t\ttype: \"spring\",\n\t\t\t\t\tvisualDuration: isHolding ? 0.2 : 0.4,\n\t\t\t\t\tbounce: isHolding ? 0 : 0.45,\n\t\t\t\t}}\n\t\t\t\tclassName={cn(\n\t\t\t\t\t\"relative inline-flex h-10 shrink-0 cursor-pointer touch-manipulation select-none items-center justify-center whitespace-nowrap rounded-full border border-border bg-background px-5 font-medium text-foreground text-sm shadow-xs outline-none transition-[background-color,box-shadow] [-webkit-touch-callout:none] hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 dark:border-input dark:bg-input/30 dark:hover:bg-input/50 [&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0\",\n\t\t\t\t\tclassName,\n\t\t\t\t)}\n\t\t\t\t{...handlers}\n\t\t\t\t{...props}\n\t\t\t>\n\t\t\t\t<span className=\"inline-flex items-center gap-2\">\n\t\t\t\t\t{icon}\n\t\t\t\t\t{children}\n\t\t\t\t</span>\n\n\t\t\t\t<motion.span\n\t\t\t\t\taria-hidden\n\t\t\t\t\tdata-slot=\"hold-to-confirm-fill\"\n\t\t\t\t\tstyle={{ clipPath, opacity: fillOpacity }}\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"pointer-events-none absolute -inset-px flex items-center justify-center rounded-[inherit] border border-transparent bg-destructive px-5 text-white dark:bg-red-600\",\n\t\t\t\t\t\tfillClassName,\n\t\t\t\t\t)}\n\t\t\t\t>\n\t\t\t\t\t<AnimatePresence mode=\"popLayout\" initial={false}>\n\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\tkey={isConfirmed ? \"confirmed\" : \"idle\"}\n\t\t\t\t\t\t\tinitial={{ opacity: 0, scale: 0.9, filter: \"blur(4px)\" }}\n\t\t\t\t\t\t\tanimate={{ opacity: 1, scale: 1, filter: \"blur(0px)\" }}\n\t\t\t\t\t\t\texit={{ opacity: 0, scale: 0.9, filter: \"blur(4px)\" }}\n\t\t\t\t\t\t\ttransition={labelTransition}\n\t\t\t\t\t\t\tclassName=\"inline-flex items-center gap-2\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{isConfirmed ? (confirmedIcon ?? <HoldToConfirmCheck />) : icon}\n\t\t\t\t\t\t\t{isConfirmed ? confirmedLabel : children}\n\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t</AnimatePresence>\n\t\t\t\t</motion.span>\n\t\t\t</motion.button>\n\n\t\t\t<span id={descriptionId} className=\"sr-only\">\n\t\t\t\t{instructions}\n\t\t\t</span>\n\t\t\t<span aria-live=\"polite\" className=\"sr-only\">\n\t\t\t\t{isConfirmed ? confirmedLabel : null}\n\t\t\t</span>\n\t\t</>\n\t);\n}\n\nexport default HoldToConfirm;\n",
      "type": "registry:component",
      "target": "@components/hold-to-confirm.tsx"
    },
    {
      "path": "src/hooks/use-hold-to-confirm.ts",
      "content": "\"use client\";\n\nimport {\n\ttype AnimationPlaybackControls,\n\tanimate,\n\tuseMotionValue,\n\tuseMotionValueEvent,\n\tuseReducedMotion,\n} from \"motion/react\";\nimport type { KeyboardEvent, PointerEvent } from \"react\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useWebHaptics } from \"web-haptics/react\";\n\nexport type HoldState = \"idle\" | \"holding\" | \"confirmed\";\n\nexport type UseHoldToConfirmOptions = {\n\t/** How long the button must be held before confirming, in milliseconds. */\n\tholdDuration?: number;\n\t/** Delay before returning to idle after confirming. Pass `false` to stay confirmed. */\n\tresetDelay?: number | false;\n\t/** Called once the hold completes. */\n\tonConfirm?: () => void;\n\t/** Called with the reached progress (0-1) when the hold is released early. */\n\tonCancel?: (progress: number) => void;\n\t/** Play a rising tone while holding and a chime on confirm. */\n\tsound?: boolean;\n\t/** Tick haptics while holding and a success pattern on confirm. */\n\thaptics?: boolean;\n\t/** Ignore all hold input. */\n\tdisabled?: boolean;\n};\n\nconst TICK_COUNT = 10;\n// The riser climbs two octaves from G3 to G5, then resolves up to a C6 chime.\nconst MIN_FREQUENCY = 196;\nconst MAX_FREQUENCY = 784;\nconst CHIME_FREQUENCIES = [1046.5, 1568];\n\nlet audioContext: AudioContext | null = null;\n\nfunction getAudioContext() {\n\tif (typeof window === \"undefined\" || !(\"AudioContext\" in window)) {\n\t\treturn null;\n\t}\n\n\taudioContext ??= new AudioContext();\n\tif (audioContext.state === \"suspended\") void audioContext.resume();\n\treturn audioContext;\n}\n\nfunction frequencyAt(progress: number) {\n\treturn MIN_FREQUENCY * (MAX_FREQUENCY / MIN_FREQUENCY) ** progress;\n}\n\nfunction holdParam(param: AudioParam, time: number) {\n\tif (typeof param.cancelAndHoldAtTime === \"function\") {\n\t\tparam.cancelAndHoldAtTime(time);\n\t\treturn;\n\t}\n\n\tconst value = param.value;\n\tparam.cancelScheduledValues(time);\n\tparam.setValueAtTime(value, time);\n}\n\ntype Riser = {\n\trelease: () => void;\n\tfinish: () => void;\n};\n\nfunction startRiser(progress: number, remaining: number): Riser | null {\n\tconst ctx = getAudioContext();\n\tif (!ctx) return null;\n\n\tconst now = ctx.currentTime;\n\tconst oscillator = ctx.createOscillator();\n\tconst filter = ctx.createBiquadFilter();\n\tconst gain = ctx.createGain();\n\n\toscillator.type = \"triangle\";\n\toscillator.frequency.setValueAtTime(frequencyAt(progress), now);\n\toscillator.frequency.exponentialRampToValueAtTime(\n\t\tMAX_FREQUENCY,\n\t\tnow + Math.max(remaining, 0.01),\n\t);\n\n\tfilter.type = \"lowpass\";\n\tfilter.frequency.value = 2400;\n\n\tgain.gain.setValueAtTime(0, now);\n\tgain.gain.linearRampToValueAtTime(0.05, now + 0.05);\n\n\toscillator.connect(filter).connect(gain).connect(ctx.destination);\n\toscillator.start(now);\n\n\tlet stopped = false;\n\tconst stop = (fade: number, drop: boolean) => {\n\t\tif (stopped) return;\n\t\tstopped = true;\n\n\t\tconst time = ctx.currentTime;\n\t\tholdParam(oscillator.frequency, time);\n\t\tholdParam(gain.gain, time);\n\t\tif (drop) {\n\t\t\toscillator.frequency.exponentialRampToValueAtTime(\n\t\t\t\tMIN_FREQUENCY,\n\t\t\t\ttime + fade,\n\t\t\t);\n\t\t}\n\t\tgain.gain.linearRampToValueAtTime(0, time + fade);\n\t\toscillator.stop(time + fade + 0.02);\n\t};\n\n\treturn {\n\t\trelease: () => stop(0.18, true),\n\t\tfinish: () => stop(0.04, false),\n\t};\n}\n\nfunction playChime() {\n\tconst ctx = getAudioContext();\n\tif (!ctx) return;\n\n\tconst now = ctx.currentTime;\n\tCHIME_FREQUENCIES.forEach((frequency, index) => {\n\t\tconst start = now + index * 0.08;\n\t\tconst oscillator = ctx.createOscillator();\n\t\tconst gain = ctx.createGain();\n\n\t\toscillator.type = \"sine\";\n\t\toscillator.frequency.value = frequency;\n\t\tgain.gain.setValueAtTime(0, start);\n\t\tgain.gain.linearRampToValueAtTime(0.08, start + 0.005);\n\t\tgain.gain.exponentialRampToValueAtTime(0.0001, start + 0.6);\n\n\t\toscillator.connect(gain).connect(ctx.destination);\n\t\toscillator.start(start);\n\t\toscillator.stop(start + 0.65);\n\t});\n}\n\nfunction isHoldKey(key: string) {\n\treturn key === \" \" || key === \"Enter\";\n}\n\nexport function useHoldToConfirm({\n\tholdDuration = 1500,\n\tresetDelay = 2000,\n\tonConfirm,\n\tonCancel,\n\tsound = true,\n\thaptics = true,\n\tdisabled = false,\n}: UseHoldToConfirmOptions = {}) {\n\tconst [state, setState] = useState<HoldState>(\"idle\");\n\tconst stateRef = useRef<HoldState>(\"idle\");\n\tconst progress = useMotionValue(0);\n\tconst fillOpacity = useMotionValue(1);\n\tconst animationRef = useRef<AnimationPlaybackControls | null>(null);\n\tconst riserRef = useRef<Riser | null>(null);\n\tconst resetTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n\tconst lastTickRef = useRef(0);\n\tconst callbacksRef = useRef({ onConfirm, onCancel });\n\tconst { trigger: haptic } = useWebHaptics();\n\n\t// Sound follows the same reduced-motion opt-out as the other TentUI sounds.\n\tconst shouldReduceMotion = useReducedMotion();\n\tconst soundEnabled = sound && !shouldReduceMotion;\n\n\tuseEffect(() => {\n\t\tcallbacksRef.current = { onConfirm, onCancel };\n\t}, [onConfirm, onCancel]);\n\n\tconst updateState = useCallback((next: HoldState) => {\n\t\tstateRef.current = next;\n\t\tsetState(next);\n\t}, []);\n\n\tconst clearResetTimeout = useCallback(() => {\n\t\tif (resetTimeoutRef.current) {\n\t\t\tclearTimeout(resetTimeoutRef.current);\n\t\t\tresetTimeoutRef.current = null;\n\t\t}\n\t}, []);\n\n\tconst reset = useCallback(() => {\n\t\tclearResetTimeout();\n\t\tanimationRef.current?.stop();\n\n\t\tif (stateRef.current !== \"confirmed\") {\n\t\t\tprogress.set(0);\n\t\t\tupdateState(\"idle\");\n\t\t\treturn;\n\t\t}\n\n\t\tanimationRef.current = animate(fillOpacity, 0, {\n\t\t\tduration: 0.2,\n\t\t\tease: [0.23, 1, 0.32, 1],\n\t\t\tonComplete: () => {\n\t\t\t\tprogress.set(0);\n\t\t\t\tfillOpacity.set(1);\n\t\t\t\tupdateState(\"idle\");\n\t\t\t},\n\t\t});\n\t}, [clearResetTimeout, fillOpacity, progress, updateState]);\n\n\tconst confirm = useCallback(() => {\n\t\tupdateState(\"confirmed\");\n\t\tprogress.set(1);\n\n\t\triserRef.current?.finish();\n\t\triserRef.current = null;\n\t\tif (soundEnabled) playChime();\n\t\tif (haptics) void haptic(\"success\");\n\n\t\tcallbacksRef.current.onConfirm?.();\n\n\t\tif (resetDelay !== false) {\n\t\t\tresetTimeoutRef.current = setTimeout(reset, resetDelay);\n\t\t}\n\t}, [haptic, haptics, progress, reset, resetDelay, soundEnabled, updateState]);\n\n\tconst start = useCallback(() => {\n\t\tif (disabled || stateRef.current !== \"idle\") return;\n\n\t\tupdateState(\"holding\");\n\t\tconst from = Math.min(Math.max(progress.get(), 0), 1);\n\t\tconst remaining = (holdDuration / 1000) * (1 - from);\n\t\tlastTickRef.current = Math.floor(from * TICK_COUNT);\n\n\t\tanimationRef.current?.stop();\n\t\tanimationRef.current = animate(progress, 1, {\n\t\t\tduration: remaining,\n\t\t\tease: \"linear\",\n\t\t\tonComplete: confirm,\n\t\t});\n\n\t\tif (soundEnabled) riserRef.current = startRiser(from, remaining);\n\t\tif (haptics) void haptic([{ duration: 10, intensity: 0.4 }]);\n\t}, [\n\t\tconfirm,\n\t\tdisabled,\n\t\thaptic,\n\t\thaptics,\n\t\tholdDuration,\n\t\tprogress,\n\t\tsoundEnabled,\n\t\tupdateState,\n\t]);\n\n\tconst cancel = useCallback(() => {\n\t\tif (stateRef.current !== \"holding\") return;\n\n\t\tupdateState(\"idle\");\n\t\tconst reached = progress.get();\n\n\t\tanimationRef.current?.stop();\n\t\tanimationRef.current = animate(\n\t\t\tprogress,\n\t\t\t0,\n\t\t\tshouldReduceMotion\n\t\t\t\t? { duration: 0.2, ease: [0.23, 1, 0.32, 1] }\n\t\t\t\t: { type: \"spring\", visualDuration: 0.35, bounce: 0.2 },\n\t\t);\n\n\t\triserRef.current?.release();\n\t\triserRef.current = null;\n\n\t\tcallbacksRef.current.onCancel?.(reached);\n\t}, [progress, shouldReduceMotion, updateState]);\n\n\t// Tick a little harder at each step so the phone feels the hold building.\n\tuseMotionValueEvent(progress, \"change\", (value) => {\n\t\tif (!haptics || stateRef.current !== \"holding\") return;\n\n\t\tconst tick = Math.floor(value * TICK_COUNT);\n\t\tif (tick > lastTickRef.current && tick < TICK_COUNT) {\n\t\t\tlastTickRef.current = tick;\n\t\t\tvoid haptic([{ duration: 8, intensity: 0.3 + 0.6 * value }]);\n\t\t}\n\t});\n\n\tuseEffect(() => {\n\t\tif (disabled) cancel();\n\t}, [disabled, cancel]);\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tclearResetTimeout();\n\t\t\tanimationRef.current?.stop();\n\t\t\triserRef.current?.finish();\n\t\t};\n\t}, [clearResetTimeout]);\n\n\tconst handlers = {\n\t\tonPointerDown: (event: PointerEvent<HTMLElement>) => {\n\t\t\tif (event.button !== 0) return;\n\t\t\tevent.currentTarget.setPointerCapture(event.pointerId);\n\t\t\tstart();\n\t\t},\n\t\tonPointerUp: cancel,\n\t\tonPointerCancel: cancel,\n\t\tonLostPointerCapture: cancel,\n\t\tonKeyDown: (event: KeyboardEvent<HTMLElement>) => {\n\t\t\tif (!isHoldKey(event.key)) return;\n\t\t\tevent.preventDefault();\n\t\t\tif (!event.repeat) start();\n\t\t},\n\t\tonKeyUp: (event: KeyboardEvent<HTMLElement>) => {\n\t\t\tif (isHoldKey(event.key)) cancel();\n\t\t},\n\t\tonBlur: cancel,\n\t\tonContextMenu: (event: { preventDefault: () => void }) => {\n\t\t\tevent.preventDefault();\n\t\t},\n\t};\n\n\treturn { state, progress, fillOpacity, handlers, reset } as const;\n}\n",
      "type": "registry:hook",
      "target": "@hooks/use-hold-to-confirm.ts"
    }
  ],
  "meta": {
    "createdAt": "2026-10-22",
    "image": "https://cdn.tentui.com/images/components/hold-to-confirm",
    "video": "https://cdn.tentui.com/videos/components/hold-to-confirm"
  },
  "docs": "https://tentui.com/components/hold-to-confirm",
  "categories": [
    "buttons"
  ],
  "type": "registry:component"
}