# Slider

> A track and thumb that picks a number or range by dragging.

```tsx title="hero.tsx"
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from '@langgenius/dify-ui/slider'

export function SliderHeroDemo() {
  return (
    <Slider
      defaultValue={0.7}
      min={0}
      max={2}
      step={0.1}
      largeStep={0.5}
      locale="en-US"
      format={{ minimumFractionDigits: 1 }}
      className="w-full max-w-80 flex-col gap-1"
    >
      <div className="flex items-center justify-between gap-2">
        <SliderLabel>Temperature</SliderLabel>
        <SliderValue className="shrink-0 system-sm-medium text-text-secondary tabular-nums" />
      </div>
      <SliderControl>
        <SliderTrack>
          <SliderIndicator />
          <SliderThumb />
        </SliderTrack>
      </SliderControl>
      <div className="flex justify-between gap-2 system-xs-regular text-text-tertiary">
        <span>Precise</span>
        <span>Creative</span>
      </div>
    </Slider>
  )
}
```

## Usage guidelines

- Use `Slider` for a number or range picked by dragging, where the approximate position matters more than an exact value. Use `NumberField` for exact numbers.
- Name the slider with `SliderLabel`. In a range slider, also give each `SliderThumb` an `aria-label` so the two thumbs are distinguishable.
- Show the current value with `SliderValue` when the number matters to the decision. Format it with the root `format` option rather than formatting text by hand, and pass the app language to `locale`; without it, units and separators follow the browser's runtime locale.
- Set `step` to the precision the value needs; keyboard and pointer changes snap to it. `largeStep` sets the jump for `PageUp`, `PageDown` and `Shift` with an arrow key. It defaults to `10`, so set it for small ranges.
- Pass a tuple to `value` or `defaultValue` for a range and type the root, such as `Slider<readonly [number, number]>`, so `onValueChange` receives the same shape.

## Anatomy

```tsx
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from '@langgenius/dify-ui/slider'
```

```tsx
<Slider>
  <SliderLabel />
  <SliderValue />
  <SliderControl>
    <SliderTrack>
      <SliderIndicator />
      <SliderThumb />
    </SliderTrack>
  </SliderControl>
</Slider>
```

`SliderControl` is the pointer area, `SliderIndicator` fills the selected part of the track, and each `SliderThumb` renders a native range input.

## Examples

### Range

Render one `SliderThumb` per value with its `index` and an `aria-label`. `SliderValue` shows both ends, formatted by `format` in the given `locale`.

```tsx title="range.tsx"
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from '@langgenius/dify-ui/slider'

type LatencyRange = readonly [number, number]

export function SliderRangeDemo() {
  return (
    <Slider<LatencyRange>
      defaultValue={[2, 8]}
      min={0}
      max={30}
      locale="en-US"
      format={{ style: 'unit', unit: 'second', unitDisplay: 'narrow' }}
      className="w-full max-w-80 flex-col gap-1"
    >
      <div className="flex items-center justify-between gap-2">
        <SliderLabel>Latency</SliderLabel>
        <SliderValue className="shrink-0 system-sm-medium text-text-secondary tabular-nums" />
      </div>
      <SliderControl>
        <SliderTrack>
          <SliderIndicator />
          <SliderThumb index={0} aria-label="Minimum latency" />
          <SliderThumb index={1} aria-label="Maximum latency" />
        </SliderTrack>
      </SliderControl>
    </Slider>
  )
}
```

### Commit on release

Use `onValueCommitted` to trigger expensive work such as re-querying retrieval results only after the pointer is released or a keyboard step finishes, while `SliderValue` updates live during the drag.

```tsx title="committed.tsx"
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from '@langgenius/dify-ui/slider'
import * as React from 'react'

const scoreFormat = new Intl.NumberFormat('en-US', {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2,
})

export function SliderCommittedDemo() {
  const [committed, setCommitted] = React.useState(0.5)

  return (
    <div className="flex w-full max-w-80 flex-col gap-2">
      <Slider<number>
        defaultValue={0.5}
        min={0}
        max={1}
        step={0.05}
        largeStep={0.25}
        locale="en-US"
        format={{ minimumFractionDigits: 2, maximumFractionDigits: 2 }}
        onValueCommitted={setCommitted}
        className="flex-col gap-1"
      >
        <div className="flex items-center justify-between gap-2">
          <SliderLabel>Score threshold</SliderLabel>
          <SliderValue className="shrink-0 system-sm-medium text-text-secondary tabular-nums" />
        </div>
        <SliderControl>
          <SliderTrack>
            <SliderIndicator />
            <SliderThumb />
          </SliderTrack>
        </SliderControl>
      </Slider>
      <p className="system-xs-regular text-text-tertiary tabular-nums">
        Applied threshold: {scoreFormat.format(committed)}
      </p>
    </div>
  )
}
```

