Toggle
A toggle is used to switch between two options or states. It typically has two states on or off.
Basic
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const Basic = () => {
return (
<div className="flex gap-8">
<Toggle defaultChecked />
<Toggle />
<Toggle disabled />
<Toggle disabled checked={true} />
<Toggle readOnly defaultChecked />
</div>
);With Label
- Label can be added via
childrenofTogglecomponent.
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const WithLabel = () => {
return (
<div className="flex flex-col items-start gap-8">
<Toggle>Label</Toggle>
<Toggle defaultChecked>Default On</Toggle>
<Toggle>
The long content explains
<br />
the purpose and effectiveness of the toggle
</Toggle>Sizes
- Toggle supports 4 sizes
sm,md,lgandresponsiveand can be changed by passing thesizeprop. - Default size is
lg.
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const Sizes = () => {
return (
<div className="flex gap-8 flex-col items-start">
<Toggle size="lg">Size lg</Toggle>
<Toggle size="md">Size md</Toggle>
<Toggle size="sm">Size sm</Toggle>
</div>
);
};
- With size
responsive, the component will be responsive to the viewport.
Screen prefix | Screen width | Component size |
|---|---|---|
xs → xl | 0px → 1279px | sm |
xl → 3xl | 1280px → 1919px | md |
3xl → ∞ | 1920px → ∞ | lg |
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const ResponsiveSize = () => {
return (
<div className="flex gap-8 flex-col items-start">
<Toggle size="responsive">Size responsive</Toggle>
</div>
);
};
Disabled
- Disabled state can be enabled by setting
disabledprop totrue.
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const Disabled = () => {
return (
<div className="flex gap-8 flex-col ">
<Toggle disabled>Disabled</Toggle>
<Toggle disabled defaultChecked>
On
</Toggle>
</div>
);
};ReadOnly
- ReadOnly state can be enabled by setting
readOnlyprop totrue.
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const ReadOnly = () => {
return (
<div className="flex gap-8 flex-col">
<Toggle readOnly>ReadOnly</Toggle>
<Toggle readOnly defaultChecked>
On
</Toggle>
</div>
);
};Label alignment
- Label can be placed to the right or left of the toggle by setting
labelAlignmentprop toleft | right. - Default
labelAlignmentisright.
Source Code
import { Toggle } from '@ocean-network-express/magenta-ui/react';
export const LabelAlignment = () => {
return (
<div className="flex gap-16">
<div className="flex gap-8 flex-col">
<Toggle>Right Alignment</Toggle>
<Toggle>
The long content explains the purpose
<br />
and effectiveness of the toggle
</Toggle>Toggle Group
FormCheckis a helpful wrapper used to group toggle components that provides an easier API.
This is anonymous
Source Code
import { FormCheck, Toggle } from '@ocean-network-express/magenta-ui/react';
export const ToggleGroup = () => {
return (
<FormCheck
label={'Phone settings'}
helperText={'This is anonymous'}
name={'phoneSettings'}
required
>
<Toggle value="airplaneMode" defaultChecked>
Airplane modeAccessibility
Toggle is a regular input[type="checkbox"] follow WAI-ARIA recommendations.
Roles, States, and Properties
- Toggle has
role="switch". - Toggle has an accessible label provided by a visible label referenced by the value of
aria-labelledbyset on the element with role switch. - Toggle uses the HTML
checkedattribute to indicate its state: checked="true"indicates the toggle is on.checked="false"indicates the toggle is off.- If a set of toggles is presented as a logical group with a visible label, either:
- The toggles are included in an element with role
group. - The
groupelement has the propertyaria-labelledbyset to the ID of the element containing the group label. - The
groupelement has the propertyaria-describedbyset to the ID of the element containing the group description.
Keyboard interactions
Key | Function | Condition |
|---|---|---|
Tab | Moves keyboard focus to the toggle. | |
Space | Changes the state of the toggle between on and off. | When the toggle has focus. |
Was this page helpful?
Switch
On/Off
Boolean
Toggle - Examples