Socket
Socket
Sign inDemoInstall

@react-aria/switch

Package Overview
Dependencies
Maintainers
2
Versions
709
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-aria/switch

Spectrum UI components in React


Version published
Weekly downloads
746K
increased by5.19%
Maintainers
2
Weekly downloads
 
Created

Package description

What is @react-aria/switch?

@react-aria/switch is a React library that provides accessible switch components. It is part of the React Aria collection, which aims to provide a set of hooks and components that help developers build accessible web applications. The switch component is used to represent a boolean state that can be toggled by the user.

What are @react-aria/switch's main functionalities?

Basic Switch

This code demonstrates a basic switch component using the @react-aria/switch package. The switch can be toggled between 'On' and 'Off' states.

import { useSwitch } from '@react-aria/switch';
import { useToggleState } from '@react-stately/toggle';
import { VisuallyHidden } from '@react-aria/visually-hidden';

function Switch(props) {
  let state = useToggleState(props);
  let ref = React.useRef();
  let { inputProps } = useSwitch(props, state, ref);

  return (
    <label>
      <VisuallyHidden>
        <input {...inputProps} ref={ref} />
      </VisuallyHidden>
      <span>{state.isSelected ? 'On' : 'Off'}</span>
    </label>
  );
}

export default function App() {
  return <Switch />;
}

Custom Styled Switch

This code demonstrates a custom styled switch component using the @react-aria/switch package. The switch is styled using CSS to look like a traditional toggle switch.

import { useSwitch } from '@react-aria/switch';
import { useToggleState } from '@react-stately/toggle';
import { VisuallyHidden } from '@react-aria/visually-hidden';
import './Switch.css';

function Switch(props) {
  let state = useToggleState(props);
  let ref = React.useRef();
  let { inputProps } = useSwitch(props, state, ref);

  return (
    <label className="switch">
      <VisuallyHidden>
        <input {...inputProps} ref={ref} />
      </VisuallyHidden>
      <span className="slider"></span>
    </label>
  );
}

export default function App() {
  return <Switch />;
}

Other packages similar to @react-aria/switch

Readme

Source

@react-aria/switch

This package is part of react-spectrum. See the repo for more details.

FAQs

Package last updated on 20 Dec 2023

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

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc