# GlassSlideToConfirm

> A slide-to-confirm track. Drag its glass thumb to the end to run one deliberate action; let go early and it springs back.

```tsx
import { GlassSlideToConfirm } from "@vitrumui/react/slide-to-confirm";

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

// Short and chunky: the `xl` track in a narrow width, with a one-word label.
export default function Demo() {
  return (
    <GlassSlideToConfirm
      className="max-w-64"
      size="xl"
      pendingLabel="Sending"
      successLabel="Sent"
      resetAfter={2600}
      onConfirm={() => wait(1400)}
    >
      Send
    </GlassSlideToConfirm>
  );
}
```

**Use it for**

- A routine action a stray tap must not trigger: send money, pay, unlock, start a shift.
- Phone-first surfaces, where a deliberate horizontal gesture replaces a confirm dialog.

**Not for**

- Easily undone actions: use GlassButton.
- An async action a single press may start: use GlassActionButton.
- Consequences people must read first: use a dialog.

```bash
npm install @vitrumui/react # coming soon
```

## Usage

```tsx
import { GlassSlideToConfirm } from '@vitrumui/react/slide-to-confirm'

<GlassSlideToConfirm
  pendingLabel="Sending"
  successLabel="Sent"
  errorLabel="Not sent, slide to retry"
  onConfirm={send} // return the promise
>
  Slide to send $240
</GlassSlideToConfirm>
```

## The flow

- **Idle.** The thumb rests at the start as frosted white glass; the instruction is written in the track.
- **Drag.** The thumb turns to clear glass and bends the fill and the label it passes; its chevron stays in place, in the fill's text colour. It stays round while held, and speed stretches it along its travel. The fill trails a moment behind, joined to the thumb by a bridge that thins as they pull apart, and the track and the fill swell around the thumb like a membrane. Pushed past the end the track stretches like a rubber band; pushed against the start the wall gives a little. The thumb stays round at both.
- **Let go early.** Below `threshold` the thumb springs back; the fill's tail follows and is absorbed after it.
- **Pending.** Past the threshold the thumb lands at the end, the track draws in to a ring around it and `pendingLabel` appears beside it while a spinner takes the chevron's place, until the promise from `onConfirm` settles.
- **Success.** The white glass takes the fill's colour and the chevron's stroke bends into a check, beside `successLabel`. It stays confirmed, unless you set `resetAfter`: then the track opens first, `successLabel` leaving with its edge, and the thumb flies home after it, squashing a little as it lands against the start.
- **Error.** If `onConfirm` throws, the track opens again, the thumb returns and `errorLabel` replaces the instruction until the next slide.

## Examples

### A full-width payment

```tsx
import { GlassSlideToConfirm } from "@vitrumui/react/slide-to-confirm";

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

// Drag the thumb to the end: it turns to clear glass, pulls the fill behind it and bends
// the label it passes. Let go early and it springs back.
export default function Demo() {
  return (
    <div className="flex w-full max-w-sm flex-col items-center">
      <span className="flex size-12 items-center justify-center rounded-full bg-neutral-900 text-[15px] font-semibold text-white dark:bg-white dark:text-neutral-900">
        BG
      </span>
      <p className="mt-4 text-sm text-neutral-500 dark:text-neutral-400">
        Sending to Bilal Gürşen
      </p>
      <p className="mt-1 text-6xl font-semibold tracking-tight text-neutral-900 tabular-nums dark:text-white">
        $240
      </p>
      <GlassSlideToConfirm
        className="mt-9"
        size="lg"
        pendingLabel="Sending"
        successLabel="Sent to Bilal"
        resetAfter={2600}
        onConfirm={() => wait(1400)}
      >
        Slide to send
      </GlassSlideToConfirm>
    </div>
  );
}
```

### A coloured fill, and a failing action

```tsx
import { GlassSlideToConfirm } from "@vitrumui/react/slide-to-confirm";

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

// `fillClassName` colours the fill. The second one always fails: a rejected promise
// springs the thumb back and writes `errorLabel` until the next slide.
export default function Demo() {
  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <GlassSlideToConfirm
        size="lg"
        fillClassName="bg-emerald-500 text-white"
        successLabel="Unlocked"
        resetAfter={2000}
        onConfirm={() => {}}
      >
        Slide to unlock
      </GlassSlideToConfirm>
      <GlassSlideToConfirm
        size="lg"
        fillClassName="bg-rose-500 text-white"
        pendingLabel="Deleting"
        errorLabel="Offline, slide to retry"
        onConfirm={async () => {
          await wait(1200);
          throw new Error("offline");
        }}
      >
        Slide to delete
      </GlassSlideToConfirm>
    </div>
  );
}
```

Colour the fill with `fillClassName`: its `bg-*` is the fill, its `text-*` is the label written on it, and the thumb's icon while the glass is clear. At rest the thumb is always frosted white glass with a dark icon (a flat white disc without WebGL2). The control fills its container, so set the width on a parent or with `className`.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `string` | — | The instruction in the track, and the slider's accessible name. |
| `onConfirm` | `() => unknown` | — | Required. Runs once the thumb reaches the end; return a promise to stay pending until it settles. |
| `onConfirmError` | `(error) => void` | — | Called when `onConfirm` throws; the thumb springs back. |
| `pendingLabel` | `string` | — | Written beside the spinner while `onConfirm` runs. |
| `successLabel` | `string` | — | Written beside the check after it succeeds. |
| `errorLabel` | `string` | — | Replaces the instruction after a failure, until the next slide. |
| `threshold` | `number` | `0.9` | Share of the track the thumb must pass before letting go confirms. |
| `resetAfter` | `number` | `0` | Milliseconds after a success before the track opens again. `0` stays confirmed. |
| `hint` | `boolean` | `true` | Nudges the thumb every few seconds until it has been touched. Off with reduced motion. |
| `icon` | `ReactNode` | `chevron` | The thumb's idle icon. |
| `size` | `'sm' \| 'md' \| 'lg' \| 'xl'` | `'md'` | Track height 40, 48, 56 or 96 px. |
| `fillClassName` | `string` | `'bg-[#3291FF] text-white'` | Classes of the fill: its colour and the colour of the text on it. |
| `disabled` | `boolean` | `false` | Dims the control and ignores pointer and keyboard. |
| `className` | `string` | — | Width and position of the track. |

## Accessibility

- A real `role="slider"` named by its label, with `aria-valuenow` kept in step with the thumb.
- Arrow keys move the thumb in tenths, Page Up and Page Down in quarters, Home returns it and End confirms. Holding Space or Enter glides it to the end; letting go early springs it back.
- A `role="status"` region announces the pending, success and error labels. While pending or confirmed the slider is `aria-disabled`.
- Success is never colour alone: the chevron becomes a check and the label is written beside it.
- Vertical page scroll still works over the control on touch. With reduced motion each state shows at once.

Source: /docs/slide-to-confirm
