0.4.0 • Published 3 years ago

react-toggle-slider v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

react-toggle-slider

npm version

A highly customizable React toggle slider component.

default square

Installation

Install using NPM:

npm install react-toggle-slider

Or install using Yarn:

yarn add react-toggle-slider

Usage

To add the component, simply import ToggleSlider and use it in your app.

import { ToggleSlider }  from "react-toggle-slider";

function App() {
    return (
        <div>
            <ToggleSlider/>
        </div>
    );
}

The slider works without any options, but it can be heavily customized.

Options

OptionDefaultDescription
activefalseInitial state
draggabletrueWhether or not the handle can be dragged
onToggleundefinedFunction to call when slider is toggled, passes active state as parameter
padding5Padding between the handle and the sides of the bar
flipfalseHandle starts of right instead of left
transitionDuration100msTransition duration of the slider
handleSize18Diameter of the handle
handleBorderRadius16Border radius of the handle
handleBackgroundColor#ffffffBackground color of the handle
handleBackgroundColorActiveundefinedBackground color of the handle while active
handleStylesundefinedExtra styles object to be applied to the handle
handleStylesActiveundefinedExtra styles object to be applied to the handle while active
handleTransitionDurationundefinedTransition duration of the handle (overrides transitionDuration)
handleRendererundefinedReact node to completely replace the handle
handleRendererActiveundefinedReact node to completely replace the handle while active
barHeight26Height of the bar
barWidth48Width of the bar
barBorderRadius16Border radius of the bar
barBackgroundColor#dededeBackground color of the bar
barBackgroundColorActive#06b7e7Background color of the bar while active
barTransitionDurationundefinedTransition duration of the bar (overrides transitionDuration)
barTransitionTypefadeSpecify if the bar should fade or slide
barStylesundefinedExtra styles object to be applied to the bar
barStylesActiveundefinedExtra styles object to be applied to the bar while active
barRendererundefinedReact node to completely replace the bar
barRendererActiveundefinedReact node to completely replace the bar while active

If any options which refer to the active state of the slider are undefined, it will default to the inactive value.

Accessing the state of the slider

Callback

The slider's state can be accessed using a callback. This can be used with useState or something similar to represent the status on the page.

import { ToggleSlider }  from "react-toggle-slider";

function App() {

    const [active, setActive] = useState(false);

    return (
        <div>
            <ToggleSlider onToggle={state => setActive(state)}/>
            Slider is {active ? "active" : "inactive"}
        </div>
    );
}

Hook

The slider can also be used as a hook. This can be used to access the state of the slider without a callback. To do this, import the useToggleSlider function. Options can be passed into the function as an object.

import { useToggleSlider } from "react-toggle-slider"

function App() {

    const [toggleSlider, active] = useToggleSlider({barBackgroundColorActive: "red"});
    return (
        <div>
            { toggleSlider }
            Slider is {active ? "active" : "inactive"}
        </div>
    );
}
0.4.0

3 years ago

0.3.0

3 years ago

0.2.4

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago