Slider
Native range input over a thin track with a filled part and a round thumb; a small script keeps the fill current.
pnpm dlx shadcn@latest add @haze/slider---
import { cn } from "../../../lib/haze/utils";
interface Props {
id: string;
name?: string;
min?: number;
max?: number;
step?: number;
value?: number;
disabled?: boolean;
/** Extra classes on the wrapper that holds the track, the fill and the input. */
class?: string;
/** Any other input attribute, for example aria-describedby, aria-valuetext or list. */
[key: string]: unknown;
}
const { id, name = id, min = 0, max = 100, step = 1, value = 50, disabled, class: className, ...rest } = Astro.props;
const span = max - min;
// Starting fill, 0 to 1. The script keeps it current while the user drags.
const fill = span > 0 ? Math.min(1, Math.max(0, (value - min) / span)) : 0;
---
<div data-haze-slider style={`--slider-fill:${fill}`} class={cn("relative h-10 w-full min-w-0 has-[:disabled]:opacity-50", className)}>
<div aria-hidden="true" class="pointer-events-none absolute inset-x-0 top-1/2 h-1.5 -translate-y-1/2 rounded-full bg-input forced-colors:hidden"></div>
<div
aria-hidden="true"
class="pointer-events-none absolute top-1/2 left-0 hidden h-1.5 w-[calc(var(--slider-fill)*(100%_-_1.375rem)_+_0.6875rem)] -translate-y-1/2 rounded-full bg-primary forced-colors:hidden [[data-enhanced]_&]:block"
></div>
<input
id={id}
name={name}
type="range"
min={min}
max={max}
step={step}
value={value}
disabled={disabled}
class={cn(
"relative h-10 w-full min-w-0 cursor-pointer appearance-none rounded-md bg-transparent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed",
"[&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-transparent",
"[&::-webkit-slider-thumb]:-mt-2 [&::-webkit-slider-thumb]:size-5.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-foreground [&::-webkit-slider-thumb]:shadow-key",
"[&::-webkit-slider-thumb]:transition-[scale] [&::-webkit-slider-thumb]:duration-150 [&::-webkit-slider-thumb]:ease-snappy",
"enabled:hover:[&::-webkit-slider-thumb]:scale-110 enabled:active:[&::-webkit-slider-thumb]:scale-95",
"[&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-transparent",
"[&::-moz-range-thumb]:size-5.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-foreground [&::-moz-range-thumb]:shadow-key",
"[&::-moz-range-thumb]:transition-[scale] [&::-moz-range-thumb]:duration-150 [&::-moz-range-thumb]:ease-snappy",
"enabled:hover:[&::-moz-range-thumb]:scale-110 enabled:active:[&::-moz-range-thumb]:scale-95",
"forced-colors:appearance-auto",
)}
{...rest}
/>
</div>
<script>
function setFill(root: HTMLElement, input: HTMLInputElement) {
const min = Number(input.min || 0);
const max = Number(input.max || 100);
const span = max - min;
const fill = span > 0 ? Math.min(1, Math.max(0, (Number(input.value) - min) / span)) : 0;
root.style.setProperty("--slider-fill", String(fill));
}
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const input = root.querySelector<HTMLInputElement>("input[type=range]");
if (!input) return;
setFill(root, input);
input.addEventListener("input", () => setFill(root, input));
input.addEventListener("change", () => setFill(root, input));
input.form?.addEventListener("reset", () => queueMicrotask(() => setFill(root, input)));
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-slider]").forEach(enhance);
init();
document.addEventListener("astro:page-load", init);
</script>When to use
- Picking an approximate value on a bounded scale, such as quality or volume
- Settings where the exact number matters less than direction and size
When not to use
- The user must type an exact value; use input with type number
- A choice from a few named steps; use radio-group or toggle-group
Installation
Add the Haze registry to components.json once, then install Slider with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/slidernpx shadcn@latest add @haze/slideryarn dlx shadcn@latest add @haze/sliderbunx --bun shadcn@latest add @haze/sliderFirst 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 | – | Input id; reference it from a label or Field. |
| name | string | id | Form field name. |
| min | number | 0 | Lowest value. |
| max | number | 100 | Highest value. |
| step | number | 1 | Increment between values. |
| value | number | 50 | Initial value. |
| disabled | boolean | – | Dims the slider and blocks input. |
| class | string | – | Extra classes on the wrapper around the track and input. |
| ...rest | attributes | – | Any other input attribute, for example aria-valuetext or aria-describedby. |
Keyboard interactions
- Arrow LeftArrow Down
- Decreases the value by one step
- Arrow RightArrow Up
- Increases the value by one step
- HomeEnd
- Jumps to the minimum or maximum
- Page UpPage Down
- Changes the value by a larger step
Accessibility
- The browser exposes the value to assistive technology; add aria-valuetext when the number needs units, such as "80 percent".
- Under forced colors the custom track and fill are hidden and the native range control is used, so the track and thumb stay visible.
- The track and fill are aria-hidden divs behind the input; the input stays the only control. Without script the fill is hidden and the track shows no filled part.
- The control shows no number. Print the value next to it if exact values matter, because a thumb position alone is hard to read precisely.
- The thumb is 16 px with a 24 px tall hit area on the input, which meets the minimum target size.
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 "Slider" with `npx shadcn@latest add @haze/slider`. 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 Slider from "@/components/haze/ui/slider.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/slider.md
Customize with real content
Adapt the Haze slider ui for our product. Replace the demo values of these props with our real content: id, name, min, max, step, value, disabled, class, ...rest. Keep the layout, the heading level rules and these accessibility details: The browser exposes the value to assistive technology; add aria-valuetext when the number needs units, such as "80 percent". Under forced colors the custom track and fill are hidden and the native range control is used, so the track and thumb stay visible. The track and fill are aria-hidden divs behind the input; the input stays the only control. Without script the fill is hidden and the track shows no filled part. The control shows no number. Print the value next to it if exact values matter, because a thumb position alone is hard to read precisely. The thumb is 16 px with a 24 px tall hit area on the input, which meets the minimum target size. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using slider, check it fits: use it when picking an approximate value on a bounded scale, such as quality or volume; settings where the exact number matters less than direction and size. Avoid it when the user must type an exact value; use input with type number; a choice from a few named steps; use radio-group or toggle-group. Alternatives: @haze/input, @haze/toggle-group, @haze/field.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.