1.3.1 • Published 4 months ago

react-toggly v1.3.1

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

react-toggly

A highly customizable and themeable toggle switch component for React, built with TypeScript. Supports multiple themes like iOS, Bootstrap, and Tailwind CSS, and allows for custom icons, accessibility features, and controlled/uncontrolled behavior.

Demo Link

Features

  • Multiple Themes: Choose from ios, bootstrap, or tailwind themes
  • Custom Icons: Add custom icons for checked and unchecked states
  • Accessibility: Full ARIA support for better accessibility
  • Controlled/Uncontrolled: Works as both controlled and uncontrolled component
  • Labels: Support for left and right labels (text or icons)
  • Disabled State: Easily disable the toggle
  • TypeScript Support: Fully typed for better developer experience

Installation

npm install react-toggly
# or
yarn add react-toggly

Styles

To use the component styles, import them in your application:

// Import the styles in your app's entry point (e.g., App.js or index.js)
import 'react-toggly/dist/styles.css';

Changelog

See CHANGELOG.md for a list of changes and version history.

Usage

Basic Usage

import ToggleSwitch from 'react-toggly';

function App() {
  const [isChecked, setIsChecked] = React.useState(false);

  const handleToggle = (e) => {
    setIsChecked(e.target.checked);
  };

  return (
    <ToggleSwitch
      isChecked={isChecked}
      onToggleChange={handleToggle}
      toggleTheme="ios"
    />
  );
}

With Labels

<ToggleSwitch
  isChecked={isChecked}
  onToggleChange={handleToggle}
  leftLabel="Off"
  rightLabel="On"
/>

With Custom Icons

<ToggleSwitch
  isChecked={isChecked}
  onToggleChange={handleToggle}
  customIcons={{
    checked: <span>✓</span>,
    unchecked: <span>✕</span>
  }}
/>

Different Themes

// iOS theme (default)
<ToggleSwitch toggleTheme="ios" />

// Bootstrap theme
<ToggleSwitch toggleTheme="bootstrap" />

// Tailwind theme
<ToggleSwitch toggleTheme="tailwind" />

Uncontrolled Component

<ToggleSwitch
  defaultIsChecked={true}
  onToggleChange={(e) => console.log('Toggle changed:', e.target.checked)}
/>

Props

PropTypeDefaultDescription
isCheckedboolean-Controlled state of the toggle
defaultIsCheckedbooleanfalseInitial state for uncontrolled usage
onToggleChange(e: ChangeEvent) => void-Change event handler
toggleTheme'ios' \| 'bootstrap' \| 'tailwind''ios'Visual theme
leftLabelReactNode-Label on the left side
rightLabelReactNode-Label on the right side
customIcons{ checked?: ReactNode, unchecked?: ReactNode }-Custom icons
isDisabledbooleanfalseDisable the toggle
containerClassNamestring-Additional container class
inputNamestring-Input name attribute
inputIdstring-Input id attribute
ariaLabelstring-ARIA label
ariaLabelledBystring-ARIA labelledby

Contributing

We welcome contributions! Please see our Contributing Guide for details on how to:

  • Set up the development environment
  • Submit pull requests
  • Follow our coding standards
  • Contribute to documentation

License

MIT