Slider
An easily stylable range input.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Slider;
use function Phem\Ui\{SliderControl, SliderIndicator, SliderThumb, SliderTrack};
#[Component]
function SliderHeroDemo(): Node
{
return (
<Slider defaultValue={{ 25 }}>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb aria-label="Volume" />
</SliderTrack>
</SliderControl>
</Slider>
);
}
Examples#
Labeling a slider#
A single-thumb slider without a visible label (such as a volume control) can be labeled using aria-label on SliderThumb. A visible label can be created using SliderLabel. For a multi-thumb range slider with a visible label, add aria-label on each SliderThumb to distinguish them.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Slider;
use function Phem\Ui\{SliderControl, SliderIndicator, SliderLabel, SliderThumb, SliderTrack};
#[Component]
function SliderLabelingDemo(): Node
{
return (
<div className="flex flex-col gap-6">
<Slider defaultValue={{ 50 }}>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb aria-label="Volume" />
</SliderTrack>
</SliderControl>
</Slider>
<Slider defaultValue={{ 30 }}>
<SliderLabel>Volume</SliderLabel>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb />
</SliderTrack>
</SliderControl>
</Slider>
<Slider defaultValue={{ [25, 75] }}>
<SliderLabel>Price range</SliderLabel>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb index={{ 0 }} aria-label="Minimum price" />
<SliderThumb index={{ 1 }} aria-label="Maximum price" />
</SliderTrack>
</SliderControl>
</Slider>
</div>
);
}
Range slider#
To create a range slider, pass an array of values and place a SliderThumb for each value in the array, with the index of its value.
Thumbs can be configured to behave differently when they collide during pointer interactions using the thumbCollisionBehavior prop on Slider.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Slider;
use function Phem\Ui\{SliderControl, SliderIndicator, SliderThumb, SliderTrack};
#[Component]
function SliderRangeDemo(): Node
{
return (
<Slider defaultValue={{ [25, 45] }}>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb index={{ 0 }} aria-label="Minimum value" />
<SliderThumb index={{ 1 }} aria-label="Maximum value" />
</SliderTrack>
</SliderControl>
</Slider>
);
}
Steps#
Use the step prop to snap the value to multiples of a given increment, and largeStep to control the increment when using Shift + Arrow Up/Arrow Down.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Slider;
use function Phem\Ui\{SliderControl, SliderIndicator, SliderLabel, SliderThumb, SliderTrack, SliderValue};
#[Component]
function SliderStepsDemo(): Node
{
return (
<Slider className="w-56" defaultValue={{ 400 }} min={{ 100 }} max={{ 900 }} step={{ 100 }} largeStep={{ 200 }}>
<SliderLabel className="cursor-default font-normal">Playback speed</SliderLabel>
<SliderValue className="text-end" />
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb />
</SliderTrack>
</SliderControl>
</Slider>
);
}
Marks#
Render your own tick marks, positioning each mark along the track as a percentage of the value range.
Marks are independent of the value’s granularity—pair them with the step prop to snap the thumb to each mark.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Slider;
use function Phem\Ui\{SliderControl, SliderIndicator, SliderThumb, SliderTrack};
#[Component]
function SliderMarksDemo(): Node
{
$marks = [0, 25, 50, 75, 100];
return (
<Slider className="block w-56" defaultValue={{ 40 }}>
<SliderControl className="py-2.5">
<SliderTrack>
<SliderIndicator />
{% for mark in marks %}
<div key={{ mark }} aria-hidden="true" className="absolute top-1/2 h-2 w-px -translate-x-1/2 -translate-y-1/2 bg-neutral-950 dark:bg-white" style="left: {{ mark }}%"></div>
{% endfor %}
<SliderThumb aria-label="Volume" />
</SliderTrack>
</SliderControl>
<div className="relative h-4 select-none" aria-hidden="true">
{% for mark in marks %}
<span key={{ mark }} className="absolute -translate-x-1/2 text-xs whitespace-nowrap text-neutral-600 dark:text-neutral-400" style="left: {{ mark }}%">{{ mark }}</span>
{% endfor %}
</div>
</Slider>
);
}
Thumb alignment#
Set thumbAlignment="edge" to inset the thumb such that its edge aligns with the edge of the control when the value is at min or max, without overflowing the control like the default "center" alignment.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Slider;
use function Phem\Ui\{SliderControl, SliderIndicator, SliderThumb, SliderTrack};
#[Component]
function SliderThumbAlignmentDemo(): Node
{
return (
<Slider thumbAlignment="edge" defaultValue={{ 25 }}>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb aria-label="Volume" />
</SliderTrack>
</SliderControl>
</Slider>
);
}
Vertical#
Set orientation to Orientation::Vertical on Slider to build a vertical slider.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Orientation, Slider};
use function Phem\Ui\{SliderControl, SliderIndicator, SliderThumb, SliderTrack};
#[Component]
function SliderVerticalDemo(): Node
{
return (
<Slider orientation={{ Orientation.Vertical }} defaultValue={{ 35 }}>
<SliderControl className="h-32">
<SliderTrack>
<SliderIndicator />
<SliderThumb aria-label="Volume" />
</SliderTrack>
</SliderControl>
</Slider>
);
}
Form integration#
To use a slider in a form, pass the slider name to Slider. For grouped multi-thumb range sliders in forms, Fieldset can provide the shared visible label while each thumb keeps its own aria-label.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Field, Fieldset, Slider};
use function Phem\Ui\{FieldsetLegend, Form, SliderControl, SliderIndicator, SliderLabel, SliderThumb, SliderTrack};
#[Component]
function SliderFormDemo(): Node
{
return (
<Form className="items-center gap-8">
<Slider name="volume" defaultValue={{ 50 }}>
<SliderLabel>Volume</SliderLabel>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb />
</SliderTrack>
</SliderControl>
</Slider>
<Field>
<Fieldset>
<FieldsetLegend className="text-sm">Price range</FieldsetLegend>
<Slider name="price[]" defaultValue={{ [25, 75] }}>
<SliderControl>
<SliderTrack>
<SliderIndicator />
<SliderThumb index={{ 0 }} aria-label="Minimum price" />
<SliderThumb index={{ 1 }} aria-label="Maximum price" />
</SliderTrack>
</SliderControl>
</Slider>
</Fieldset>
</Field>
</Form>
);
}
API reference#
Import the components of the family:
use Phem\Ui\Slider;
use function Phem\Ui\{SliderLabel, SliderValue, SliderControl, SliderTrack, SliderIndicator, SliderThumb};Slider#
A value, or a range, on a track: Base UI Slider.Root, on the phem-slider controller.
defaultValue is a number for one thumb, or a list for a range: put one SliderThumb for each value, with its index. The server render places the thumbs, so they do not move when the controller starts.
| Prop | Type | Default |
|---|---|---|
defaultValue | float|array | 0 |
min | float | 0 |
max | float | 100 |
step | float | 1 |
largeStep | float | 10 |
minStepsBetweenValues | float | 0 |
orientation | 'horizontal' | 'vertical' | Orientation::Horizontal |
disabled | bool | false |
name | ?string | null |
thumbAlignment | string | 'center' |
thumbCollisionBehavior | string | 'push' |
format | ?array | null |
locale | ?string | null |
children | ?Html | null |
...attrs | array | [] |
SliderValue#
The formatted values.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
SliderControl#
The area that takes the presses and the drags.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SliderTrack#
The bar of the slider: it holds the SliderIndicator and the SliderThumb parts.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
SliderIndicator#
The filled part of the track: from the start to the value, or between the values of a range.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
SliderThumb#
A thumb, with a native range input for the keyboard, the ARIA and the form. Give it an aria-label.
| Prop | Type | Default |
|---|---|---|
index | int | 0 |
name | ?string | null |
...rest | mixed | — |
SliderLabel#
The label of the slider.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/Slider.phem.