# Slider

> Native range input over a thin track with a filled part and a round thumb; a small script keeps the fill current.

- Kind: ui · Category: Forms · Interactivity: script
- Install: `npx shadcn@latest add @haze/slider`
- File: `src/components/haze/ui/slider.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/slider

## Use 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 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
- [input](https://hazeui.com/ui/input.md)
- [toggle-group](https://hazeui.com/ui/toggle-group.md)
- [field](https://hazeui.com/ui/field.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Input id; reference it from a label or Field. |
| name | `string` | id | no | Form field name. |
| min | `number` | 0 | no | Lowest value. |
| max | `number` | 100 | no | Highest value. |
| step | `number` | 1 | no | Increment between values. |
| value | `number` | 50 | no | Initial value. |
| disabled | `boolean` |  | no | Dims the slider and blocks input. |
| class | `string` |  | no | Extra classes on the wrapper around the track and input. |
| ...rest | `attributes` |  | no | Any other input attribute, for example aria-valuetext or aria-describedby. |

## 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.
- Key Arrow Left / Arrow Down: Decreases the value by one step
- Key Arrow Right / Arrow Up: Increases the value by one step
- Key Home / End: Jumps to the minimum or maximum
- Key Page Up / Page Down: Changes the value by a larger step

## Agent prompts

### 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": "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 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: https://hazeui.com/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.

## Example requests that should lead here
- Add an image quality slider with help text under it
- Show a disabled rate limit slider

## Source

```astro
---
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>
```
