> Leaf UI 0.17.3 · Package: @ifhover/leaf-ui · Source: c3221834f84f2f48481a2054026b713456f8e5dc
> Browser documentation: https://ifhover.github.io/leaf-ui/en/components/slider.html

> For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt.

# Slider

Choose a number or range by sliding.


## Basic usage

Choose a numeric value with the slider and provide an associated field label.


Volume

basic.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/basic.tsx"
import { Form, FormField, Slider } from '@ifhover/leaf-ui';

export function SliderBasic() {
  return (
    <Form layout="vertical">
      <FormField label="Volume">
        <Slider name="volume" defaultValue={35} />
      </FormField>
    </Form>
  );
}
```

Expand code


## Examples

### Range

`range` uses a `[start, end]` tuple. The two thumbs cannot cross.


Budget range

range.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/range.tsx"
import { Form, FormField, Slider } from '@ifhover/leaf-ui';

export function SliderRange() {
  return (
    <Form layout="vertical">
      <FormField label="Budget range">
        <Slider aria-label="Budget range" name="budget" range defaultValue={[20, 70]} />
      </FormField>
    </Form>
  );
}
```

Expand code

### Discrete marks

`marks` adds clickable positions. Set `step={null}` to restrict selection to those positions.


Discount

02560100

marks.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/marks.tsx"
import { Form, FormField, Slider } from '@ifhover/leaf-ui';

export function SliderMarks() {
  return (
    <Form layout="vertical">
      <FormField label="Discount">
        <Slider
          defaultValue={25}
          step={null}
          marks={[
            { value: 0, label: '0' },
            { value: 25, label: '25' },
            { value: 60, label: '60' },
            { value: 100, label: '100' },
          ]}
        />
      </FormField>
    </Form>
  );
}
```

Expand code

### Read only and disabled

Both prevent changes. Use `readOnly` when the value must still be included in form submission.


Read only

Disabled

disabled.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/disabled.tsx"
import { Form, FormField, Slider } from '@ifhover/leaf-ui';

export function SliderDisabled() {
  return (
    <Form layout="vertical">
      <FormField label="Read only">
        <Slider defaultValue={45} readOnly />
      </FormField>
      <FormField label="Disabled">
        <Slider defaultValue={45} disabled />
      </FormField>
    </Form>
  );
}
```

Expand code

### Vertical

Set `vertical` to adjust the value along a vertical track.


Volume

vertical.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/vertical.tsx"
import { Form, FormField, Slider } from '@ifhover/leaf-ui';

export function SliderVertical() {
  return (
    <Form layout="vertical">
      <FormField label="Volume">
        <Slider vertical defaultValue={60} />
      </FormField>
    </Form>
  );
}
```

Expand code

[](#)
### Value tooltip

`tooltip` shows the value when the thumb is hovered or focused. Use `formatter` to customize its text.


Volume

tooltip.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/tooltip.tsx"
import { Form, FormField, Slider } from '@ifhover/leaf-ui';

export function SliderTooltip() {
  return (
    <Form layout="vertical">
      <FormField label="Volume">
        <Slider defaultValue={35} tooltip={{ formatter: (value) => `${value}%` }} />
      </FormField>
    </Form>
  );
}
```

Expand code

### Controlled value and completion

`onChange` reports each value change: a number for a single value or a tuple for a range. `onChangeComplete` runs after an interaction that changed the value, for example to save the final value. Reset controlled fields by also restoring `value` in application state.


Volume

Current value: 35

Committed: 35

Reset

controlled.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/slider/controlled.tsx"
import { Button, Flex, Form, FormField, Slider, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';

export function SliderControlled() {
  const [value, setValue] = useState(35);
  const [completed, setCompleted] = useState(35);

  return (
    <Form
      layout="vertical"
      onReset={() => {
        setValue(35);
        setCompleted(35);
      }}
    >
      <FormField label="Volume" help={`Current value: ${value}`}>
        <Slider
          name="volume"
          value={value}
          onChange={setValue}
          onChangeComplete={(next) => {
            if (typeof next === 'number') setCompleted(next);
          }}
        />
      </FormField>
      <Text tone="muted" role="status">
        Committed: {completed}
      </Text>
      <Flex align="center">
        <Button type="reset" variant="outline">
          Reset
        </Button>
      </Flex>
    </Form>
  );
}
```

Expand code

#### Forms and keyboard

Set `name` for native form submission. A range submits two values under the same name; read them with `FormData.getAll`. Uncontrolled fields restore their initial value on form reset. See [Form](https://ifhover.github.io/leaf-ui/en/components/form.md) for form integration.

Arrow keys move by one step, Home / End go to the bounds, and Shift + arrows or PageUp / PageDown move ten steps. Discrete mode moves between adjacent marks.


## API

### Slider API

Configure a value or range, numeric bounds and slider interaction.

| Property           | Type                                                               | Default                    | Description                                                                            |
| ------------------ | ------------------------------------------------------------------ | -------------------------- | -------------------------------------------------------------------------------------- |
| `min`              | `number`                                                           | `0`                        | Lower bound, 0 by default.                                                             |
| `max`              | `number`                                                           | `100`                      | Upper bound, 100 by default.                                                           |
| `step`             | `number \| null`                                                   | `1`                        | Increment per adjustment; null restricts selection to marks.                           |
| `marks`            | `readonly `[`SliderMark`](#slidermark)`[]`                         | `[]`                       | Marks                                                                                  |
| `vertical`         | `boolean`                                                          | `false`                    | Use a vertical layout                                                                  |
| `showValue`        | `boolean`                                                          | `false`                    | Show the value above the rail on hover or focus; hidden by default                     |
| `onChangeComplete` | `(value: number \| readonly [number, number]) => void`             | —                          | Called after an interaction that changed the value; returns a number or a range tuple. |
| `tooltip`          | `boolean \| { formatter?: (value: number) => ReactNode }`          | `false`                    | Show a thumb tooltip, optionally with custom formatting.                               |
| `range`            | `false \| true`                                                    | `false`                    | Range selection                                                                        |
| `value`            | `number \| readonly [number, number]`                              | —                          | Controlled value.                                                                      |
| `defaultValue`     | `number \| readonly [number, number]`                              | `range ? [min, max] : min` | Initial uncontrolled value; defaults to min, or \[min, max] in range mode.             |
| `onChange`         | `((value: number) => void) \| ((value: [number, number]) => void)` | —                          | Value changes; callback follows range mode                                             |
| `disabled`         | `boolean`                                                          | `false`                    | Disable interaction.                                                                   |
| `readOnly`         | `boolean`                                                          | `false`                    | Lock the value against typing, selection, clearing and removal.                        |
| `name`             | `string`                                                           | —                          | Field name in native FormData.                                                         |
| `form`             | `string`                                                           | —                          | ID of the associated native form.                                                      |

### SliderMark

Display a mark and label at a specific numeric position.

| Property | Type        | Default  | Description         |
| -------- | ----------- | -------- | ------------------- |
| `value`  | `number`    | Required | Mark value.         |
| `label`  | `ReactNode` | —        | Visible mark label. |
