Socket
Socket
Sign inDemoInstall

react-switch-selector

Package Overview
Dependencies
49
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-switch-selector

Tiny switch/toggle component for React️ ⚛


Version published
Weekly downloads
4.9K
decreased by-2.44%
Maintainers
1
Install size
3.45 MB
Created
Weekly downloads
 

Readme

Source

react-switch-selector · downloads version

Storybook

https://gr34se.github.io/react-switch-selector/

Examples

Default

ReactSwitchSelector

Custom colors

ReactSwitchSelector

Outer border-radius

ReactSwitchSelector

Installation

npm install react-switch-selector --save

or

yarn add react-switch-selector

Usage

SwitchSelector will stretch to fill its parent size (both width and height), so it's required create dedicated div container.

import SwitchSelector from "react-switch-selector";
const options = [
   {
       label: <span>Foo</span>,
       value: {
            foo: true
       },
       selectedBackgroundColor: "#0097e6",
   },
   {
       label: "Bar",
       value: "bar",
       selectedBackgroundColor: "#fbc531"
   }
];

const onChange = (newValue) => {
    console.log(newValue);
};

const initialSelectedIndex = options.findIndex(({value}) => value === "bar");

return (
    <div className="your-required-wrapper" style={{width: 100, height: 30}}>
        <SwitchSelector
            onChange={onChange}
            options={options}
            initialSelectedIndex={initialSelectedIndex}
            backgroundColor={"#353b48"}
            fontColor={"#f5f6fa"}
        />
    </div>
);

Props

Prop nameTypeDefaultRequiredNote
optionsArray of OptionType[]trueOptions array to render. Each item has a label, value and optional styling props
onChangeFunction(v) => (console.log(v))trueonChange callback that returns selected Option's value
namestringundefinedfalseHTML input name. Needed if you want to have multiple instances of the component
initialSelectedIndexnumber0falseInitially selected index of options array
forcedSelectedIndexnumberundefinedfalseForce selectedIndex with this prop (can be also used to resetting the toggle)
borderstring/number0falseBorder of wrapping div
backgroundColorstring#ecf0f1falseBackground color of wrapping div
selectedBackgroundColorstring#2ecc71falseBackground of selected Option
wrapperBorderRadiusnumber/CSS.Property.BorderRadius20falseBorder radius of wrapping div
optionBorderRadiusnumber/CSS.Property.BorderRadius18falseBorder radius of Option component
fontSizenumber14falseFont size of Option's label
fontColorstring#000falseFont color of Option's label
selectedFontColorstring#ffffalseFont color of selected Option's label
selectionIndicatorMarginnumber2falseInner px margin of selected option indicator
disabledbooleanfalsefalseDisabling the toggle
OptionType
Property nameTypeDefaultRequiredNote
labelstring/number/ReactElement/HTMLElementundefinedtrueOption's label
valueanyundefinedtrueOption's value that is returned by onChange callback
selectedBackgroundColorstringundefinedfalseBackground of this selected Option
fontColorstringundefinedfalseFont color of this Option's label
selectedFontColorstringundefinedfalseFont color of this selected Option's label
Overriding styles by pure css classes is available with react-switch-selector prefix:
  • react-switch-selector-wrapper - component root div
  • react-switch-selector-disabled - toggle in disabled state
  • react-switch-selector-option - each option
  • react-switch-selector-option-selected - option in "selected" state
  • react-switch-selector-option-unselected - each option in "unselected" state
  • react-switch-selector-option-label - each option item's label

Keywords

FAQs

Last updated on 07 Mar 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc