# Slide to Confirm

> A track with a thumb that is dragged to the end to confirm a consequential action. Falls back to a standard button without scripts.

- Kind: ui · Category: Actions · Interactivity: script
- Install: `npx shadcn@latest add @haze/slide-to-confirm`
- File: `src/components/haze/ui/slide-to-confirm.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/button, @haze/utils
- Docs page: https://hazeui.com/ui/slide-to-confirm

## Use when
- A single action such as sending a payment is easy to trigger by mistake and a dialog would be heavy.
- Dragging to the end suits the surrounding touch flow.

## Avoid when
- The action needs its consequences explained or more input; use alert-dialog.
- Most people will use a keyboard or assistive technology; use hold-to-confirm or a two-step button.

## Alternatives
- [hold-to-confirm](https://hazeui.com/ui/hold-to-confirm.md)
- [alert-dialog](https://hazeui.com/ui/alert-dialog.md)
- [stateful-button](https://hazeui.com/ui/stateful-button.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique ID for the wrapper. |
| label | `string` | Send payment | no | Accessible name of the slider and label of the standard button. |
| slideLabel | `string` | Slide to confirm | no | Text on the track before confirming. |
| confirmedLabel | `string` | Payment sent | no | Text and live announcement after confirmation. |
| form | `string` |  | no | ID of a form to submit on confirmation. |
| name | `string` |  | no | Native submit button name. |
| value | `string` |  | no | Native submit button value. |
| disabled | `boolean` | false | no | Dims the track and blocks dragging and keys. |
| class | `string` |  | no | Classes on the wrapper. |

## Accessibility
- The thumb is a slider with aria-valuenow (0 to 100) and a spoken value (the slide label at 0, n percent with release to cancel in between, the confirmed label when done) and a hidden description of the keys. Releasing at 90% or more confirms; a shorter drag returns the thumb to the start.
- Confirming emits a bubbling slide-confirm event and clicks the standard button, so a form is submitted. Without scripts the standard button is shown instead of the track.
- After confirming the thumb shows a check, stays focusable with aria-disabled, and the confirmed label is announced. The root element has a reset() method to retry. The settle and return animations are skipped under reduced motion. The layout is left-to-right.
- Key Arrow Right / Arrow Up / Enter / Space: Advance the thumb by 25%. Four steps confirm.
- Key Arrow Left / Arrow Down: Move back by 25%.
- Key Home / Escape: Return to the start.
- Key End: Confirm at once.

## Agent prompts

### Install and use

Install the Haze UI component "Slide to Confirm" with `npx shadcn@latest add @haze/slide-to-confirm`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import SlideToConfirm from "@/components/haze/ui/slide-to-confirm.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/slide-to-confirm.md

### Customize with real content

Adapt the Haze slide-to-confirm ui for our product. Replace the demo values of these props with our real content: id, label, slideLabel, confirmedLabel, form, name, value, disabled, class. Keep the layout, the heading level rules and these accessibility details: The thumb is a slider with aria-valuenow (0 to 100) and a spoken value (the slide label at 0, n percent with release to cancel in between, the confirmed label when done) and a hidden description of the keys. Releasing at 90% or more confirms; a shorter drag returns the thumb to the start. Confirming emits a bubbling slide-confirm event and clicks the standard button, so a form is submitted. Without scripts the standard button is shown instead of the track. After confirming the thumb shows a check, stays focusable with aria-disabled, and the confirmed label is announced. The root element has a reset() method to retry. The settle and return animations are skipped under reduced motion. The layout is left-to-right. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using slide-to-confirm, check it fits: use it when a single action such as sending a payment is easy to trigger by mistake and a dialog would be heavy.; dragging to the end suits the surrounding touch flow.. Avoid it when the action needs its consequences explained or more input; use alert-dialog.; most people will use a keyboard or assistive technology; use hold-to-confirm or a two-step button.. Alternatives: @haze/hold-to-confirm, @haze/alert-dialog, @haze/stateful-button.

## Example requests that should lead here
- Let people slide to confirm a payment on the review screen

## Source

```astro
---
import { Check, ChevronsRight } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";

