Slider
Choose a number or range by sliding.
Basic usage
Choose a numeric value with the slider and provide an associated field label.
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.
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.
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.
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.
import { Form, FormField, Slider } from '@ifhover/leaf-ui';
export function SliderVertical() {
return (
<Form layout="vertical">
<FormField label="Volume">
<Slider vertical defaultValue={60} />
</FormField>
</Form>
);
}
tooltip shows the value when the thumb is hovered or focused. Use formatter to customize its text.
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.
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>
);
}
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.
SliderMark
Display a mark and label at a specific numeric position.