Simple slider for React apps based on render prop
Via npm
npm i @starnavi/react-slider
Via yarn
yarn add @starnavi/react-slider
All this libarary provide only one component ReactSlider
which just calculate position of Grip
relative of Bar
(using css positioning), handle Grip
drag'n'drop and return new value. To implement such behavior ReactSlider
use its single children props function as v and provide getBarProps() and getGripProps() functions to it. It's meen that rendering and styling of component is up to you, you can nest structure as deep as you want and have full controll on component childrens and their props. ReactSlider
just provide behavior.
import React, { Component } from 'react';
import ReactSlider from '@starnavi/react-slider';
class App extends Component {
state = {
value: 50,
}
handleChange = (value) => {
this.setState({
value,
});
}
render() {
const { value } = this.state;
return (
<div className="App">
<ReactSlider
min={40}
max={120}
step={7}
value={value}
onChange={this.handleChange}
>
{({ getBarProps, getGripProps }) => (
<div>
<div
{...getBarProps()}
>
<button
{...getGripProps({
type: 'button',
})}
/>
</div>
</div>
)}
</ReactSlider>
{ value }
</div>
);
}
}
export default App;
render prop responsible for component rendering
current slider value
function called with new value as first argument
min value of slider
default
0
max value of slider
default
100
The value will be only a multiple of this number
default
1
if you need to get refs
of ReactElement with getProp* function you need just to privide callback refs as a props for getProp* function
<div
{...getBarProps({
ref: yourCallbackRef,
})}
>
- Provide all your custom props for Bar and Grip to its getProp* function as single first argument object.
- Don't change Grip positioning by hands, this component can use
position: absolute
andleft, right, top, bottom
css props for Grip andposition: relative
for Bar. - This component use
refs
of DOM element so getProp* functions should be called for getting props of React.CreateElement function with first argumentstring
(for examplediv
), it used React.createRef API and forward it asref
by default. If you want to forward it with another name (for example for style-components with version < 4) useprovideRefAs
prop in getProp* function withstring
(name forref
you want for exampleinnerRef
for style-components with version < 4)