Slider
HkSlider is the range control for settings and forms: a rail, a persistent thumb, a fill up to the thumb and optional tick dots, with the value snapped onto a step grid anchored at min. Clicking the rail jumps the thumb there and dragging moves it continuously; the arrow keys step, Page Up / Page Down step four times over and Home / End jump to the ends. It is controlled only — pass modelValue and wire update:modelValue.
- For a number typed or nudged with buttons, use HkNumberInput.
- For a number with its own caption line and validation message, wrap this control in HkFormField.
- For a two-state setting, use HkSwitch.
Import
;
use *; // Slider, SliderSize, Callback
use *; // rsx!, VNode
The Rust form is a plain function, Slider(SliderProps { … }). Two naming differences: Vue calls the value modelValue (a required prop) and the mirror calls it value: f64, defaulting to 0.0 so the props struct stays Default-constructible; and the live emit update:modelValue becomes on_input, which fires continuously while dragging, exactly like Vue.
Value, bounds and step
min and max bound the range (0 and 100 by default), and step is the snap grid — emitted values are always min + n * step. A non-positive step falls back to 1. Both the value passed in and the value reported back are clamped into [min, max] and snapped, so an off-grid modelValue is drawn on the nearest stop rather than where it was given. The Vue example is live: drag the thumbs and the readout follows.
Ticks, sizes and the value text
showTicks draws one dot per stop, with the stops already crossed marked active. The dots are only drawn for a readable stop count — fewer than two stops, or more than thirty-two, renders the rail bare, so a 0–100 range at step 1 stays clean. size is sm or md (the default); there is no lg step. formatValue supplies the spoken form of the value (the aria-valuetext), which is how a percentage or a unit gets announced instead of a bare number.
The Rust mirror takes the same formatter as format_value, a Callback<f64, String>: Callback::new wraps one function from a number to its spoken text.
Vertical, disabled and drag tracking
vertical stands the rail upright: values climb bottom to top, the thumb and ticks ride top instead of left, the fill's extent becomes height, and the root announces aria-orientation="vertical". The host sizes the height — the control has no intrinsic one — so the example gives it a stage. disabled dims the rail, takes it out of the Tab order and silences pointer and keyboard alike.
The Rust mirror tracks a drag through the root element's own mouse events: a drag that leaves the track stops emitting until the pointer comes back, and a release outside is recovered from the button state, so a later hover never writes a value. Vue attaches its move and release listeners to the window instead, which is why a drag keeps tracking past the element there.
API
Vue
Props
| Name | Type | Default | Required |
|---|---|---|---|
| modelValue | Number | — | ✓ |
| min | Number | 0 | — |
| max | Number | 100 | — |
| step | Number | 1 | — |
| disabled | Boolean | false | — |
| showTicks | Boolean | false | — |
| size | <code>String as () => "sm" | "md"</code> | "md" | — |
| vertical | Boolean | false | — |
| ariaLabel | String | undefined | — |
| formatValue | Function as PropType<(value: number) => string> | undefined | — |
Emits
| Event | Payload |
|---|---|
| update:modelValue | number |
Rust
Props — Slider
| Prop | Type | Default | Description |
|---|---|---|---|
| value | f64 | 0.0 | Controlled value (Vue: modelValue, required there; 0.0 here because Rust props need a Default). |
| min | f64 | 0.0 | — |
| max | f64 | 100.0 | — |
| step | f64 | 1.0 | Snap step; non-positive values fall back to 1 (Vue's safeStep). |
| disabled | bool | false | — |
| show_ticks | bool | false | Render one tick dot per stop (Vue: showTicks). |
| size | SliderSize | SliderSize::Md | — |
| vertical | bool | false | Stand the rail upright (Vue: vertical, default false): values climb bottom→top, the thumb and ticks ride top, the fill's extent is height, the root announces aria-orientation="vertical" and the pointer maps off the track's bottom edge. |
| aria_label | Option<String> | None | aria-label (Vue: ariaLabel); omitted when None. |
| format_value | Option<Callback<f64, String>> | None | aria-valuetext formatter (Vue: formatValue). |
| on_input | Option<EventHandler<f64>> | None | Emitted with the snapped value while interacting (Vue: update:modelValue, emitted continuously like Vue's drag/keyboard path). |
| class | String | "" | Vue attribute fallthrough onto the root <div>. |
| style | String | "" | — |
Accessibility
- The root is a <div role="slider"> with aria-valuemin, aria-valuemax, aria-valuenow and aria-valuetext, so the current value is read out on focus and on every change.
- aria-valuetext is the number's own rendering unless formatValue is given; pass one whenever the raw number is not what the user hears (a percentage, a unit, a word).
- Pass ariaLabel: the control has no visible caption of its own and would otherwise be announced only by its role and value.
- The root is focusable (tabindex="0", and -1 while disabled), and the arrows, Page Up / Page Down and Home / End all move it; aria-orientation="vertical" is set only in vertical mode, and the keyboard is unchanged there — Up and Right both increase.
- formatValue is called for the spoken value only; it never changes the number that is emitted.
Related components
- HkFormField — the caption and message shell for a control that needs a label line.
- HkNumberInput — the same scalar typed or stepped instead of dragged.
- HkSwitch — the two-state setting.
- HkCheckbox — the independent on/off choice.