本文へ移動
日本語

仕組み

A button that confirms only when held. One CSS variable, --_p (0 to 1), drives everything: each frame JS writes time held ÷ duration, the fill scales from the left, and a white text layer is clipped to the same edge, so text turns white as the fill passes. Releasing, leaving the button or losing focus snaps back in 160ms. All texts share one grid cell, so the size never changes. The fill is a gradient that is lighter behind and full strength at its leading edge; when the action completes the button lifts with a soft glow of its own colour.

向いている場面

  • Irreversible or costly actions such as deleting a record, cancelling an order or unlinking a device, in place of a confirm dialog.
  • Destructive confirmations that must work on both mobile and desktop without interrupting the page.
  • Ordinary confirmations that need protection from slips (tone="primary"), such as emptying a cart.

向かない場面

  • Ordinary, reversible actions: use a plain button.
  • Consequences that need explaining, or a typed confirmation: use a dialog.
  • Several hold buttons side by side: keep it for the single most dangerous action.

使い方

import { useRef } from 'react'
import { HoldToConfirm } from './HoldToConfirm.jsx'

export function DeleteProject({ projectId }) {
  const ref = useRef(null)
  return (
    <HoldToConfirm
      ref={ref}
      tone="danger"
      duration={1200}
      label="Hold to delete project"
      holdingLabel="Keep holding…"
      loadingLabel="Deleting…"
      doneLabel="Project deleted"
      // Return a Promise: a loading state shows meanwhile, and a rejection snaps back
      onConfirm={() => api.deleteProject(projectId)}
    />
  )
  // To allow another try: ref.current?.reset(), or give the component a new key
}

プロパティ

プロパティ型既定値説明
durationnumber1200How long to hold before it fires, in ms. Minimum 200.
labelReactNode'Hold to confirm'Default text. Say what the hold does, such as “Hold to delete project”. Also the accessible name.
holdingLabelReactNode'Keep holding…'Text while held. It appears 120ms after pressing, so a quick tap does not flash it.
loadingLabelReactNode'Working…'Shown with a spinner while the Promise from onConfirm is pending.
doneLabelReactNode'Confirmed'Shown with a checkmark when finished.
againLabelReactNode'Press again to confirm'For screen readers and voice control, which cannot hold: shown and announced after the first activation. A second activation within 4 seconds confirms.
labelsobject—Replaces built-in text that has no prop of its own (hint: (seconds) => string, the description read by screen readers). Shallow-merged over the English defaults.
onConfirm() => void | Promise—Called when the fill completes. A returned Promise shows loading; resolving shows done, rejecting snaps back so the user can retry.
tone'danger' | 'primary' | 'ink''danger'Colour. Danger for delete, close or cancel; primary or ink (neutral, inverts in dark) for ordinary confirmations that need protection from slips.
blockbooleanfalseFills the width of the parent.
disabledbooleanfalseDisables the button. Disabling it while held cancels at once.
refRef<{ reset(), focus() }>—reset returns to the initial state and discards a pending result; focus focuses the button. A new key also works.
...restButtonHTMLAttributes—Other attributes go to the <button>. Pointer and key handlers you pass run before the component's own.

アクセシビリティ

  • A native <button>: holding Space or Enter is timed the same way, and releasing, switching windows or moving focus cancels.
  • aria-describedby gives the hint “Hold for about N s to confirm”; replace it through labels.hint.
  • Screen reader and voice clicks have no hold, so they become a two-step confirmation: the first announces “Press again to confirm”, a second within 4 seconds fires.
  • Pressed, working and done states are announced through a live region; the button is 44px tall.
  • Reduced motion keeps the fill (it is timing feedback) and removes the snap-back, press scale, text movement and checkmark drawing.

モーション

Fill advances linearly over the duration (1200ms by default); an early release snaps back in 160ms ease-in. Text enters in 240ms with a 6px rise, and the checkmark draws in 360ms. On completion a soft glow of the tone colour fades in over 360ms.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

HoldToConfirm.jsxhold-to-confirm.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

ボタン
「オーロラ縁ボタン」は Pro に含まれます。解除するとプレビューが表示されます。
「かじりボタン」は Pro に含まれます。解除するとプレビューが表示されます。