1.0.2 • Published 9 months ago

react-toggle-slider-switch v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

React Toggle Slider Switch

A customizable react toggle slider switch component. Simple to use with built-in label functionality and customization options.

Installation

To install the package, run:

Using npm:

npm install react-toggle-slider-switch

Using yarn:

yarn add react-toggle-slider-switch

Screenshot

Screenshot

Basic Usage

Here’s a basic example using a functional component with the ReactToggleSliderSwitch:

Copy code:

import React, { useState } from "react";
import ReactToggleSliderSwitch from "react-toggle-slider-switch";

const BasicExample: React.FC = () => {
  const [checked, setChecked] = useState<boolean>(false);

  const handleChange = (newChecked: boolean): void => {
    setChecked(newChecked);
  };

  return (
    <ReactToggleSliderSwitch
      checked={checked}
      onChange={handleChange}
      onColor="#22c55e"
      offColor="#d1d5db"
      checkedIcon={true}
      uncheckedIcon={true}
      height={28}
      width={70}
    />
  );
};

export default BasicExample;

Label Example

Here’s a custom example with additional styles, using the ReactToggleSliderSwitch component:

Copy code:

import React from "react";
import ReactToggleSliderSwitch from "react-toggle-slider-switch";

const LabelExample: React.FC = () => {
  const [checked, setChecked] = useState<boolean>(false);

  const handleChange = (newChecked: boolean): void => {
    setChecked(newChecked);
  };

  return (
    <ReactToggleSliderSwitch
      label="Label"
      checked={checked}
      onChange={handleChange}
      onColor="#22c55e"
      offColor="#d1d5db"
      checkedIcon={true}
      uncheckedIcon={true}
      height={28}
      width={70}
    />
  );
};

export default LabelExample;

Custom Example

Here’s an custom example with label using a functional component with the ReactToggleSliderSwitch:

Copy code:

import React from "react";
import ReactToggleSliderSwitch from "react-toggle-slider-switch";

const CustomExample: React.FC = () => {
  const [checked, setChecked] = useState<boolean>(false);

  const handleChange = (newChecked: boolean): void => {
    setChecked(newChecked);
  };

  return (
    <ReactToggleSliderSwitch
      label="Custom Label"
      checked={checked}
      onChange={handleChange}
      onColor="#0f0"
      offColor="#f00"
      height={28}
      width={70}
      checkedIcon={false}
      uncheckedIcon={false}
      switchBgClassName="custom-bg-class"
      switchHandleClassName="custom-handle-class"
      labelStyle={{ fontWeight: "bold", fontSize: "14px" }}
    />
  );
};

export default CustomExample;

Features

Customizable colors : Change the on and off switch colors.

Built-in label support : Automatically handles labels if you need one.

Drag & Click Behavior : You can click or drag to toggle the switch.

Custom icons : Supports custom icons for both checked and unchecked states.

Props

PropTypeDefaultDescription
checkedbooleanRequiredThe checked state of the switch. If true, the switch is set to checked.
onChange(checked: boolean, event: Event, id?: string) => voidRequiredCallback invoked when the user clicks or drags the switch. checked indicates the future state.
disabledbooleanfalseWhen true, the switch is non-interactive, and its colors are greyed out.
offColorstring"#d1d5db"The color of the switch when it is not checked. Accepts hex values (e.g., #888, #45abcd).
onColorstring"#22c55e"The color of the switch when it is checked. Accepts hex values (e.g., #080, #45abcd).
offHandleColorstring"#fff"The color of the handle when the switch is not checked. Accepts hex values (e.g., #fff, #45abcd).
onHandleColorstring"#fff"The color of the handle when the switch is checked. Accepts hex values (e.g., #fff, #45abcd).
outlinebooleanundefinedSpecifies if the handle should have an outline when focused.
handleWidthnumber35The width of the handle, measured in pixels.
labelstringundefinedThe label to display alongside the switch.
labelPosition"before" \| "after""after"Position of the label if the label is defined.
labelStyleReact.CSSPropertiesundefinedCustom styles for the label (e.g., fontSize, color, etc.).
handleDiameternumberundefinedDiameter of the handle, measured in pixels.
uncheckedHandleIconJSX.ElementundefinedCustom icon to display on the handle when the switch is not checked.
checkedHandleIconJSX.ElementundefinedCustom icon to display on the handle when the switch is checked.
uncheckedIconboolean \| JSX.ElementdefaultUncheckedIconIcon that will be shown when the switch is not checked. Set to false to show no icon.
checkedIconboolean \| JSX.ElementdefaultCheckedIconIcon that will be shown when the switch is checked. Set to false to show no icon.
boxShadowstring \| nullundefinedBox-shadow of the handle of the switch.
activeBoxShadowstring"0px 0px 2px 2px #3bf"Box-shadow of the handle when focused or active.
heightnumber28Height of the switch background in pixels.
widthnumber70Width of the switch background in pixels.
borderRadiusnumberundefinedBorder radius of the switch and handle.
classNamestringundefinedCustom className for the outer shell of the switch.
idstringundefinedID of the embedded checkbox.
switchBgClassNamestring""Custom className for the switch background.
switchHandleClassNamestring""Custom className for the switch handle.
checkedIconClassNamestring""Custom className for the checked icon.
uncheckedIconClassNamestring""Custom className for the unchecked icon.
checkedIconStyleReact.CSSPropertiesundefinedCustom styles for the checked icon.
uncheckedIconStyleReact.CSSPropertiesundefinedCustom styles for the unchecked icon.

Development

You're welcome to contribute to react-toggle-switch. Keep in mind that big changes have to be thoroughly tested on different browsers and devices before they can be merged.

To set up the project:

Fork and clone the repository

Install dependencies:

npm install

Run the development server:

npm run dev

License

This project is licensed under the MIT License.

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

10 months ago