interface Props {
  id: string;
  label?: string;
  slideLabel?: string;
  confirmedLabel?: string;
  form?: string;
  name?: string;
  value?: string;
  disabled?: boolean;
  class?: string;
}

const { id, label = "Send payment", slideLabel = "Slide to confirm", confirmedLabel = "Payment sent", form, name, value, disabled = false, class: className } = Astro.props;
---

<div id={id} data-haze-slide-to-confirm data-confirmed-label={confirmedLabel} data-slide-label-text={slideLabel} data-disabled={disabled ? "" : undefined} class={cn("grid w-full min-w-0 max-w-xs gap-2", className)}>
  <div data-slide-track hidden aria-disabled={disabled ? "true" : undefined}
    class="group relative h-11 touch-pan-y overflow-hidden rounded-full bg-muted p-1 select-none data-[disabled]:opacity-50 [&[hidden]]:hidden">
    <span aria-hidden="true" data-slide-fill
      class="absolute inset-y-0 start-0 w-[calc(var(--slide-x,0px)+2.75rem)] rounded-full bg-primary/10 motion-safe:transition-[width] motion-safe:duration-200 motion-safe:ease-snappy group-data-[dragging]:duration-0"></span>
    <span data-slide-label
      class="pointer-events-none absolute inset-0 flex items-center justify-center gap-1.5 px-12 text-[0.8125rem] font-medium text-muted-foreground opacity-(--slide-label,1) motion-safe:transition-[opacity] motion-safe:duration-200 motion-safe:ease-snappy group-data-[dragging]:duration-0 group-data-[done]:text-foreground">
      <Check aria-hidden="true" class="hidden size-4 shrink-0 motion-safe:animate-pop group-data-[done]:block" />
      <span data-slide-text class="truncate">{slideLabel}</span>
    </span>
    <div data-slide-thumb role="slider" tabindex={disabled ? -1 : 0} aria-label={label} aria-orientation="horizontal" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-valuetext={slideLabel} aria-describedby={`${id}-help`} aria-disabled={disabled ? "true" : undefined}
      class="relative z-10 grid size-9 translate-x-(--slide-x,0px) cursor-grab touch-none place-items-center rounded-full bg-primary text-primary-foreground hover:bg-primary/90 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring group-data-[dragging]:cursor-grabbing group-data-[done]:cursor-default group-data-[disabled]:cursor-not-allowed motion-safe:transition-[translate,background-color] motion-safe:duration-200 motion-safe:ease-snappy group-data-[dragging]:duration-0">
      <ChevronsRight aria-hidden="true" class="size-4 group-data-[done]:hidden" />
      <Check aria-hidden="true" class="hidden size-4 motion-safe:animate-pop group-data-[done]:block" />
    </div>
  </div>
  <Button data-slide-fallback type={form ? "submit" : "button"} form={form} name={name} value={value} disabled={disabled} class="w-full max-w-full whitespace-normal [&[hidden]]:hidden">{label}</Button>
  <span id={`${id}-help`} class="sr-only">Use Right Arrow or End to confirm; Home or Escape to cancel.</span>
  <span data-slide-status role="status" class="sr-only"></span>
</div>

