New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-toggle-slider

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-toggle-slider

A highly customizable React toggle slider component.

0.4.1
latest
Source
npm
Version published
Maintainers
0
Created
Source

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>
    );
}

Keywords

react

FAQs

Package last updated on 18 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts