コンポーネント一覧へ戻る

これは何か
ゴミ箱のアイコンと Hold to delete の文字が並ぶ、白い pill 型の削除ボタンです。押し続けると赤い塗りが左から広がり、2 秒で満ちると Deleted に替わります。途中で離すと塗りはすぐ戻ります。Space / Enter の長押しでも動きます。押す時間とラベルを差し替えて、onConfirm に削除の処理をつなぎます。
TSX の全文です。アカウントは不要です。
hold-to-delete-button.tsx
"use client"; import * as React from "react";import { Check, Trash2 } from "lucide-react";import { cn } from "@/lib/utils"; export type HoldToDeleteButtonProps = Omit< React.ComponentPropsWithoutRef<"button">, "children" | "type" | "value" | "defaultValue"> & { /** Called once the hold completes. */ onConfirm?: () => void; /** How long the press must be held, in ms. */ holdDuration?: number; /** Return to the idle state this long after confirming, in ms. `null` keeps the confirmed state. */ resetAfter?: number | null; /** Label before and during the hold. */ label?: string; /** Label once confirmed. */ confirmedLabel?: string;}; type Phase = "idle" | "holding" | "confirmed"; /** Strong ease-out for the release and the label swap. */const EASE_OUT = "cubic-bezier(0.23, 1, 0.32, 1)"; /* * Slow where the user is deciding, fast where the system responds: * the red fill wipes in linearly over the hold (it is a progress bar, progress does not ease), * and snaps back in 200ms ease-out on release. * Content is drawn twice — once on white, once on red — and the red copy is revealed with clip-path, * so the text and icon change color exactly at the fill edge. */const CSS = `.hold-to-delete-button { transition: transform 160ms ${EASE_OUT}; -webkit-touch-callout: none; }.hold-to-delete-button[data-phase="holding"] { transform: scale(0.97); }.hold-to-delete-button-fill { clip-path: inset(0 100% 0 0); transition: clip-path 200ms ${EASE_OUT}; }.hold-to-delete-button[data-phase="holding"] .hold-to-delete-button-fill { clip-path: inset(0 0 0 0); transition: clip-path var(--hold-to-delete-duration) linear; }.hold-to-delete-button[data-phase="confirmed"] .hold-to-delete-button-fill { clip-path: inset(0 0 0 0); }.hold-to-delete-button-swap { grid-area: 1 / 1; transition: opacity 200ms ${EASE_OUT}, filter 200ms ${EASE_OUT}, transform 200ms ${EASE_OUT}; }.hold-to-delete-button-swap[data-hidden] { opacity: 0; filter: blur(2px); transform: scale(0.94); }@media (hover: hover) and (pointer: fine) { .hold-to-delete-button[data-phase="idle"]:hover .hold-to-delete-button-icon { background-color: rgb(254 226 226); }}@media (prefers-reduced-motion: reduce) { .hold-to-delete-button, .hold-to-delete-button[data-phase="holding"] { transform: none; } .hold-to-delete-button-swap { transition: opacity 200ms ease; } .hold-to-delete-button-swap[data-hidden] { filter: none; transform: none; }}`; function Swap({ show, children }: { show: boolean; children: React.ReactNode }) { return ( <span className="hold-to-delete-button-swap" data-hidden={show ? undefined : ""}> {children} </span> );} function Face({ confirmed, tone, label, confirmedLabel }: { confirmed: boolean; tone: "base" | "fill"; label: string; confirmedLabel: string;}) { const fill = tone === "fill"; return ( <span className="flex h-full items-center gap-2.5 py-[6px] pr-5 pl-[6px]"> <span className={cn( "hold-to-delete-button-icon grid size-9 shrink-0 place-items-center rounded-full transition-colors duration-200 motion-reduce:transition-none", fill ? "bg-white/15 text-white" : "bg-red-50 text-red-600", )} > <Swap show={!confirmed}> <Trash2 className="size-[17px]" strokeWidth={2.2} /> </Swap> <Swap show={confirmed}> <Check className="size-[17px]" strokeWidth={2.6} /> </Swap> </span> {/* Both labels share one grid cell, so the pill keeps the wider width and never jumps. */} <span className={cn( "grid text-left text-[15px] leading-none font-semibold tracking-[-0.02em] whitespace-nowrap", fill ? "text-white" : "text-zinc-900", )} > <Swap show={!confirmed}>{label}</Swap> <Swap show={confirmed}>{confirmedLabel}</Swap> </span> </span> );} const HOLD_KEYS = new Set([" ", "Enter"]); export const HoldToDeleteButton = React.forwardRef<HTMLButtonElement, HoldToDeleteButtonProps>( ( { onConfirm, holdDuration = 2000, resetAfter = 2000, label = "Hold to delete", confirmedLabel = "Deleted", disabled, className, style, onPointerDown, onPointerUp, onPointerLeave, onPointerCancel, onKeyDown, onKeyUp, onBlur, onContextMenu, ...props }, ref, ) => { const [phase, setPhase] = React.useState<Phase>("idle"); const holdTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null); const resetTimer = React.useRef<ReturnType<typeof setTimeout> | null>(null); const onConfirmRef = React.useRef(onConfirm); onConfirmRef.current = onConfirm; const clearTimers = React.useCallback(() => { if (holdTimer.current) clearTimeout(holdTimer.current); if (resetTimer.current) clearTimeout(resetTimer.current); holdTimer.current = null; resetTimer.current = null; }, []); React.useEffect(() => clearTimers, [clearTimers]); const start = () => { if (disabled || phase !== "idle") return; setPhase("holding"); // The timer, not transitionend, decides completion, so it still fires if transitions are disabled. holdTimer.current = setTimeout(() => { holdTimer.current = null; setPhase("confirmed"); onConfirmRef.current?.(); if (resetAfter !== null) { resetTimer.current = setTimeout(() => { resetTimer.current = null; setPhase("idle"); }, resetAfter); } }, holdDuration); }; const cancel = () => { if (phase !== "holding") return; if (holdTimer.current) clearTimeout(holdTimer.current); holdTimer.current = null; setPhase("idle"); }; const confirmed = phase === "confirmed"; return ( <button ref={ref} type="button" disabled={disabled} data-tsuna-id="hold-to-delete-button" data-phase={phase} className={cn( "hold-to-delete-button relative inline-flex h-12 cursor-pointer touch-manipulation overflow-hidden rounded-full border-0 bg-white p-0 select-none", "shadow-[0_18px_40px_-18px_rgba(15,23,42,0.28),0_4px_12px_-8px_rgba(15,23,42,0.14)]", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-red-600/40 focus-visible:ring-offset-2 focus-visible:ring-offset-[#e8eaed]", "disabled:cursor-not-allowed disabled:opacity-50", className, )} style={{ ["--hold-to-delete-duration" as string]: `${holdDuration}ms`, ...style }} onPointerDown={(event) => { onPointerDown?.(event); if (event.defaultPrevented || event.button !== 0) return; start(); }} onPointerUp={(event) => { onPointerUp?.(event); cancel(); }} onPointerLeave={(event) => { onPointerLeave?.(event); cancel(); }} onPointerCancel={(event) => { onPointerCancel?.(event); cancel(); }} onKeyDown={(event) => { onKeyDown?.(event); if (event.defaultPrevented || !HOLD_KEYS.has(event.key)) return; // Stop Enter/Space from firing a click: only a full hold confirms. event.preventDefault(); if (!event.repeat) start(); }} onKeyUp={(event) => { onKeyUp?.(event); if (!HOLD_KEYS.has(event.key)) return; event.preventDefault(); cancel(); }} onBlur={(event) => { onBlur?.(event); cancel(); }} onContextMenu={(event) => { onContextMenu?.(event); // A long press on touch would otherwise open the context menu mid-hold. event.preventDefault(); }} {...props} > <style>{CSS}</style> <span className="sr-only" aria-live="polite"> {confirmed ? confirmedLabel : label} </span> <span aria-hidden className="contents"> <Face confirmed={confirmed} tone="base" label={label} confirmedLabel={confirmedLabel} /> <span className="hold-to-delete-button-fill absolute inset-0 rounded-full bg-red-600"> <Face confirmed={confirmed} tone="fill" label={label} confirmedLabel={confirmedLabel} /> </span> </span> </button> ); },); HoldToDeleteButton.displayName = "HoldToDeleteButton";コンポーネント
長押し削除ボタン
ゴミ箱のアイコンと Hold to delete の文字が並ぶ、白い pill 型の削除ボタンです。押し続けると赤い塗りが左から広がり、2 秒で満ちると Deleted に替わります。途中で離すと塗りはすぐ戻ります。Space / Enter の長押しでも動きます。押す時間とラベルを差し替えて、onConfirm に削除の処理をつなぎます。
Verified Technical Spec
- アニメーション
- CSS / Tailwind
- Reduced motion
- 確認済み
- キーボードフォーカス
- 確認済み
- 対応環境
- Tailwind CSS v3.4 + v4 · React 19 / Next.js 15 App Router + Vite / Remix 対応
含まれるもの
- ソースを Cursor / Claude に渡してエージェントに組み込める
- React · TypeScript · Tailwind CSS · Lucide の完成 UI
依存パッケージ
- React 19.0.0
- Typescript 5.7.2
- Tailwindcss 3.4.16
- Lucide-react 1.23.0
技術スタック
- React
- TypeScript
- Tailwind CSS
- Lucide
バージョン 1.0.0




