Skip to main content
Brand and assets
Build
Monitor and contribute
/
Component library
  • All components
  • Buttons & actions
  • Forms & inputs
    • Checkbox
    • ChoiceGroup
    • CurrencyInput
    • DateInput
    • FileInputZone
    • Filters
    • Form
    • MaskedTextInput
    • PasswordInput
    • Pill
    • Pill
    • Radio
    • SearchInput
    • SegmentedSlider
    • Select
    • Switch
    • TextArea
    • TextInput
  • Layout & containers
  • Overlays
  • Feedback & status
  • Data display
  • Typography
  • Accessibility
  • Contrib
  • Deprecated
  • Forms & inputs
  • Switch

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.

Health
Component health
Source
View on Github
Design
View Figma
Storybook
View in Storybook
Playground
No Example
Accessibility

compliant

Platform
Universal
  • 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​

Loading...
tip
This component is universal, its usage is the same in murray and murray/mobile.

Switch props

NameTypeDefault ValueDescriptionUsage count
aria-describedbystringundefinedId of an element that describes the input, only necessary if `helpText` is not used and the text is already present in the page
aria-labelstringundefinedAccessibility label, only necessary if `label` is not used
aria-labelledbystringundefinedAccessibility label, only necessary if `label` is not used and the text is already present in the page
checkedbooleanundefinedControl the input.
defaultCheckedbooleanundefinedUncontrolled default value.
disabledbooleanundefinedDisable element
errorReactNodeContents of `InputError` component, if not set, error is not rendered.
helpTextReactNodeContents of `InputHelpTextNext` component, if not set, description is not rendered.
idstringundefinedOverride the default generated id
labelReactNodeContents of `InputLabelNext` component, if not set, label is not rendered.
labelRightSectionReactNodeRendered right after the label, outside its text: an info button, a tag, any view. A view inside `label` displays as "…" on Android.
loadingbooleanundefinedLoading state of switch
namestringundefinedHtml input name
onBlurundefinedFocusEventHandler<HTMLDivElement>Callback that is called when the element is blurred
onChange((value: boolean) => void)undefinedTrigger when the checked state is changing
onFocusundefinedFocusEventHandler<HTMLDivElement>Callback that is called when the element is focused
requiredbooleanundefinedAdds aria-required attribute to the input and a red asterisk on the right side of label, `false` by default
reversedbooleanundefinedIf true, the label and help test will be on the left on the Switch
size"sm""md""lg"undefinedSize of the switch. Defaults depends on density theme and platform (standard: md, condensed: sm)
sxPositionPropsundefined
testIDstringundefinedTest identifier, set on the switch control itself (not the label)
valuestringundefinedGive it a value (for use in HTML forms).

SwitchGroupNext props

No props

Style API​

Use sx for one-off adjustments. It accepts these style keys:

alignSelfanimationaspectRatiobottomcursordisplayendflexflexBasisflexGrowflexShrinkheightinsetinsetInlineinsetInlineEndinsetInlineStartleftmarginmarginBottommarginHorizontalmarginInlinemarginInlineEndmarginInlineStartmarginLeftmarginRightmarginTopmarginVerticalmatrixmaxHeightmaxWidthminHeightminWidthopacityperspectivepositionrightrotaterotateXrotateYrotateZscalescaleXscaleYskewXskewYstarttoptransformtransformOriginwidthxyzIndex

Each can be overridden per breakpoint with:

$lg$ltLg$ltMd$ltSm$ltVs$ltXl$ltXxl$md$sm$vs$xl$xs$xxl
Edit this page
Previous
Select
Next
TextArea
  • Usage
  • States
  • Controlled
  • Condensed theme
  • Reversed
  • Label right section
  • Switch.Group
  • Usage in <Form>
  • Playground