Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@react-aria/switch
Advanced tools
@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.
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 />;
}
react-switch is a lightweight React component for creating toggle switches. It provides a simple API and is easy to customize with CSS. Compared to @react-aria/switch, react-switch is more focused on providing a visually appealing switch component but may not have the same level of accessibility features.
rc-switch is a React component for creating switch controls. It is part of the rc-components collection and provides a highly customizable switch component. While it offers a range of customization options, it may require additional work to ensure full accessibility compared to @react-aria/switch.
react-toggle is a React component for creating highly customizable toggle switches. It provides a simple API and supports various customization options. However, it may not be as focused on accessibility as @react-aria/switch, making it a good choice for visually appealing switches with basic accessibility support.
This package is part of react-spectrum. See the repo for more details.
FAQs
Spectrum UI components in React
The npm package @react-aria/switch receives a total of 178,617 weekly downloads. As such, @react-aria/switch popularity was classified as popular.
We found that @react-aria/switch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.