Hold button

Slant / Controls

Press and hold to confirm, the wait drawn in slanted pixels.

Demo

Usage

@tuttn/slant
import { HoldButton } from '@tuttn/slant'

// Danger by default: hold for 1.2s and onConfirm runs once.
<HoldButton onConfirm={deleteProject}>Delete project</HoldButton>

// Variant, tone and size work as on Button; duration is the hold in ms.
<HoldButton variant="primary" duration={2000} onConfirm={revoke}>Revoke key</HoldButton>
<HoldButton tone="neutral" variant="ghost" size="sm" onConfirm={archive}>Archive</HoldButton>

// The hint is the accessible description. Set it here, or for every hold
// button through the root's messages.holdToConfirm.
<HoldButton hint="Hold for two seconds to revoke" duration={2000} onConfirm={revoke}>Revoke</HoldButton>

// A click alone never submits; submit from onConfirm instead.
<HoldButton onConfirm={() => form.current?.requestSubmit()}>Delete account</HoldButton>

Props

PropTypeDefaultDescription
onConfirm() => void—Called once when a hold reaches the end: the pointer or Space/Enter stayed down for the whole `duration`.
durationnumber1200How long the hold takes, in milliseconds.
variant'primary' | 'secondary' | 'ghost''secondary'Emphasis, as on Button: a solid fill in the tone's colour, a raised surface, or no background.
toneTone'danger'Semantic colour, as on Button. A hold usually guards something destructive, so it starts as danger.
size'sm' | 'md' | 'lg''md'Height on the control scale, as on Button.
hintstring'Press and hold to confirm' (the root's `messages.holdToConfirm`)How to use the button, given to assistive technology as its accessible description. Pass `''` for none.
disabledbooleanfalseThe native attribute. Disabling the button mid-hold drops the hold and empties the fill.
…restButtonHTMLAttributes—aria-*, data-* and handlers pass through to the button, which also takes the ref, className and style. onClick still hears clicks, but a click confirms nothing and its default action is prevented.

Keyboard

Space or Enter starts the hold and releasing the key ends it; a key that auto-repeats doesn't restart it. A tap of either key does nothing, because the hold replaces the native click. Moving focus away lets go too.

Pointer

Only the primary button (or a single finger) holds. Lifting it, sliding off the button or a cancelled touch ends the hold, and an early release drains the fill back to empty in about 200ms. Pressing again mid-drain picks up from where the fill has got to.

Accessibility

The label stays the button's name and the hint, "Press and hold to confirm" by default, is its description. On reaching the end it calls onConfirm once, says "Confirmed" through the root's polite announcer, and sets data-confirmed until you let go. data-holding is present while it is held. The fill is decorative and hidden from assistive technology; the label keeps its contrast over it in every theme.

Motion and direction

The progress is written to --sl-hold-progress every frame from JavaScript, so the fill still runs when reduced motion removes Slant's transitions: the hold is functional, not decoration. It fills from the inline start, so under dir="rtl" it grows from the right.