00cc8c50e3
* add Alert component * expose Alert * rename forgotten FLYOUT to POPOVER * use PopoverRenderPropArg * organize imports in a consistent way * ensure Portals behave as expected Portals can be nested from a React perspective, however in the DOM they are rendered as siblings, this is mostly fine. However, when they are rendered inside a Dialog, the Dialog itself is marked with `role="modal"` which makes all the other content inert. This means that rendering Menu.Items in a Portal or an Alert in a portal makes it non-interactable. Alerts are not even announced. To fix this, we ensure that we make the `root` of the Portal the actual dialog. This allows you to still interact with it, because an open modal is the "root" for the assistive technology. But there is a catch, a Dialog in a Dialog *can* render as a sibling, because you force the focus into the new Dialog. So we also ensured that Dialogs are always rendered in the portal root, and not inside another Dialog. * add dialog with alert example * add internal Description component * add internal Label component * add RadioGroup component * expose RadioGroup * add RadioGroup example * ensure to include tha RadioGroup.Option own id * update changelog * split documentation
5.0 KiB
5.0 KiB
Switch (Toggle)
The Switch component and related child components are used to quickly build custom switch/toggle components that are fully accessible out of the box, including correct ARIA attribute management and robust keyboard support.
Installation
# npm
npm install @headlessui/react
# Yarn
yarn add @headlessui/react
Basic example
Switches are built using the Switch component. Optionally you can also use the Switch.Group and Switch.Label components.
import { useState } from 'react'
import { Switch } from '@headlessui/react'
function NotificationsToggle() {
const [enabled, setEnabled] = useState(false)
return (
<Switch
checked={enabled}
onChange={setEnabled}
className={`${
enabled ? 'bg-blue-600' : 'bg-gray-200'
} relative inline-flex items-center h-6 rounded-full w-11`}
>
<span className="sr-only">Enable notifications</span>
<span
className={`${
enabled ? 'translate-x-6' : 'translate-x-1'
} inline-block w-4 h-4 transform bg-white rounded-full`}
/>
</Switch>
)
}
Using a custom label
By default the Switch will use the contents as the label for screenreaders. If you need more control, you can render a Switch.Label outside of the Switch, as long as both the switch and label are within a parent Switch.Group.
Clicking the label will toggle the switch state, like you'd expect from a native checkbox.
import { useState } from 'react'
import { Switch } from '@headlessui/react'
function NotificationsToggle() {
const [enabled, setEnabled] = useState(false)
return (
<Switch.Group>
<Switch.Label>Enable notifications</Switch.Label>
<Switch
checked={enabled}
onChange={setEnabled}
className={`${
enabled ? 'bg-blue-600' : 'bg-gray-200'
} relative inline-flex items-center h-6 rounded-full w-11`}
>
<span
className={`${
enabled ? 'translate-x-6' : 'translate-x-1'
} inline-block w-4 h-4 transform bg-white rounded-full`}
/>
</Switch>
</Switch.Group>
)
}
Component API
Switch
<Switch checked={checkedState} onChange={setCheckedState}>
<span className="sr-only">Enable notifications</span>
{/* ... */}
</Switch>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as |
String | Component | button |
The element or component the Switch should render as. |
checked |
Boolean | - | Whether or not the switch is checked. |
onChange |
(value: boolean): void |
- | The function to call when the switch is toggled. |
Render prop object
| Prop | Type | Description |
|---|---|---|
checked |
Boolean | Whether or not the switch is checked. |
Switch.Label
<Switch.Group>
<Switch.Label>Enable notifications</Switch.Label>
<Switch checked={enabled} onChange={setEnabled} className="...">
{/* ... */}
</Switch>
</Switch.Group>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as |
String | Component | label |
The element or component the Switch.Label should render as. |
Switch.Description
<Switch.Group>
<Switch.Description>Enable notifications</Switch.Description>
<Switch checked={enabled} onChange={setEnabled} className="...">
{/* ... */}
</Switch>
</Switch.Group>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as |
String | Component | label |
The element or component the Switch.Description should render as. |
Switch.Group
<Switch.Group>
<Switch.Label>Enable notifications</Switch.Label>
<Switch checked={enabled} onChange={setEnabled} className="...">
{/* ... */}
</Switch>
</Switch.Group>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as |
String | Component | React.Fragment (no wrapper element) |
The element or component the Switch.Group should render as. |