Skip to main content

Switch

A basic widget for getting the user input. It's a wonderful interface for toggling a value between two states. You can use Switch as an alternative to the Checkbox component.

import { Switch } from 'rizzui/switch';

Default

The default style of Switch component.

import { Switch } from 'rizzui/switch';

export default function App() {
return <Switch />;
}

Variants

You can change the style of Switch using variant property.

import { Switch } from 'rizzui/switch';

export default function App() {
return (s
<>
<Switch variant="flat" label="Flat" />
<Switch variant="outline" label="Outline" />
</>
);
}

Sizes

You can set different sizes of the Switch component using size property.

import { Switch } from 'rizzui/switch';

export default function App() {
return (
<>
<Switch label="Small" size="sm" />
<Switch label="Default" />
<Switch size="lg" label="Large" />
</>
);
}

Label Placement

You can set label on the two different sides using labelPlacement property.

import { Switch } from 'rizzui/switch';

export default function App() {
return (
<>
<Switch label="Left Placement" variant="outline" labelPlacement="left" />
<Switch
label="Right Placement"
variant="outline"
labelPlacement="right"
/>
</>
);
}

With Custom Icons

You can set custom on, off icon of the Switch component using onIcon & offIcon property.

import { Switch } from 'rizzui/switch';
import { SunIcon, MoonIcon } from '@heroicons/react/24/outline';

export default function App() {
return (
<Switch
label="Change theme"
offIcon={<SunIcon className="h-3.5 w-3.5" />}
onIcon={<MoonIcon className="h-3 w-3" />}
variant="outline"
/>
);
}

Disabled

The disabled style of the Switch component.

import { Switch } from 'rizzui/switch';

export default function App() {
return <Switch label="Disabled" disabled />;
}

With Helper Text

You can add helperText to the Switch using helperText property.

Please turn on the switch
import { Switch } from 'rizzui/switch';

export default function App() {
return (
<Switch
variant="outline"
label="Remember me"
labelClassName="font-medium"
helperText="Please turn on the switch"
/>
);
}

With Error Message

You can show the validation error message using error property.

import { Switch } from 'rizzui/switch';

export default function App() {
return (
<Switch
label="Remember me"
labelClassName="font-medium"
error="This field is required"
/>
);
}

API Reference


Switch Props

Here is the API documentation of the Switch component. And the rest of the props are the same as the original html checkbox input field.

PropsTypeDescriptionDefault
labelReactNodeSet field label__
labelWeightLabelWeightSet label font weight"medium"
labelPlacementleft rightChange label direction"right"
variantSwitchVariantsThe variants of the component are:"flat"
sizeSwitchSizesThe size of the component."md"
disabledbooleanWhether the switch is disabledfalse
onIconReactNodeSet custom icon when the switch is on__
offIconReactNodeSet custom icon when the switch is off__
helperTextReactNodeAdd helper text. Can be a string or a React component__
errorstringShow error message using this prop__
labelClassNamestringOverride default CSS style of label__
switchClassNamestringOverride default CSS style of switch__
switchKnobClassNamestringOverride default CSS style of switch knob__
helperClassNamestringOverride default CSS style of helperText__
errorClassNamestringOverride default CSS style of error message__
classNamestringAdd custom classes to the root of the component__
refRef<HTMLInputElement>__
...InputHTMLAttributesnative props like value, onChange, onFocus, onBlur ...__

Switch Variants

type SwitchVariants = 'outline' | 'flat';

Label Weight

type LabelWeight = 'normal' | 'medium' | 'semibold' | 'bold';

Switch Sizes

type SwitchSizes = 'sm' | 'md' | 'lg';