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.
pnpm dlx shadcn@latest add @haze/slide-to-confirm---
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>When to use
- 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.
When not to use
- 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.
Installation
Add the Haze registry to components.json once, then install Slide to Confirm with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/slide-to-confirmnpx shadcn@latest add @haze/slide-to-confirmyarn dlx shadcn@latest add @haze/slide-to-confirmbunx --bun shadcn@latest add @haze/slide-to-confirmFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique ID for the wrapper. |
| label | string | Send payment | Accessible name of the slider and label of the standard button. |
| slideLabel | string | Slide to confirm | Text on the track before confirming. |
| confirmedLabel | string | Payment sent | Text and live announcement after confirmation. |
| form | string | – | ID of a form to submit on confirmation. |
| name | string | – | Native submit button name. |
| value | string | – | Native submit button value. |
| disabled | boolean | false | Dims the track and blocks dragging and keys. |
| class | string | – | Classes on the wrapper. |
Keyboard interactions
- Arrow RightArrow UpEnterSpace
- Advance the thumb by 25%. Four steps confirm.
- Arrow LeftArrow Down
- Move back by 25%.
- HomeEscape
- Return to the start.
- End
- Confirm at once.
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.