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.
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.
| Props | Type | Description | Default |
|---|---|---|---|
| label | ReactNode | Set field label | __ |
| labelWeight | LabelWeight | Set label font weight | "medium" |
| labelPlacement | left right | Change label direction | "right" |
| variant | SwitchVariants | The variants of the component are: | "flat" |
| size | SwitchSizes | The size of the component. | "md" |
| disabled | boolean | Whether the switch is disabled | false |
| onIcon | ReactNode | Set custom icon when the switch is on | __ |
| offIcon | ReactNode | Set custom icon when the switch is off | __ |
| helperText | ReactNode | Add helper text. Can be a string or a React component | __ |
| error | string | Show error message using this prop | __ |
| labelClassName | string | Override default CSS style of label | __ |
| switchClassName | string | Override default CSS style of switch | __ |
| switchKnobClassName | string | Override default CSS style of switch knob | __ |
| helperClassName | string | Override default CSS style of helperText | __ |
| errorClassName | string | Override default CSS style of error message | __ |
| className | string | Add custom classes to the root of the component | __ |
| ref | Ref<HTMLInputElement> | __ | |
| ... | InputHTMLAttributes | native 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';