Slider
A Slider picks a number from a range. A RangeSlider picks a start and an end, and a MultiSlider takes any number of handles.
Usage#
import { MultiSlider, MultiSliderHandle, RangeSlider, Slider } from "@clawscale/react";<Slider max={32} value={workers} onChange={setWorkers} />Sliders are controlled. Pass value and update it in onChange. onRelease fires once when the user lets go of a handle.
Examples#
Single value#
min, max and stepSize define the scale, and labelStepSize spaces the labels. Name the handle with handleHtmlProps.
import { Slider } from "@clawscale/react";
import { useState } from "react";
export default function SliderBasic() {
const [workers, setWorkers] = useState(12);
return (
<Slider
handleHtmlProps={{ "aria-label": "Worker count" }}
labelStepSize={8}
max={32}
min={0}
value={workers}
onChange={setWorkers}
/>
);
}Range#
RangeSlider takes a [start, end] pair and fills the track between the two handles.
import { RangeSlider } from "@clawscale/react";
import { useState } from "react";
export default function SliderRange() {
const [replicas, setReplicas] = useState<[number, number]>([4, 16]);
return (
<RangeSlider
handleHtmlProps={{ start: { "aria-label": "Minimum replicas" }, end: { "aria-label": "Maximum replicas" } }}
labelStepSize={8}
max={32}
min={0}
value={replicas}
onChange={setReplicas}
/>
);
}Multiple handles#
MultiSlider renders one MultiSliderHandle per value. intentBefore and intentAfter color the track on each side of a handle, which suits threshold bands.
import { MultiSlider, MultiSliderHandle } from "@clawscale/react";
import { useState } from "react";
export default function SliderMulti() {
const [warning, setWarning] = useState(70);
const [critical, setCritical] = useState(90);
return (
<MultiSlider labelStepSize={20} max={100} min={0}>
<MultiSliderHandle
htmlProps={{ "aria-label": "Disk usage warning threshold" }}
intentAfter="warning"
intentBefore="success"
value={warning}
onChange={setWarning}
/>
<MultiSliderHandle
htmlProps={{ "aria-label": "Disk usage critical threshold" }}
intentAfter="danger"
value={critical}
onChange={setCritical}
/>
</MultiSlider>
);
}Vertical#
vertical draws the track from bottom to top.
import { Slider } from "@clawscale/react";
import { useState } from "react";
export default function SliderVertical() {
const [cpuLimit, setCpuLimit] = useState(65);
return (
<Slider
handleHtmlProps={{ "aria-label": "CPU limit" }}
labelStepSize={25}
max={100}
min={0}
value={cpuLimit}
vertical
onChange={setCpuLimit}
/>
);
}Label renderer#
labelRenderer formats labels, for example as a percentage or in milliseconds. Its second argument tells you when the label is the handle's own tooltip.
import { Slider } from "@clawscale/react";
import { useState } from "react";
export default function SliderLabelRenderer() {
const [sampleRate, setSampleRate] = useState(0.25);
const [timeoutMs, setTimeoutMs] = useState(1500);
return (
<>
<Slider
handleHtmlProps={{ "aria-label": "Sample rate" }}
labelRenderer={(value) => `${Math.round(value * 100)}%`}
labelStepSize={0.25}
max={1}
min={0}
stepSize={0.05}
value={sampleRate}
onChange={setSampleRate}
/>
<Slider
handleHtmlProps={{ "aria-label": "Request timeout" }}
labelRenderer={(value, opts) => (opts?.isHandleTooltip ? `${value} ms` : `${value / 1000} s`)}
labelStepSize={1000}
max={5000}
min={0}
stepSize={100}
value={timeoutMs}
onChange={setTimeoutMs}
/>
</>
);
}Props interface#
interface SliderProps extends SliderBaseProps@clawscale/react| Props | Description |
|---|---|
children | React.ReactNodeSliderBaseProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
disabled | boolean = falseWhether the slider is non-interactive. Inherited from SliderBaseProps |
handleHtmlProps | HandleHtmlPropsA limited subset of HTML props to apply to the slider Handle |
initialValue | number = 0Initial value of the slider. This determines the other end of the track fill: from initialValue to value. |
intent | IntentVisual intent color to apply to element. Inherited from IntentProps |
labelPrecision | number = inferred from stepSizeNumber of decimal places to use when rendering label value. Default value is the number of decimals used in the Inherited from stepSize prop. This prop has _no effect_ if you supply a custom labelRenderer callback.SliderBaseProps |
labelRenderer | boolean | ((value: number, opts?: { isHandleTooltip: boolean; }) => string | React.JSX.Element) = trueCallback to render a single label. Useful for formatting numbers as currency or percentages. If Inherited from true, labels will use number value formatted to labelPrecision decimal places. If false, labels will not be shown.SliderBaseProps |
labelStepSize | number = inferred (if labelStepSize is undefined)Increment between successive labels. Must be greater than zero. Inherited from SliderBaseProps |
labelValues | readonly number[]Array of specific values for the label placement. This prop is mutually exclusive with Inherited from labelStepSize.SliderBaseProps |
max | number = 10Maximum value of the slider. Value must be a finite number. Inherited from SliderBaseProps |
min | number = 0Minimum value of the slider. Value must be a finite number. Inherited from SliderBaseProps |
onChange | (value: number): voidCallback invoked when the value changes. |
onRelease | (value: number): voidCallback invoked when the handle is released. |
showTrackFill | boolean = trueWhether a solid bar should be rendered on the track between current and initial values, or between handles for Inherited from RangeSlider.SliderBaseProps |
stepSize | number = 1Increment between successive values; amount by which the handle moves. Must be greater than zero. Inherited from SliderBaseProps |
value | number = 0Value of slider. |
vertical | boolean = falseWhether to show the slider in a vertical orientation. Inherited from SliderBaseProps |
interface RangeSliderProps extends SliderBaseProps@clawscale/react| Props | Description |
|---|---|
children | React.ReactNodeSliderBaseProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
disabled | boolean = falseWhether the slider is non-interactive. Inherited from SliderBaseProps |
handleHtmlProps | { start?: HandleHtmlProps; end?: HandleHtmlProps; }HTML props to apply to the slider Handles |
intent | IntentVisual intent color to apply to element. Inherited from IntentProps |
labelPrecision | number = inferred from stepSizeNumber of decimal places to use when rendering label value. Default value is the number of decimals used in the Inherited from stepSize prop. This prop has _no effect_ if you supply a custom labelRenderer callback.SliderBaseProps |
labelRenderer | boolean | ((value: number, opts?: { isHandleTooltip: boolean; }) => string | React.JSX.Element) = trueCallback to render a single label. Useful for formatting numbers as currency or percentages. If Inherited from true, labels will use number value formatted to labelPrecision decimal places. If false, labels will not be shown.SliderBaseProps |
labelStepSize | number = inferred (if labelStepSize is undefined)Increment between successive labels. Must be greater than zero. Inherited from SliderBaseProps |
labelValues | readonly number[]Array of specific values for the label placement. This prop is mutually exclusive with Inherited from labelStepSize.SliderBaseProps |
max | number = 10Maximum value of the slider. Value must be a finite number. Inherited from SliderBaseProps |
min | number = 0Minimum value of the slider. Value must be a finite number. Inherited from SliderBaseProps |
onChange | (value: NumberRange): voidCallback invoked when the range value changes. |
onRelease | (value: NumberRange): voidCallback invoked when a handle is released. |
showTrackFill | boolean = trueWhether a solid bar should be rendered on the track between current and initial values, or between handles for Inherited from RangeSlider.SliderBaseProps |
stepSize | number = 1Increment between successive values; amount by which the handle moves. Must be greater than zero. Inherited from SliderBaseProps |
value | NumberRange = [0, 10]Range value of slider. Handles will be rendered at each position in the range. |
vertical | boolean = falseWhether to show the slider in a vertical orientation. Inherited from SliderBaseProps |
interface MultiSliderProps extends SliderBaseProps@clawscale/react| Props | Description |
|---|---|
children | React.ReactNodeSliderBaseProps |
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
defaultTrackIntent | IntentDefault intent of a track segment, used only if no handle specifies intentBefore/After. |
disabled | boolean = falseWhether the slider is non-interactive. Inherited from SliderBaseProps |
intent | IntentVisual intent color to apply to element. Inherited from IntentProps |
labelPrecision | number = inferred from stepSizeNumber of decimal places to use when rendering label value. Default value is the number of decimals used in the Inherited from stepSize prop. This prop has _no effect_ if you supply a custom labelRenderer callback.SliderBaseProps |
labelRenderer | boolean | ((value: number, opts?: { isHandleTooltip: boolean; }) => string | React.JSX.Element) = trueCallback to render a single label. Useful for formatting numbers as currency or percentages. If Inherited from true, labels will use number value formatted to labelPrecision decimal places. If false, labels will not be shown.SliderBaseProps |
labelStepSize | number = inferred (if labelStepSize is undefined)Increment between successive labels. Must be greater than zero. Inherited from SliderBaseProps |
labelValues | readonly number[]Array of specific values for the label placement. This prop is mutually exclusive with Inherited from labelStepSize.SliderBaseProps |
max | number = 10Maximum value of the slider. Value must be a finite number. Inherited from SliderBaseProps |
min | number = 0Minimum value of the slider. Value must be a finite number. Inherited from SliderBaseProps |
onChange | (values: number[]): voidCallback invoked when a handle value changes. Receives handle values in sorted order. |
onRelease | (values: number[]): voidCallback invoked when a handle is released. Receives handle values in sorted order. |
showTrackFill | boolean = trueWhether a solid bar should be rendered on the track between current and initial values, or between handles for Inherited from RangeSlider.SliderBaseProps |
stepSize | number = 1Increment between successive values; amount by which the handle moves. Must be greater than zero. Inherited from SliderBaseProps |
vertical | boolean = falseWhether to show the slider in a vertical orientation. Inherited from SliderBaseProps |
interface HandleProps extends Props@clawscale/react| Props | Description |
|---|---|
className | stringA space-delimited list of class names to pass along to a child element. Inherited from Props |
htmlProps | HandleHtmlPropsA limited subset of HTML props to apply to the rendered <span> element. |
intentAfter | IntentIntent for the track segment immediately after this handle, taking priority over intentBefore. |
intentBefore | IntentIntent for the track segment immediately before this handle. |
interactionKind | HandleInteractionKind = "lock"How this handle interacts with other handles. |
onChange | (newValue: number) => voidCallback invoked when this handle's value is changed due to a drag interaction. Note that "push" interactions can cause multiple handles to update at the same time. |
onRelease | (newValue: number) => voidCallback invoked when this handle is released (the end of a drag interaction). |
trackStyleAfter | CSSPropertiesStyle to use for the track segment immediately after this handle, taking priority over trackStyleBefore. |
trackStyleBefore | CSSPropertiesStyle to use for the track segment immediately before this handle |
type | HandleType = "full"Handle appearance type. |
valuerequired | numberNumeric value of this handle. |