### Disabled

Set `disabled` on `Field` (or on `Slider` directly) to dim the slider and block pointer and keyboard input. Keep `FieldDescription` on the field to explain why it is locked; it stays readable and becomes the thumb's accessible description.

```tsx title="disabled.tsx"
import { Field, FieldDescription } from '@langgenius/dify-ui/field'
import {
  Slider,
  SliderControl,
  SliderIndicator,
  SliderLabel,
  SliderThumb,
  SliderTrack,
  SliderValue,
} from '@langgenius/dify-ui/slider'

export function SliderDisabledDemo() {
  return (
    <Field disabled className="w-full max-w-80">
      <Slider defaultValue={3} min={1} max={10} className="flex-col gap-1">
        <div className="flex items-center justify-between gap-2">
          <SliderLabel>Top K</SliderLabel>
          <SliderValue className="shrink-0 system-sm-medium text-text-secondary tabular-nums" />
        </div>
        <SliderControl>
          <SliderTrack>
            <SliderIndicator />
            <SliderThumb />
          </SliderTrack>
        </SliderControl>
      </Slider>
      <FieldDescription>Set by the knowledge base retrieval settings.</FieldDescription>
    </Field>
  )
}
```

## Keyboard

| Key                      | Action                                                                                     |
| ------------------------ | ------------------------------------------------------------------------------------------ |
| `ArrowRight`, `ArrowUp`  | Increases the focused thumb by one `step`.                                                 |
| `ArrowLeft`, `ArrowDown` | Decreases the focused thumb by one `step`.                                                 |
| `Shift` + arrow key      | Changes the focused thumb by `largeStep`.                                                  |
| `PageUp`, `PageDown`     | Increases or decreases by `largeStep`.                                                     |
| `Home`, `End`            | Moves the focused thumb to the minimum or maximum. A range thumb stops at the other thumb. |
| `Tab`                    | Moves focus between thumbs, then out of the slider.                                        |

## API reference

### Slider

Groups all parts of a single-value or range slider. Renders a `<div>` element.

