/** * 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` | `` | * | `control` | `
`, 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 ``, with the `` 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 `` 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`: `horizontal` or `vertical` * - `data-dragging`: a thumb is being dragged * - `data-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 `min` and `max`, 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) * * ```ts import.meta.vitest * 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 = /]*>/.exec(html) * if (input === null || !html.includes('