Switch
Turns something on or off, applying straight away: a setting, an email, a display option, any feature. A `Checkbox` selects from a list, a `Switch` toggles.
compliant
- Overview
- Props
- Figma
Usage
Help text
States
Controlled
import { useState } from "react";
import { Switch } from "murray";
function Demo() {
const [checked, setChecked] = useState(false);
return <Switch checked={checked} onChange={(value) => setChecked(value)} />;
}
Condensed theme
For applications with condensed theme density, this component renders in a condensed style:
Reversed
In some cases (especially on mobile), it's useful to reverse the order of the label and the input:
Label right section
A view inside label displays as "…" on Android. Put an info button or a tag next to the label with labelRightSection instead:
Switch.Group
It's possible to control several related checkboxes by wraiing them in a Switch.Group component:
Usage in <Form>
On web, Switch and SwitchGroupNext can be used in <Form> component and be controleld by Formik:
Playground
murray and murray/mobile.Switch props
| Name | Type | Default Value | Description | Usage count |
|---|---|---|---|---|
| aria-describedby | stringundefined | Id of an element that describes the input, only necessary if `helpText` is not used and the text is already present in the page | ||
| aria-label | stringundefined | Accessibility label, only necessary if `label` is not used | ||
| aria-labelledby | stringundefined | Accessibility label, only necessary if `label` is not used and the text is already present in the page | ||
| checked | booleanundefined | Control the input. | ||
| defaultChecked | booleanundefined | Uncontrolled default value. | ||
| disabled | booleanundefined | Disable element | ||
| error | ReactNode | Contents of `InputError` component, if not set, error is not rendered. | ||
| helpText | ReactNode | Contents of `InputHelpTextNext` component, if not set, description is not rendered. | ||
| id | stringundefined | Override the default generated id | ||
| label | ReactNode | Contents of `InputLabelNext` component, if not set, label is not rendered. | ||
| labelRightSection | ReactNode | Rendered right after the label, outside its text: an info button, a tag, any view. A view inside `label` displays as "…" on Android. | ||
| loading | booleanundefined | Loading state of switch | ||
| name | stringundefined | Html input name | ||
| onBlur | undefinedFocusEventHandler<HTMLDivElement> | Callback that is called when the element is blurred | ||
| onChange | ((value: boolean) => void)undefined | Trigger when the checked state is changing | ||
| onFocus | undefinedFocusEventHandler<HTMLDivElement> | Callback that is called when the element is focused | ||
| required | booleanundefined | Adds aria-required attribute to the input and a red asterisk on the right side of label, `false` by default | ||
| reversed | booleanundefined | If true, the label and help test will be on the left on the Switch | ||
| size | "sm""md""lg"undefined | Size of the switch. Defaults depends on density theme and platform (standard: md, condensed: sm) | ||
| sx | PositionPropsundefined | |||
| testID | stringundefined | Test identifier, set on the switch control itself (not the label) | ||
| value | stringundefined | Give it a value (for use in HTML forms). |
SwitchGroupNext props
No propsStyle API
Use sx for one-off adjustments. It accepts these style keys:
Each can be overridden per breakpoint with: