# Slider

Pick a number, or a range, by dragging along a track.

Pick a number, or a range between two, by dragging a thumb along a track; arrow keys step it. The control and thumb sizes are the switch's (md 24px with a 20px thumb, sm 18px with 14px), the rail is the switch's colours (line-strong, the filled range in ink) and the thumb is the segmented control's (surface with shadow.btn). An optional label and value sit on one row above. Behaviour by Ark UI's slider (Zag). Controlled or uncontrolled.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `slider` machine, through Ark UI

## Add

```bash
npx fndn add slider
```

It writes the folder `slider/` into the project's ui folder (`index.ts`, `slider.tsx`, `Slider.vue`, `Slider.svelte`, `slider.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { Slider } from "@/components/ui/slider";
```

```vue
import { Slider } from "@/components/ui/slider";
```

```svelte
import { Slider } from "$lib/components/ui/slider";
```

## Parts

In the DOM the root carries `data-slot="slider"` and each part `data-slot="slider-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `slider` |  | Ark's slider root: a grid, the label at the start and the value at the end of the first row (baselines shared), the control below. data-disabled, data-dragging, data-focus from the engine. As wide as its container. |
| `label` | `label` | `slider-label` | optional | Visible name (the label prop): text.sm, medium, ink. Names every thumb (aria-labelledby). It wraps, never truncates; a click on it focuses the first thumb. |
| `value-text` | `span` | `slider-value-text` | optional | The current value (showValue), at the end of the label row: text.sm, tabular figures, ink-2, never wrapped. Two thumbs read "20 – 80". formatValue (React) formats each value, and the same text is announced (aria-valuetext). |
| `control` | `div` | `slider-control` |  | The hit area: full width, 24px high (sm 18px), cursor pointer; a press anywhere on it moves the nearest thumb there and starts a drag. The engine puts touch-action: none on it, so a horizontal drag never scrolls the page. On a coarse pointer it reaches 8px above and below (40px), without changing its look. |
| `track` | `div` | `slider-track` |  | The 4px rail, radius.pill, color.line-strong; under a mouse hovering the control it darkens a little (the switch's hover). |
| `range` | `div` | `slider-range` |  | The filled part of the rail, in ink: from the start to the thumb, or between two thumbs. Positioned by the engine. |
| `thumb` | `div` | `slider-thumb` | repeats | role=slider, one per value, focusable (the shared focus ring, round). 20px circle (sm 14px), surface with shadow.btn, kept inside the track by the engine. Pointer: cursor grab, grabbing while dragged. On a coarse pointer its hit area is 40px (md). Carries kbd-instant. |
| `hidden-input` | `input` | `slider-hidden-input` | optional, repeats | Ark's hidden input per thumb, carrying the value for a form (name). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number[]` | `[50]` | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). One number per thumb: [40] is a slider, [20, 80] a range. Controlled: value with onValueChange. Uncontrolled: defaultValue. |
| `min` | `number` | `0` |  |
| `max` | `number` | `100` |  |
| `step` | `number` | `1` | Arrow keys move one step; Page Up / Page Down ten steps; Home / End to the ends. |
| `minStepsBetweenThumbs` | `number` | `0` | A range: how many steps the two thumbs keep between them. |
| `size` | `"sm" \| "md"` | `"md"` | md: 24px control, 20px thumb (the switch md). sm: 18px control, 14px thumb (the switch sm). |
| `label` | `string` |  | Visible label, above at the start; names the thumbs. |
| `showValue` | `boolean` | `false` | Show the value at the end of the label row. |
| `thumbLabels` | `string[]` |  | Names of the thumbs of a range ("Minimum", "Maximum"), one per thumb. Without them every thumb is named by the label, or by aria-label. |
| `name` | `string` |  | Form field name (the hidden inputs). |
| `disabled` | `boolean` | `false` | Inert at half opacity; the thumbs leave the tab order. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: number[]) while dragging and on every key |
| `valueChangeEnd` | (value: number[]) when a drag ends or after a key |

## States

`hover`, `dragging`, `focus-visible`, `disabled`

## Keyboard and accessibility

Role: slider.

Keyboard:

