Cómo funciona
Position comes from one CSS variable, --_p (0 to 1): a runner exactly as wide as the travel distance moves the handle with translateX, and the fill and hint text derive from the same value, so a width change never misaligns anything. Dragging uses Pointer Events with pointer capture and writes the variable directly, without React renders. The handle is a <button>, not a slider: a plain tap only nudges it to hint that sliding is needed.
Úsalo para
- Irreversible actions that need protection from accidental taps: paying, withdrawing, confirming a delivery.
- The main confirm action at the bottom of a mobile screen, instead of a button plus a confirmation dialog.
- Steps with financial consequences, such as releasing funds.
Busca otra opción cuando
- Ordinary, reversible actions: a plain button is cheaper than sliding.
- Desktop-first admin forms: a button with a confirmation dialog is handier than dragging.
- Several confirmations side by side: use sliding only for the one that matters most.
Uso
import { useRef } from 'react'
import { SlideToConfirm } from './SlideToConfirm.jsx'
export function PayBar({ orderId }) {
const ref = useRef(null)
return (
<SlideToConfirm
ref={ref}
label="Slide to pay"
loadingLabel="Paying…"
doneLabel="Payment complete"
threshold={0.8}
// Return a Promise: loading shows meanwhile, and failure resets the handle
onConfirm={() => api.pay(orderId)}
/>
)
// To start over: ref.current?.reset(), or give the component a new key
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
label | ReactNode | 'Slide to confirm' | Hint text on the track (with the shimmer), also the accessible name of the handle button. |
loadingLabel | ReactNode | 'Processing…' | Shown while the Promise returned by onConfirm is pending. |
doneLabel | ReactNode | 'Confirmed' | Shown after completion. |
labels | object | — | Replaces other built-in text (hint, the instruction read by screen readers). Shallow-merged over the English defaults. |
threshold | number | 0.8 | How far across the track the handle must be released to confirm, 0 to 1. |
onConfirm | () => void | Promise<unknown> | — | Called on confirm. A returned Promise shows loading, resolves to the done state and resets the handle on rejection. |
disabled | boolean | false | Disables the control. |
ref | Ref<{ reset(), focus() }> | — | reset() returns the handle to the start and discards a pending result; focus() focuses the handle. A new key also works. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes, such as className and style, go to the root. |
Accesibilidad
- The handle is a focusable <button>: Enter, Space, Right Arrow and End confirm directly, and aria-describedby gives the instruction.
- Screen reader activation (a click without a pointer press) also confirms; a plain pointer tap does not.
- Processing and done states are read through an aria-live region; once done the button name changes and aria-disabled is set, so focus is not lost.
- The knob is 50×50 and the track 60px tall; touch-action: pan-y keeps vertical page scrolling.
- With reduced motion the shimmer is removed, snapping and returning are instant and the check is not drawn; dragging still follows 1:1.
Movimiento
Dragging has no transition and follows 1:1; returning below the threshold 360ms ease-out (no overshoot against the wall); snapping past it 240ms ease-out, then the knob lands with a 560ms spring bounce (1.12). Pressing shrinks the knob to 0.92 in 100ms and it springs back in 360ms. Icon switch 240ms spring; check draw 360ms after 80ms; shimmer 2.8s per sweep; tap nudge 14px over 520ms spring.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
SlideToConfirm.jsxslide-to-confirm.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.







