Cómo funciona
The root keeps the size of the round trigger; the two pills live in an absolutely positioned tray beside it, so opening never moves anything. Closed, each pill is clipped to a circle at its anchored end (clip-path inset with round ends) and shifted onto the trigger by a distance measured when it opens. Opening releases both: the pills travel out with a light spring while the clip opens, the far one 40ms later, so the circle reads as splitting in two, and the labels sharpen in from a 3px blur. Cancel is the pill nearest the trigger, so a hurried second click lands on the safe choice. A darker layer drains across Cancel for the length of the timeout and pauses while the pointer or focus is inside.
Úsalo para
- Deleting or removing one item in a list or card: a file, a saved address, a team member.
- Destructive actions that deserve a second look but not a dialog that interrupts the page.
- Toolbars and rows where there is free space beside the button for the pills to open over.
Busca otra opción cuando
- Consequences that need explaining, or several items at once: use a confirm dialog.
- Actions that must not slip at all: Hold to confirm asks for more.
- Places with no room beside the button, such as a narrow cell at the edge of the screen.
Uso
import { TwoStepButton } from './TwoStepButton.jsx'
export function FileRow({ file, onRemoved }) {
return (
<div className="file-row">
<span>{file.name}</span>
<TwoStepButton
label={`Delete ${file.name}`}
// A Promise shows a spinner, then a check; reject it to keep the pills open and shake
onConfirm={() => fetch(`/api/files/${file.id}`, { method: 'DELETE' }).then((r) => {
if (!r.ok) throw new Error('failed')
})}
onDone={() => onRemoved(file.id)}
timeout={4000}
/>
</div>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
label | string | 'Delete' | Accessible name of the round trigger, for example “Delete Itinerary.pdf”. |
icon | ReactNode | trash icon | The 18px icon in the trigger (decorative). |
onConfirm | () => void | Promise | — | Runs when the action pill is pressed. Return a Promise to show a spinner until it settles: fulfilled shows a check and the done label, then folds; rejected shakes the pill and keeps it open for another try. |
onDone | () => void | — | Called after the done face has shown and the pills fold back: the moment to remove the item. |
onCancel | () => void | — | Called when it folds without confirming: Cancel, Escape, a press outside or the timeout. |
open / defaultOpen / onOpenChange | boolean / boolean / (open) => void | - / false / - | Controlled or uncontrolled open state, and a callback when it changes (use it to dim the row while asking). |
timeout | number | 4000 | Milliseconds before it folds back by itself; a layer drains across Cancel meanwhile and pauses while the pointer or focus is inside. 0 turns it off. |
tone | 'danger' | 'ink' | 'accent' | 'danger' | Colour of the action pill and the icon. |
size | 'sm' | 'md' | 'lg' | 'md' | Trigger and pill height: 32, 40 or 44px (sm grows to 36px on touch screens). |
align | 'end' | 'start' | 'end' | Which side the trigger sits on: the pills open over the space to its left (end) or right (start). Nothing around it moves. |
labels | object | — | Replaces built-in text: confirm, cancel, done, working, group. Shallow-merged over the English defaults. |
disabled | boolean | false | Disables the trigger. |
...rest | HTMLAttributes | — | Other attributes go to the root <span>. |
Accesibilidad
- The trigger is a <button> with aria-expanded and aria-controls; opening moves focus to Cancel, and folding back returns it to the trigger.
- The pills are real buttons in a group named by labels.group; Tab reaches both, Escape folds back.
- While closed the tray is hidden and its buttons leave the tab order.
- Working and done are announced through a polite live region; the action pill has aria-busy while working.
- The countdown pauses while the pointer or keyboard focus is inside, so nobody loses the choice while deciding.
- Reduced motion makes the pills appear in place with a short fade, without travel, blur, drain or shake.
Movimiento
Open: transform 460ms with a light overshoot, clip-path 380ms ease-out, the far pill 40ms later; labels fade and sharpen over 240–360ms after 110ms; the icon shrinks out. Close: 160ms ease-in. Done: the check draws in 360ms and the pair folds after 900ms. Failure: a 420ms decaying shake.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
TwoStepButton.jsxtwo-step-button.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.