- Tab moves to each thumb in turn
- Arrow Right: one step up; Arrow Left: one step down (right-to-left, Arrow Left goes up); Arrow Up / Down do nothing on a horizontal slider (the engine's keys)
- Page Up / Page Down: a large step (ten steps, on the step grid)
- Home / End: the minimum / the maximum

Notes:

- Each thumb is role=slider with aria-valuemin, aria-valuemax, aria-valuenow and aria-orientation; a range's thumbs are bounded by each other (aria-valuemin / max follow the other thumb).
- Name it with label (visible), aria-label, or thumbLabels for the thumbs of a range.
- formatValue gives the text announced (aria-valuetext) as well as the text shown.
- To set it from outside, control `value`: React value / onValueChange.

## Motion

- thumb travel: none; it follows the pointer 1:1, jumps where the track is pressed, and keys move it instantly (kbd-instant)
- thumb (pointer only): gives to motion.press-scale-sm over duration.press with easing.out-strong while it is dragged
- thumb hover (a real pointer only): shadow.btn → shadow.raised over duration.fast
- rail hover (pointer only): colour over duration.fast

## Tokens

`color.line-strong`, `color.ink`, `color.ink-2`, `color.ink-3`, `color.surface`, `shadow.btn`, `radius.pill`, `text.sm`, `duration.fast`, `duration.press`, `easing.out-strong`, `motion.press-scale-sm`

## Examples

### Monthly spend limit

React · `spend-limit.tsx`

```tsx
import { Slider } from "@/components/ui/slider";

/* one number on a track: the label names it, the value shows formatted at the end of the row */
const usd = (v: number) => `$${v.toLocaleString("en-US")}`;

export default function SpendLimit() {
  return (
    <div className="flex w-80 flex-col gap-2">
      <Slider label="Monthly spend limit" showValue min={100} max={5000} step={100} defaultValue={[1200]} formatValue={usd} />
      <p className="text-xs text-ink-2">We email maya@northwind.example when Northwind reaches 80%.</p>
    </div>
  );
}
```

Vue · `spend-limit.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Slider } from "@/components/ui/slider";

/* one number on a track: the label names it, the value shows formatted at the end of the row */
const usd = (v: number) => `$${v.toLocaleString("en-US")}`;

const limit = ref([1200]);
</script>

<template>
  <div class="flex w-80 flex-col gap-2">
    <Slider v-model:value="limit" label="Monthly spend limit" show-value :min="100" :max="5000" :step="100" :format-value="usd" />
    <p class="text-xs text-ink-2">We email maya@northwind.example when Northwind reaches 80%.</p>
  </div>
</template>
```

Svelte · `spend-limit.svelte`

```svelte
<script lang="ts">
  import { Slider } from "$lib/components/ui/slider";

  /* one number on a track: the label names it, the value shows formatted at the end of the row */
  const usd = (v: number) => `$${v.toLocaleString("en-US")}`;

  let limit = $state([1200]);
</script>

<div class="flex w-80 flex-col gap-2">
  <Slider bind:value={limit} label="Monthly spend limit" showValue min={100} max={5000} step={100} formatValue={usd} />
  <p class="text-xs text-ink-2">We email maya@northwind.example when Northwind reaches 80%.</p>
</div>
```

### Filter by response time (range)

React · `response-time.tsx`

```tsx
import { Slider } from "@/components/ui/slider";

/* a range: two thumbs, each named, kept a few steps apart */
export default function ResponseTime() {
  return (
    <Slider
      label="Response time"
      showValue
      min={0}
      max={2000}
      step={10}
      defaultValue={[120, 800]}
      minStepsBetweenThumbs={5}
      thumbLabels={["Fastest", "Slowest"]}
      formatValue={(v) => `${v} ms`}
      className="w-80"
    />
  );
}
```

Vue · `response-time.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Slider } from "@/components/ui/slider";

/* a range: two thumbs, each named, kept a few steps apart */
const range = ref([120, 800]);
</script>

<template>
  <Slider
    v-model:value="range"
    label="Response time"
    show-value
    :min="0"
    :max="2000"
    :step="10"
    :min-steps-between-thumbs="5"
    :thumb-labels="['Fastest', 'Slowest']"
    :format-value="(v) => `${v} ms`"
    class="w-80"
  />
</template>
```

Svelte · `response-time.svelte`

```svelte
<script lang="ts">
  import { Slider } from "$lib/components/ui/slider";

  /* a range: two thumbs, each named, kept a few steps apart */
  let range = $state([120, 800]);
</script>

<Slider
  bind:value={range}
  label="Response time"
  showValue
  min={0}
  max={2000}
  step={10}
  minStepsBetweenThumbs={5}
  thumbLabels={["Fastest", "Slowest"]}
  formatValue={(v) => `${v} ms`}
  class="w-80"
/>
```

### Replicas, with the price

React · `replicas.tsx`

```tsx
import { useState } from "react";
import { Slider } from "@/components/ui/slider";

/* controlled: the price under the slider follows it while it moves */
const PRICE = 18;

export default function Replicas() {
  const [replicas, setReplicas] = useState([3]);
  const count = replicas[0] ?? 1;
  return (
    <div className="flex w-80 flex-col gap-2">
      <Slider label="Replicas of billing-api" showValue min={1} max={10} value={replicas} onValueChange={setReplicas} />
      <p className="text-sm text-ink-2 tabular-nums">
        {count} × ${PRICE} = <span className="text-ink">${count * PRICE}.00</span> per month
      </p>
    </div>
  );
}
```

Vue · `replicas.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { Slider } from "@/components/ui/slider";

/* controlled: the price under the slider follows it while it moves */
const PRICE = 18;

const replicas = ref([3]);
const count = computed(() => replicas.value[0] ?? 1);
</script>

<template>
  <div class="flex w-80 flex-col gap-2">
    <Slider v-model:value="replicas" label="Replicas of billing-api" show-value :min="1" :max="10" />
    <p class="text-sm text-ink-2 tabular-nums">
      {{ count }} × ${{ PRICE }} = <span class="text-ink">${{ count * PRICE }}.00</span> per month
    </p>
  </div>
</template>
```

Svelte · `replicas.svelte`

```svelte
<script lang="ts">
  import { Slider } from "$lib/components/ui/slider";

  /* controlled: the price under the slider follows it while it moves */
  const PRICE = 18;

  let replicas = $state([3]);
  const count = $derived(replicas[0] ?? 1);
</script>

<div class="flex w-80 flex-col gap-2">
  <Slider bind:value={replicas} label="Replicas of billing-api" showValue min={1} max={10} />
  <p class="text-sm text-ink-2 tabular-nums">
    {count} × ${PRICE} = <span class="text-ink">${count * PRICE}.00</span> per month
  </p>
</div>
```

### Sample rates in a dense panel (small)

React · `sample-rates.tsx`

```tsx
import { Slider } from "@/components/ui/slider";

/* sm: a smaller track and thumb for a dense settings panel */
const percent = (v: number) => `${v}%`;

export default function SampleRates() {
  return (
    <div className="flex w-72 flex-col gap-4">
      <Slider size="sm" label="Log sampling" showValue defaultValue={[25]} formatValue={percent} />
      <Slider size="sm" label="Trace sampling" showValue defaultValue={[10]} formatValue={percent} />
    </div>
  );
}
```

Vue · `sample-rates.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Slider } from "@/components/ui/slider";

/* sm: a smaller track and thumb for a dense settings panel */
const percent = (v: number) => `${v}%`;

const logs = ref([25]);
const traces = ref([10]);
</script>

<template>
  <div class="flex w-72 flex-col gap-4">
    <Slider v-model:value="logs" size="sm" label="Log sampling" show-value :format-value="percent" />
    <Slider v-model:value="traces" size="sm" label="Trace sampling" show-value :format-value="percent" />
  </div>
</template>
```

Svelte · `sample-rates.svelte`

```svelte
<script lang="ts">
  import { Slider } from "$lib/components/ui/slider";

  /* sm: a smaller track and thumb for a dense settings panel */
  const percent = (v: number) => `${v}%`;

  let logs = $state([25]);
  let traces = $state([10]);
</script>

<div class="flex w-72 flex-col gap-4">
  <Slider bind:value={logs} size="sm" label="Log sampling" showValue formatValue={percent} />
  <Slider bind:value={traces} size="sm" label="Trace sampling" showValue formatValue={percent} />
</div>
```

### Locked on the Free plan (disabled)

React · `free-plan.tsx`

```tsx
import { Slider } from "@/components/ui/slider";

/* disabled: the setting stays readable, and the line under it says what unlocks it */
export default function FreePlan() {
  return (
    <div className="flex w-80 flex-col gap-2">
      <Slider label="Concurrent builds" showValue min={1} max={8} defaultValue={[1]} disabled />
      <p className="text-xs text-ink-2">The Free plan runs one build at a time. Upgrade to Team for up to 8.</p>
    </div>
  );
}
```

Vue · `free-plan.vue`

```vue
<script setup lang="ts">
import { Slider } from "@/components/ui/slider";
</script>

<!-- disabled: the setting stays readable, and the line under it says what unlocks it -->
<template>
  <div class="flex w-80 flex-col gap-2">
    <Slider label="Concurrent builds" show-value :min="1" :max="8" :value="[1]" disabled />
    <p class="text-xs text-ink-2">The Free plan runs one build at a time. Upgrade to Team for up to 8.</p>
  </div>
</template>
```

Svelte · `free-plan.svelte`

```svelte
<script lang="ts">
  import { Slider } from "$lib/components/ui/slider";
</script>

<!-- disabled: the setting stays readable, and the line under it says what unlocks it -->
<div class="flex w-80 flex-col gap-2">
  <Slider label="Concurrent builds" showValue min={1} max={8} value={[1]} disabled />
  <p class="text-xs text-ink-2">The Free plan runs one build at a time. Upgrade to Team for up to 8.</p>
</div>
```

## Recipe

`slider.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* slider — pick a number (or a range) by dragging a thumb along a track (Ark UI slider).
 *   md (default)  24px control · 20px thumb      sm  18px control · 14px thumb
 * The control and the thumb are the switch's (md 24 high with a 20px thumb, sm 18 with 14); the
 * track is a 4px rail in the switch's colours: line-strong, and ink for the filled range. The thumb
 * is the segmented control's thumb (surface with shadow.btn), so it reads on the page, on the range
 * and in both themes.
 *
 * Root: a grid, label at the start and the value at the end of the first row (baselines shared),
 * the control below across both columns. The engine positions the thumb (inset-inline-start, with
 * the thumb kept inside the track) and the range (left / right), and puts touch-action: none on the
 * control. The thumb tracks the pointer 1:1: its travel never animates (a drag would lag; a click on
 * the track jumps, as a native range does), and keys move it instantly.
 *
 * Touch: the control is the hit area (the whole width, 24px high); on a coarse pointer it reaches
 * 8px above and below (40px), without changing its look. */
export const slider = tv({
  slots: {
    base: [
      "grid grid-cols-[minmax(0,1fr)_auto] items-baseline gap-x-3 gap-y-1.5 w-full min-w-0",
      "data-disabled:opacity-50 data-disabled:pointer-events-none",
    ],
    label: "min-w-0 text-sm font-medium text-ink [overflow-wrap:anywhere] [unicode-bidi:plaintext]",
    /* the value never jitters in width while it changes; it keeps the end of the row */
    valueText: "col-start-2 justify-self-end text-sm tabular-nums whitespace-nowrap text-ink-2",
    control: [
      "col-span-full flex items-center w-full cursor-pointer",
      "pointer-coarse:after:content-[''] pointer-coarse:after:absolute pointer-coarse:after:inset-x-0 pointer-coarse:after:-inset-y-2",
    ],
    track: [
      "w-full h-1 overflow-hidden rounded-pill bg-line-strong",
      /* the switch's hover: the rail darkens a little under a mouse */
      "[@media(hover:hover)_and_(pointer:fine)]:[[data-slot=slider-control]:hover>&]:bg-[color-mix(in_oklch,var(--fndn-line-strong)_70%,var(--fndn-ink-3))]",
      "motion-colors-fast",
    ],
    range: "inset-y-0 bg-ink",
    /* centred on the control (the engine owns transform for the inline position; the block centring
     * is on translate). Pressed or dragged from a pointer it gives a little, as the switch's thumb;
     * never from the keyboard. Hovered by a mouse it lifts (shadow-raised at duration.fast). Above the
     * coarse hit area so it stays grabbable. */
    thumb: [
      "kbd-instant",
      "top-1/2 -translate-y-1/2 z-1 rounded-[50%] bg-surface shadow-btn cursor-grab",
      "data-dragging:cursor-grabbing",
      /* the thing to grab answers a mouse: the raised shadow (hover is gated to a real pointer) */
      "hover:shadow-raised motion-shadow-fast",
      "motion-also-[scale_var(--fndn-duration-press)_var(--fndn-easing-out-strong)]",
      "[:root:not([data-fndn-input=keyboard])_&]:data-dragging:squeeze-press-sm",
      "pointer-coarse:after:content-[''] pointer-coarse:after:absolute pointer-coarse:after:-inset-2.5",
    ],
  },
  variants: {
    size: {
      sm: { control: "h-[18px]", thumb: "size-3.5" },
      md: { control: "h-6", thumb: "size-5" },
    },
  },
  defaultVariants: { size: "md" },
});

export type SliderRecipeProps = VariantProps<typeof slider>;
```