<script>
  function enhance(root: HTMLElement) {
    if (root.hasAttribute("data-enhanced")) return;
    const track = root.querySelector<HTMLElement>("[data-slide-track]");
    const thumb = root.querySelector<HTMLElement>("[data-slide-thumb]");
    const text = root.querySelector<HTMLElement>("[data-slide-text]");
    const status = root.querySelector<HTMLElement>("[data-slide-status]");
    const fallback = root.querySelector<HTMLButtonElement>("[data-slide-fallback]");
    if (!track || !thumb || !text || !status || !fallback) return;
    const disabled = root.hasAttribute("data-disabled");
    const STEP = 0.25;
    const THRESHOLD = 0.9;
    let progress = 0;
    let done = false;
    let pointer: number | null = null;
    let grab = 0;

    const range = () => Math.max(1, track.clientWidth - thumb.offsetWidth - 8);
    const render = (value: number) => {
      progress = Math.min(1, Math.max(0, value));
      root.style.setProperty("--slide-x", `${progress * range()}px`);
      root.style.setProperty("--slide-label", done ? "1" : String(Math.max(0, 1 - progress * 1.6)));
      const percent = Math.round(progress * 100);
      thumb.setAttribute("aria-valuenow", String(percent));
      thumb.setAttribute("aria-valuetext", done ? root.dataset.confirmedLabel ?? "" : percent === 0 ? root.dataset.slideLabelText ?? "" : `${percent} percent, release to cancel`);
    };
    const confirm = () => {
      if (done || disabled) return;
      if (fallback.form && !fallback.form.noValidate && !fallback.form.reportValidity()) { render(0); return; }
      done = true;
      pointer = null;
      track.removeAttribute("data-dragging");
      track.setAttribute("data-done", "");
      thumb.setAttribute("aria-disabled", "true");
      text.textContent = root.dataset.confirmedLabel ?? "";
      render(1);
      status.textContent = text.textContent;
      root.dispatchEvent(new CustomEvent("slide-confirm", { bubbles: true }));
      fallback.click();
    };
    const settle = (value: number) => (value >= THRESHOLD ? confirm() : render(0));

    thumb.addEventListener("pointerdown", event => {
      if (!event.isPrimary || event.button !== 0 || done || disabled || pointer !== null) return;
      pointer = event.pointerId;
      thumb.setPointerCapture(event.pointerId);
      grab = event.clientX - progress * range();
      track.setAttribute("data-dragging", "");
    });
    thumb.addEventListener("pointermove", event => {
      if (pointer !== event.pointerId) return;
      render((event.clientX - grab) / range());
    });
    const release = (event: PointerEvent) => {
      if (pointer !== event.pointerId) return;
      pointer = null;
      track.removeAttribute("data-dragging");
      if (event.type === "pointercancel") render(0);
      else settle(progress);
    };
    thumb.addEventListener("pointerup", release);
    thumb.addEventListener("pointercancel", release);
    thumb.addEventListener("keydown", event => {
      if (done || disabled) return;
      let next: number | null = null;
      if (event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === "Enter" || event.key === " ") next = progress + STEP;
      else if (event.key === "ArrowLeft" || event.key === "ArrowDown") next = progress - STEP;
      else if (event.key === "Home" || event.key === "Escape") next = 0;
      else if (event.key === "End") next = 1;
      if (next === null) return;
      event.preventDefault();
      next = Math.min(1, Math.max(0, next));
      if (next >= 0.99) confirm();
      else render(next);
    });
    (root as HTMLElement & { reset?: () => void }).reset = () => {
      done = false;
      pointer = null;
      track.removeAttribute("data-done");
      track.removeAttribute("data-dragging");
      if (!disabled) thumb.removeAttribute("aria-disabled");
      text.textContent = root.dataset.slideLabelText ?? "";
      status.textContent = "";
      render(0);
    };
    new ResizeObserver(() => render(progress)).observe(track);

    if (disabled) track.setAttribute("data-disabled", "");
    track.hidden = false;
    fallback.hidden = true;
    render(0);
    root.setAttribute("data-enhanced", "");
  }

  const init = () => document.querySelectorAll<HTMLElement>("[data-haze-slide-to-confirm]").forEach(enhance);
  init();
  if (!document.documentElement.hasAttribute("data-haze-slide-to-confirm-listener")) {
    document.documentElement.setAttribute("data-haze-slide-to-confirm-listener", "");
    document.addEventListener("astro:page-load", init);
  }
</script>
```
