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, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/en/components/slider.md.

Slider

Choose a number or range by sliding.

Basic usage

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

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>
  );
}

Examples

Range

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

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>
  );
}

Discrete marks

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

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>
  );
}

Read only and disabled

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

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>
  );
}

Vertical

Set vertical to adjust the value along a vertical track.

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>
  );
}

Value tooltip

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

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>
  );
}

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.

Current value: 35
Committed: 35
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>
  );
}

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 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.

PropertyTypeDefaultDescription
minnumber0Lower bound, 0 by default.
maxnumber100Upper bound, 100 by default.
stepnumber | null1Increment per adjustment; null restricts selection to marks.
marksreadonly SliderMark[][]Marks
verticalbooleanfalseUse a vertical layout
showValuebooleanfalseShow 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.
tooltipboolean | { formatter?: (value: number) => ReactNode }falseShow a thumb tooltip, optionally with custom formatting.
rangefalse | truefalseRange selection
valuenumber | readonly [number, number]—Controlled value.
defaultValuenumber | readonly [number, number]range ? [min, max] : minInitial 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
disabledbooleanfalseDisable interaction.
readOnlybooleanfalseLock the value against typing, selection, clearing and removal.
namestring—Field name in native FormData.
formstring—ID of the associated native form.

SliderMark

Display a mark and label at a specific numeric position.

PropertyTypeDefaultDescription
valuenumberRequiredMark value.
labelReactNode—Visible mark label.