Inherits `style`, `defaultValue`, `disabled`, `format`, `locale`, `max`, `min`, `minStepsBetweenValues`, `name`, `form`, `orientation`, `step`, `largeStep`, `thumbAlignment`, `thumbCollisionBehavior`, `value`, `onValueChange`, `onValueCommitted`, `className`, `render` from [Base UI Slider.Root](https://base-ui.com/react/components/slider#root).

**Data attributes**

| Attribute          | Description                                                                  |
| ------------------ | ---------------------------------------------------------------------------- |
| `data-dragging`    | Present while the user is dragging.                                          |
| `data-orientation` | Indicates the orientation of the slider.                                     |
| `data-disabled`    | Present when the slider is disabled.                                         |
| `data-valid`       | Present when the slider is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`     | Present when the slider is in an invalid state (when wrapped in Field.Root). |
| `data-touched`     | Present when the slider has been touched (when wrapped in Field.Root).       |
| `data-dirty`       | Present when the slider's value has changed (when wrapped in Field.Root).    |
| `data-focused`     | Present when the slider is focused (when wrapped in Field.Root).             |

### SliderControl

The clickable, interactive part of the slider. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Slider.Control](https://base-ui.com/react/components/slider#control).

**Data attributes**

| Attribute          | Description                                                                  |
| ------------------ | ---------------------------------------------------------------------------- |
| `data-dragging`    | Present while the user is dragging.                                          |
| `data-orientation` | Indicates the orientation of the slider.                                     |
| `data-disabled`    | Present when the slider is disabled.                                         |
| `data-valid`       | Present when the slider is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`     | Present when the slider is in an invalid state (when wrapped in Field.Root). |
| `data-touched`     | Present when the slider has been touched (when wrapped in Field.Root).       |
| `data-dirty`       | Present when the slider's value has changed (when wrapped in Field.Root).    |
| `data-focused`     | Present when the slider is focused (when wrapped in Field.Root).             |

### SliderIndicator

Visualizes the current value of the slider. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Slider.Indicator](https://base-ui.com/react/components/slider#indicator).

**Data attributes**

| Attribute          | Description                                                                  |
| ------------------ | ---------------------------------------------------------------------------- |
| `data-dragging`    | Present while the user is dragging.                                          |
| `data-orientation` | Indicates the orientation of the slider.                                     |
| `data-disabled`    | Present when the slider is disabled.                                         |
| `data-valid`       | Present when the slider is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`     | Present when the slider is in an invalid state (when wrapped in Field.Root). |
| `data-touched`     | Present when the slider has been touched (when wrapped in Field.Root).       |
| `data-dirty`       | Present when the slider's value has changed (when wrapped in Field.Root).    |
| `data-focused`     | Present when the slider is focused (when wrapped in Field.Root).             |

### SliderLabel

An accessible label that is automatically associated with the slider thumbs. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Slider.Label](https://base-ui.com/react/components/slider#label).

**Data attributes**

| Attribute                    | Description                                                                                                                                |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `data-touched`               | Present when the field has been touched.                                                                                                   |
| `data-dirty`                 | Present when the field value has changed from its initial value.                                                                           |
| `data-filled`                | Present when the field has a value.                                                                                                        |
| `data-focused`               | Present when the field is focused.                                                                                                         |
| `data-activethumbindex`      | The index of the active thumb.                                                                                                             |
| `data-disabled`              | Present when the component should ignore user interaction.                                                                                 |
| `data-dragging`              | Present when the thumb is currently being dragged.                                                                                         |
| `data-max`                   | The maximum value.                                                                                                                         |
| `data-min`                   | The minimum value.                                                                                                                         |
| `data-minstepsbetweenvalues` | The minimum steps between values in a range slider.                                                                                        |
| `data-orientation`           | The component orientation.                                                                                                                 |
| `data-step`                  | The step increment of the slider when incrementing or decrementing. It will snap to multiples of this value. Decimal values are supported. |

### SliderThumb

The draggable part of the slider at the tip of the indicator. Renders a `<div>` element and a nested `<input type="range">`.

Inherits `disabled`, `aria-valuetext`, `getAriaLabel`, `getAriaValueText`, `index`, `inputRef`, `tabIndex`, `className`, `render`, `style` from [Base UI Slider.Thumb](https://base-ui.com/react/components/slider#thumb).

**Data attributes**

| Attribute          | Description                                                                  |
| ------------------ | ---------------------------------------------------------------------------- |
| `data-index`       | Indicates the index of the thumb in range sliders.                           |
| `data-dragging`    | Present while the user is dragging.                                          |
| `data-orientation` | Indicates the orientation of the slider.                                     |
| `data-disabled`    | Present when the slider is disabled.                                         |
| `data-valid`       | Present when the slider is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`     | Present when the slider is in an invalid state (when wrapped in Field.Root). |
| `data-touched`     | Present when the slider has been touched (when wrapped in Field.Root).       |
| `data-dirty`       | Present when the slider's value has changed (when wrapped in Field.Root).    |
| `data-focused`     | Present when the slider is focused (when wrapped in Field.Root).             |

### SliderTrack

Contains the slider indicator and represents the entire range of the slider. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Slider.Track](https://base-ui.com/react/components/slider#track).

**Data attributes**

| Attribute          | Description                                                                  |
| ------------------ | ---------------------------------------------------------------------------- |
| `data-dragging`    | Present while the user is dragging.                                          |
| `data-orientation` | Indicates the orientation of the slider.                                     |
| `data-disabled`    | Present when the slider is disabled.                                         |
| `data-valid`       | Present when the slider is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`     | Present when the slider is in an invalid state (when wrapped in Field.Root). |
| `data-touched`     | Present when the slider has been touched (when wrapped in Field.Root).       |
| `data-dirty`       | Present when the slider's value has changed (when wrapped in Field.Root).    |
| `data-focused`     | Present when the slider is focused (when wrapped in Field.Root).             |

### SliderValue

Displays the current value of the slider as text. Renders an `<output>` element.

Inherits `children`, `className`, `render`, `style` from [Base UI Slider.Value](https://base-ui.com/react/components/slider#value).

**Data attributes**

| Attribute          | Description                                                                  |
| ------------------ | ---------------------------------------------------------------------------- |
| `data-dragging`    | Present while the user is dragging.                                          |
| `data-orientation` | Indicates the orientation of the slider.                                     |
| `data-disabled`    | Present when the slider is disabled.                                         |
| `data-valid`       | Present when the slider is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`     | Present when the slider is in an invalid state (when wrapped in Field.Root). |
| `data-touched`     | Present when the slider has been touched (when wrapped in Field.Root).       |
| `data-dirty`       | Present when the slider's value has changed (when wrapped in Field.Root).    |
| `data-focused`     | Present when the slider is focused (when wrapped in Field.Root).             |
