Slider
A value, or a range with two thumbs, set by dragging or with the keyboard.
Example
<div class="stack"> <focsd-slider label="Volume" value="40" value-text="{value}%"></focsd-slider> <focsd-slider label="Price" range value-low="20" value-high="80" step="5" value-text="€{value}"></focsd-slider></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-slider.js';</script>
<focsd-slider label="Volume" value="40" value-text="{value}%"></focsd-slider>import { Slider } from '@focsd-dev/react';
export function Example() { return <Slider onChange={(e) => console.log(e.target)} label="Volume" value="40" value-text="{value}%"></Slider>;}<script setup>import '@focsd-dev/components/focsd-slider.js';</script>
<template> <focsd-slider @change="onChange" label="Volume" value="40" value-text="{value}%"></focsd-slider></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-slider.js';
// template<focsd-slider (change)="onChange($event)" label="Volume" value="40" value-text="{value}%"></focsd-slider><script> import '@focsd-dev/components/focsd-slider.js';</script>
<focsd-slider onchange={(e) => console.log(e.target)} label="Volume" value="40" value-text="{value}%"></focsd-slider>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | number | 50 |
min | min | number | 0 |
max | max | number | 100 |
step | step | number | 1 |
range | range | boolean | false |
value-low | valueLow | number | 0 |
value-high | valueHigh | number | 100 |
value-text | valueText | string | — |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
stepUp(n) | Move the value up by n steps, clamped to the range. Fires no events, like native. |
stepDown(n) | Move the value down by n steps, clamped to the range. Fires no events, like native. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change to the value or state. |
CSS parts
Style the inside of the component with ::part(), for example focsd-slider::part(layout).
| Part | Description |
|---|---|
layout | The wrapper that places the label, the body and the messages. |
body | The track, with the hint and message below it. |
fill | The fill element. |
header | The label and value row (above the track), or only the value when the label sits beside. |
label | The label element. |
output | The visible value next to the label. |
hint | The author's hint. |
message | The shown error, warning or success message. |
message-icon | The icon in the message. |
announcer | The visually hidden live region. |
thumb | The thumb element. |
track | The track element. |
Accessibility
- Do not set
readonlyhere: like a native checkbox or select it has no effect, and HTML skips validation for any custom form control with areadonlyattribute. Usedisabled, or show the value as text. valueAsNumber,stepUp()andstepDown()work as on a native range input.- Each thumb is
role="slider"witharia-valuenow/min/maxandaria-valuetextfrom thevalue-texttemplate. - Range thumbs are named "Minimum Price" and "Maximum Price" and cannot cross.
- Form-associated: participates in
FormData, validation, reset and fieldsetdisabledlike a native control.
Keyboard
| Key | Action |
|---|---|
| Arrow keys | Step by step |
| Page Up / Down | Step by 10 × step |
| Home / End | Jump to min / max |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.