
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
@radix-ui/react-toggle
Advanced tools
@radix-ui/react-toggle is a React component library that provides accessible and customizable toggle buttons. It is part of the Radix UI suite, which focuses on providing unstyled, accessible components that can be easily styled to fit any design system.
Basic Toggle Button
This code demonstrates a basic toggle button using the @radix-ui/react-toggle package. The Toggle.Root component is the container, and the Toggle.Thumb component represents the toggle switch.
import * as Toggle from '@radix-ui/react-toggle';
function App() {
return (
<Toggle.Root>
<Toggle.Thumb />
</Toggle.Root>
);
}
export default App;
Controlled Toggle Button
This code demonstrates a controlled toggle button where the toggle state is managed by a React useState hook. The pressed prop is used to control the toggle state, and the onPressedChange prop is used to update the state.
import * as Toggle from '@radix-ui/react-toggle';
import { useState } from 'react';
function App() {
const [isToggled, setIsToggled] = useState(false);
return (
<Toggle.Root pressed={isToggled} onPressedChange={setIsToggled}>
<Toggle.Thumb />
</Toggle.Root>
);
}
export default App;
Custom Styled Toggle Button
This code demonstrates how to apply custom styles to the toggle button using CSS classes. The className prop is used to add custom styles to the Toggle.Root and Toggle.Thumb components.
import * as Toggle from '@radix-ui/react-toggle';
import './App.css';
function App() {
return (
<Toggle.Root className="custom-toggle">
<Toggle.Thumb className="custom-thumb" />
</Toggle.Root>
);
}
export default App;
react-toggle is a widely-used package for creating toggle buttons in React. It provides a simple API and comes with default styles that can be customized. Compared to @radix-ui/react-toggle, react-toggle is more opinionated in terms of styling but is easier to set up for basic use cases.
rc-switch is a React component for creating switch toggles. It is part of the rc-components suite and offers a highly customizable and accessible toggle switch. Compared to @radix-ui/react-toggle, rc-switch provides more built-in styles and is more feature-rich, but it may require more effort to integrate into a custom design system.
react-switch is another popular package for creating toggle switches in React. It offers a simple and flexible API with built-in styles that can be easily customized. Compared to @radix-ui/react-toggle, react-switch is more focused on providing a quick and easy solution for toggle switches with less emphasis on accessibility and customization.
react-toggle
$ yarn add @radix-ui/react-toggle
# or
$ npm install @radix-ui/react-toggle
View docs here.
FAQs
View docs [here](https://radix-ui.com/primitives/docs/components/toggle).
The npm package @radix-ui/react-toggle receives a total of 5,185,411 weekly downloads. As such, @radix-ui/react-toggle popularity was classified as popular.
We found that @radix-ui/react-toggle demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 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.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.