import React from 'react'
import { useRangeSlider, TRangeTuple } from '@strv/react-sliders'
const RangeSliderExample = () => {
const [value, setValue] = React.useState<TRangeTuple>([0, 100])
const { getRailProps, getTrackProps, getMinHandleProps, getMaxHandleProps } = useRangeSlider({
value,
min: 0,
max: 100,
onChange: setValue,
})
return (
<div className="range-slider-container">
<span className="range-slider-rail" {...getRailProps()} />
<span className="range-slider-track" {...getTrackProps()} />
<span className="range-slider-handle" {...getMinHandleProps()} />
<span className="range-slider-handle" {...getMaxHandleProps()} />
</div>
)
}
import React from 'react'
import { useRangeSlider, TRangeTuple, IRangeMarker } from '@strv/react-sliders'
const markers: IRangeMarker = [{ value: 0 }, { value: 50 }, { value: 100 }]
const RangeSliderExample = () => {
const [value, setValue] = React.useState<TRangeTuple>([0, 100])
const { getRailProps, getTrackProps, getMinHandleProps, getMaxHandleProps } = useRangeSlider({
value,
min: 0,
max: 100,
onChange: setValue,
})
return (
<div className="range-slider-container">
<span className="range-slider-rail" {...getRailProps()} />
<span className="range-slider-track" {...getTrackProps()} />
{markers.map((marker) => {
const { style, isInRange } = getMarkerProps(marker)
return (
<React.Fragment key={`marker-${marker.value}`}>
<span className="marker-label" style={style}>
{marker.label}
</span>
<span
className="marker"
style={{
...style,
backgroundColor: isInRange ? 'red' : 'grey',
}}
/>
</React.Fragment>
)
})}
<span className="range-slider-handle" {...getMinHandleProps()} />
<span className="range-slider-handle" {...getMaxHandleProps()} />
</div>
)
}
[number, number]
| required
The controlled value of the range slider.
number
| required
The minimum allowed value.
number
| required
The maximum allowed value.
(value: [number, number]) => void
| required
Called each time the value
has changed. Value can be changed by dragging the handle (either by mouse or by touching) or by using keyboard.
Supported keys:
Key | Action |
---|---|
Arrow Up | value + step |
Arrow Right | value + step |
Arrow Down | value - step |
Arrow Left | value - step |
Page Up | value + 10 * step |
Page Down | value - 10 * step |
End | max |
Home | min |
number
| optional, defaults to1
Granularity of values on which the slider can step through.
(value: number) => string
| optional, no useful default
Formatting function which is used to format the value into more human readable format. The formatted value is then supplied to getHandleProps
where it sets "aria-valuetext"
property.
This method should be applied to a rail element. It will attach a ref
, which is used in mouse & touch movement calculations.
NOTE: Rail bar represents the whole line upon which the slider can move.
NOTE: Must be attached to the actual rail element to properly calculate mouse or touch dragging.
This method should be applied to a track element. It will attach a ref
, which is used in a mouse & touch movement calculations.
NOTE: Track bar represents the actual sliders value.
NOTE: Must be attached to the actual track element to properly calculate mouse or touch dragging.
This method should be applied to a handle element. It will attach a ref
, which is used in a mouse & touch movement calculations. It will pass A11y properties to the handle such as "aria-valuenow"
, "aria-valuetext"
, "aria-valuemin"
and "aria-valuemax"
and binds event handlers to control the value.
NOTE: Must be attached to the proper handle elements to enable range slider functionality.
This method should be applied to a marker element. It will return a style
property containing positioning info and isInRange
property which can be used for highlighting the markers if they are in current value range.
NOTE: Using this prop getter is optional.
marker: { value: number, label?: string }