Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

RangeSlider

Since 0.1.0-alpha.22
Lets the user pick a span of numbers with two thumbs on one track.

A range slider picks a span, such as a price filter's lowest and highest prices. It's two native range inputs on one track, so both ends post with the form, with JavaScript off too. When the exact numbers matter, use two NumberFields.

Filtering by a range

Bind the slider to a NumberRange of any number type, such as NumberRange<int> or NumberRange<decimal>. It holds Start and End, both included, and Contains tests a value against them. The ends post as Price.Start and Price.End, so a [SupplyParameterFromForm] model binds them on a static page.

The scale runs from 0 to 100 by default. To change it, set Min and Max; to set the increments, set Step.

Static

Preview

Price
  • Travel noise-canceling · $140
  • Open-back studio · $210

Code

Usage.razor
<Form Model="Model" FormName="range-slider-usage" OnValidSubmit="Apply" MaxWidth="28.Rem" Grow>
    <RangeSlider @bind-Value="Model!.Price" Label="Price" Max="400" Step="10" ValueFormat="${0}" ValueTooltip="SliderTooltip.Always" ShowEnds />
    <Row><Button Submit>Show headphones</Button></Row>
    <List>
        @foreach (var item in _shown)
        {
            <ListItem>@item.Name · $@item.Price</ListItem>
        }
    </List>
</Form>

@code {
    [SupplyParameterFromForm(FormName = "range-slider-usage")]
    private Filter? Model { get; set; }

    private IReadOnlyList<Headphones> _shown = [];

    protected override void OnInitialized()
    {
        Model ??= new();
        Apply();
    }

    private void Apply() => _shown = Catalog.Where(h => Model!.Price.Contains(h.Price)).ToList();
}

Static: The thumbs stop at each other, and Show headphones posts both ends without a full reload.

Add this example to a page with the izzy CLI:

bash
izzy add component RangeSlider --at <file>:<line>

To show each thumb's value in a tooltip over it, set ValueTooltip to SliderTooltip.Active (while it's hovered, focused or dragged) or SliderTooltip.Always. To label the ends of the track, set ShowEnds; for marks, set Marks or ShowStepMarks, as on Slider. To set how values read, such as "${0}", set ValueFormat.

With no value, the thumbs sit at the ends of the scale, as a filter that hasn't narrowed anything.

Naming the thumbs

Screen readers hear the label and then each thumb's name, "Minimum" and "Maximum" by default. When other words fit better, such as "Earliest" and "Latest", set StartText and EndText.

Static

Preview

Delivery window

Someone will be home between these hours.

Code

Window.razor
<Form Model="Model" FormName="range-slider-window" MaxWidth="28.Rem" Grow>
    <RangeSlider @bind-Value="Model!.Window" Label="Delivery window" Min="7" Max="21" ValueFormat="{0}:00" ValueTooltip="SliderTooltip.Active" ShowEnds ShowStepMarks
                 StartText="Earliest" EndText="Latest" Hint="Someone will be home between these hours." />
</Form>

@code {
    [SupplyParameterFromForm(FormName = "range-slider-window")]
    private Delivery? Model { get; set; }

    protected override void OnInitialized() => Model ??= new();

    public sealed class Delivery
    {
        public NumberRange<int> Window { get; set; } = new(9, 17);
    }
}

When the thumbs meet

With IzzyBlazor's script, a thumb stops at the other, so the ends can be equal but never cross, and a click on the track moves the nearer thumb there. With JavaScript off, the thumbs can pass each other; when the form posts, the server binds the lower value as Start.

The server rejects an end outside Min and Max, so a crafted post can't widen the range. Before trusting a posted value, see Values a form post can change.

Vertical, live and disabled

To stand the slider upright, set Orientation to Orientation.Vertical; the lowest value is at the bottom, and Height sets the track's length, 10rem by default. On interactive pages, to update the bound value as a thumb moves, set Live; each move is a round trip. Size and Disabled work as on Slider, and so does a read-only Form: the slider is disabled, and hidden fields post both ends.

Mode support

What works in each mode, RangeSlider
FeatureStaticInteractive
Two native range inputs on one track: drag, arrow keys, Page Up/Down and Home/End; both ends posted with the formYesYes
The thumbs stop at each otherYesYes
The track fills between the thumbs, and the value tooltips followYesYes
A click on the track moves the nearer thumbYesYes
Screen readers hear each value as ValueFormat writes itYesYes
The bound value follows while dragging (Live)NoYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Without JavaScript

Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, the thumbs can pass each other (the server puts the ends in order when the form posts), the track doesn't fill between them, and a click on the track doesn't move a thumb.

Accessibility

  • It renders a fieldset named by the label, with two native range inputs (sliders to screen readers) named "Minimum" and "Maximum" (StartText, EndText).
  • Each thumb is its own tab stop, lower first; arrow keys, Page Up/Down and Home/End move it, and with script it stops at the other thumb.
  • Screen readers hear each value as ValueFormat writes it ("$20") once scripts run or on interactive pages; on a static page without script, the bare number.
  • Each slider reports the whole scale as its minimum and maximum, not the other thumb's value: the native input's scale is what places its thumb.
  • The value tooltips, the marks and the ends' labels are hidden from screen readers, which hear each slider's own value and scale.
  • Hint and errors are linked to the group.

Parameters

RangeSlider parameters
PropTypeDefault
MinThe lowest value either end can take.double?0
MaxThe highest value either end can take.double?100
StepThe step between values.double?1
ValueTooltipShows each thumb's value in a tooltip over it: SliderTooltip.Active while it's hovered, focused or held, SliderTooltip.Always all the time.SliderTooltipSliderTooltip.None
ShowEndsLabels the ends of the track with Min and Max, as ValueFormat writes them.boolfalse
MarksPoints to mark on the track, each a dot with an optional label under it, such as new(37, "37°C").IReadOnlyList<SliderMark>?
ShowStepMarksMarks every step with a dot.boolfalse
ValueFormatHow each end reads, with {0} for the number: "{0}%", "${0}".string"{0}"
StartTextThe lower thumb's name, in place of "Minimum".string?
EndTextThe upper thumb's name, in place of "Maximum".string?
OrientationOrientation.Horizontal, the default, runs from the lowest value at the start to the highest at the end; Orientation.Vertical stands upright, the lowest at the bottom, with the values shown under it.OrientationOrientation.Horizontal
HeightThe track's length when Orientation is vertical.Length?computed
LiveOn interactive pages, update the bound value while dragging rather than on release.boolfalse
@bind-ValueThe value, bound with @bind-Value.NumberRange<TNumber>?
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed.string?

See also

  • Slider: lets the user pick a number by dragging along a range.
  • NumberField: lets the user enter a number, with step buttons.
  • DateRangeField: lets the user choose a range of whole days, both ends included.
  • FilterForm: puts a search and filters in the URL for the page to read.

Connection lost

Rejoining the server…