Slider unstable
A controlled slider with one thumb or several. Pass the current value as
value, a number or an array of numbers, and update it when the slider
emits Moved. Your view supplies the markup; the component adds ARIA and
data attributes, handles pointer and keyboard input, and places the native
range input inside each thumb. It is a headless component, because it
keeps the thumb being dragged and the focused one.
Anatomy
| Part | Element |
|---|---|
root |
any element, role="group" |
value |
<output> |
control |
<div>, the area that takes pointer presses |
track |
any element inside the control |
indicator |
any element inside the track, the filled range |
thumb |
any element that can contain an <input>, with the <input type="range"> inside |
Each part takes the consumer's builder and returns it, so the consumer
goes on chaining and adds children with .children(...). thumb takes
two builders, { element, input, index }: element is the thumb you
see and drag, and input is the <input> that takes focus, the keys,
the screen reader and the form value. The thumb sets the input up as a
visually hidden range input and puts it inside element, before the
children you chain afterward. Because you hand over the input's builder,
what you set on it stays: its name, its aria-label, or a
Field's field.control or field.member. index picks the value the thumb shows, and is
required when value is an array.
The kit sets no layout. thumb gets --slider-thumb-position and
indicator gets --slider-indicator-start and
--slider-indicator-size. These CSS percentages are rounded to at most
3 decimal places and count from the left or, when vertical, from the bottom.
A horizontal right-to-left slider has min on the right; its percentages
still count from the left. The hidden input fills the
thumb with position: absolute, so give the thumb a position. For
dragging by touch, give the control touch-action: none.
Naming
Naming each thumb is the consumer's job, on its input. A Field names a
single thumb and adds a description, an error and the validity state:
draw its input with field.control(h.input). A disabled field disables the
input, but the slider's other parts follow the disabled prop, so pass
disabled: field.disabled as well.
In a Field, a range slider is the field's group. Draw the root with
slider.root(field.group(h.div)), the field's field.legend to name it, and each
thumb's input with field.member(h.input) and an aria-label of its own,
such as "Minimum". Each input then points at the description and the
error. The field is focused while either thumb has focus, and touched
once focus leaves the slider. Don't pass field.control to more than one
input, since that gives two elements one id.
Data attributes
On every part, each present with an empty value or absent, except
data-orientation:
data-orientation:horizontalorverticaldata-dragging: a thumb is being draggeddata-disabled
thumb also gets data-index. A Field's data attributes, such as
data-invalid and data-touched, go on its parts and on the input
field.control or field.member decorates.
Keyboard
Tab moves between the thumbs' inputs. On a focused thumb:
- ArrowRight and ArrowUp add
step; ArrowLeft and ArrowDown take it away, with Left and Right swapped on a horizontal right-to-left slider - Shift with an arrow, PageUp and PageDown move by
largeStep - Home and End go to
minandmax, or, with several thumbs, as far as the neighbouring thumb allows
A key moves to a value a step from min lands on, as the native input
does, so with step: 3 and largeStep: 5 PageUp from 0 goes to 6. A
max between two steps can't be reached: the last step below it is the
top. A value off the grid shows at its nearest step, as the native
input shows it, and values and the events report that step.
A key held with Alt, Ctrl or Meta is left to the browser. The slider
follows the nearest DirectionProvider above it or, without one, the
control's CSS direction. The input's keydown handler cancels the
default of the keys it handles only, so the input gets every keydown
while it has focus, and an ancestor's keydown handler never sees them.
To handle them, add a keydown handler to the input's builder.
Pointer
A press with the primary button on a thumb drags it from where it was grabbed. A press elsewhere on the control moves the nearest thumb there and drags it. The drag follows the pointer outside the control until it is released. The thumb being dragged gets focus without a focus ring, and the first key pressed on it shows the ring.
Events
Moved reports each value the user moves to, during a drag and for each
key. Committed reports the value once the user is done: when the
pointer is released and after each key. Both carry a number when
value is a number, and an array otherwise.
Forms
Each input submits its value under the name you give it. Place the
slider inside the form that should submit it.
Example A labelled volume slider whose value lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Field, Slider } from "effect/ui/kit"import { Effect, Schema } from "effect"
const PlayerMessage = UiSchema.TaggedEnum({ MovedVolume: { value: Schema.Number } })
const Player = Html.component({ name: "Player", model: Schema.Struct({ volume: Schema.Number }), message: PlayerMessage}).make({ init: () => ({ volume: 40 }), update: { MovedVolume: ($, { value }) => $.evolve({ volume: value }) }, view: (h, model) => Field(h, { view: (field) => field.root(h.div).children( field.label(h.label).children("Volume"), Slider(h, { props: { value: model.volume, disabled: field.disabled }, onEmit: { Moved: ({ value }) => PlayerMessage.MovedVolume({ value: value as number }), Committed: () => undefined }, view: (slider) => slider.root(h.div.class("slider")).children( slider.control(h.div.class("control")).children( slider.track(h.div.class("track")).children( slider.indicator(h.div.class("indicator")), slider.thumb({ element: h.div.class("thumb"), input: field.control(h.input).attr("name", "volume") }) ) ) ) }) ) })})
// The thumb holds a range input at the value, which the Field's label names.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Player, {}))const input = /<input id="([^"]+)" name="volume" type="range"[^>]*>/.exec(html)if (input === null || !html.includes('<label for="' + input[1] + '"')) throw new Error("the range input isn't labelled")if (!input[0].includes('value="40"') || !html.includes("--slider-thumb-position:40%")) throw new Error("the thumb isn't at 40")Components
A headless slider. Call it with the consumer's h, the props, and a
view closure that draws the parts, with a thumb for each value inside
the control.
Details
The parts also return values, the sorted values, dragging,
disabled, orientation and activeThumbIndex, the thumb whose
input has focus or -1, for markup that depends on them.
thumb({ element, input, index }) returns element with the input
inside it. The other parts each return the builder they were given. A
handler the consumer adds runs after the slider's own.
The slider's own attributes win over the ones the consumer set on the
builder before the part: role on root, the data attributes on every
part, and on the input type, min, max, step, value,
disabled, aria-orientation, aria-valuenow and aria-disabled,
which it removes. The thumb's and indicator's CSS variables and the
input's hiding style are added to the consumer's style, after it.
Every other attribute stays the consumer's.
A thumb whose index has no value throws when the view renders.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const Slider: Headless<{ readonly disabled?: boolean; readonly largeStep?: number; readonly max?: number; readonly min?: number; readonly minStepsBetweenValues?: number; readonly orientation?: "horizontal" | "vertical"; readonly step?: number; readonly thumbCollisionBehavior?: "push" | "none" | "swap"; readonly value: number | readonly Array<number>;}, { readonly _tag: "Committed"; readonly value: number | readonly Array<number>;} | { readonly _tag: "Moved"; readonly value: number | readonly Array<number>;}, { activeThumbIndex: number; control: Part<"div", [], never>; disabled: boolean; dragging: boolean; indicator: Part<string, [], never>; orientation: "horizontal" | "vertical"; root: Part<string, [], never>; thumb: <B extends PartTarget<string>, I extends PartTarget<"input">>(__namedParameters: { readonly element: B; readonly index?: number; readonly input: I; }) => WithRequirements<B, Requirements<I>> & Pick<Part<string, [index: number, input: Content<unknown, any>], never>, "~R">; track: Part<string, [], never>; value: Part<"output", [], never>; values: readonly Array<number>;}, never, never, never, never, never, never, { readonly active: number; readonly moved: number | readonly Array<number> | null; readonly pressed: number; readonly pressedValues: readonly Array<number>; readonly rtl: boolean;}, { readonly _tag: "ReleasedPointer";} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedKey"; readonly index: number; readonly key: string; readonly shift: boolean;} | { readonly _tag: "PressedPointer"; readonly fraction: number; readonly index: number; readonly onThumb: boolean;} | { readonly _tag: "MovedPointer"; readonly fraction: number;} | { readonly _tag: "UpdatedThumb"; readonly index: number; readonly value: string;} | { readonly _tag: "FocusedThumb"; readonly index: number;} | { readonly _tag: "BlurredThumb"; readonly index: number;}, QueryEntries<{}, {}>>Models
Schemas
SliderEvents
unstableWhat a slider emits. Moved asks for each value the user moves to, and
Committed reports the value once a drag or a key press is done. value is
a number when the value prop is, and an array otherwise.
Signature
declare const SliderEvents: TaggedEnum<{ Committed: TaggedStruct<"Committed", { readonly value: Union<readonly [Number, $Array<Number>]>; }>; Moved: TaggedStruct<"Moved", { readonly value: Union<readonly [Number, $Array<Number>]>; }>;}>SliderProps
unstableProps for the slider's value, its range and how its thumbs move.
Details
value: one number for one thumb, or an array with one number per thumb. The slider sorts the values and keeps them withinminandmax.minandmax:0and100by default.step:1by default. A drag lands on a multiple of it frommin.largeStep: what Shift with an arrow, PageUp and PageDown move by,10by default.minStepsBetweenValues: how many steps apart two thumbs stay,0by default. Part of a step counts as a whole one.orientation:"horizontal"by default.thumbCollisionBehavior: what a thumb dragged into another does."push", the default, pushes it along;"swap"passes it, and the drag goes on with the other thumb;"none"stops at it.disabled: disables every input, and the slider emits nothing.
Signature
declare const SliderProps: Struct<{ readonly disabled: optional<Boolean>; readonly largeStep: optional<Number>; readonly max: optional<Number>; readonly min: optional<Number>; readonly minStepsBetweenValues: optional<Number>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>; readonly step: optional<Number>; readonly thumbCollisionBehavior: optional<Literals<readonly ["push", "swap", "none"]>>; readonly value: Union<readonly [Number, $Array<Number>]>;}>