Switch

import Switch from 'frui/fields/Switch';
Copy

Props

Switch accepts all props of a standard HTML input element. See Moz for standard input attributes.

NameTypeRequiredNotes
bluebooleanNoShow blue switch
checkbooleanNoShow check when checked
checkedbooleanNoDefault checked state (Controlled)
checkexbooleanNoUse check/ex switch
classNamestringNoStandard HTML class names
defaultCheckedstringNoDefault checked state (Uncontrolled)
defaultValuestringNoDefault value (Uncontrolled)
greenbooleanNoShow green/red switch
errorstring|booleanNoAny error message
labelstringNoShows text to the right of switch
namestringNoUsed for react server components.
onChangeFunctionNoEvent handler when value has changed
onoffbooleanNoUse on/off switch
onUpdateFunctionNoUpdate event handler
orangebooleanNoShow orange switch
passRefLegacyRefNoPasses ref to html input
ridgebooleanNoAdds ridges to knobs
roundedbooleanNoMake switch rounded
styleCSS ObjectNoStandard CSS object
sunmoonbooleanNoUse sun/moon switch
valuestringNoDefault value (Controlled)
yesnobooleanNoUse yes/no switch

Basics

Switch wraps the HTML standard `<input />` element. Therefore, you can use any input attributes as props.

<Switch name="name" value="yes" defaultChecked />
Copy

Events

onUpdate is like onChange except the value is passed instead of the change event.

<Switch onUpdate={(value, checked) => alert(`${value} - ${checked}`)} />
Copy

On Change

The onChange event is triggered when the value has changed. The following arguments are passed to the event handler:

NameTypeSample
eventEvent Objectsee: Change Event

On Update

The onUpdate event is triggered when the value has been updated. The following arguments are passed to the event handler:

NameTypeSample
valuestring`foobar`

Errors

You can pass the error prop to highlight the Switch field red.

<Switch error defaultChecked />
Copy

Custom Styles

You can apply rounded, colors and shapes to the Switch component.

Ridge

Use ridge prop to add ridges to the knobs.

<Switch ridge defaultChecked />
Copy

Rounded

Use rounded prop to make the switches circular.

<Switch rounded defaultChecked />
Copy

Colors

Use blue, orange or green prop to change the color of switches.

<Switch blue label="Blue" defaultChecked />
Copy

Shapes

Use onoff, yesno, checkex or sunmoon prop to change the color of switches.

<Switch onoff label="Blue" defaultChecked />
Copy

Combniations

Try different combinations to get the switch you want.

You can also add your own custom class to Switch components or use any combination of frui-field-option, frui-field-option-control, and frui-field-option-label CSS classes.