Switch

A control that lets the user toggle between checked and unchecked states.

Installation

If @melio-ui/react is already installed globally, you can skip this step.

Import

  • SwitchRoot: All parts of the switch are included.
  • SwitchTrack: It represents the switch's background, indicating on and off states with different colors or styles.
  • SwitchThumb: It is the interactive element that toggles between on and off states.
  • SwitchLabel: It displays text or content next to the switch to clarify its purpose.

Another way to import

'use client';
import { Switch } from '@melio-ui/switch';
<Switch.Root>
<Switch.Track>
<Switch.Thumb />
</Switch.Track>
<Switch.Label>Label</Switch.Label>
</Switch.Root>;

'use client' must be used when rendering on the server side.

API Reference

SwitchRoot

All parts of the switch are included.

PropTypeDefaultDescription
checkedboolean-Set whether to check or not.
defaultCheckedboolean-The checked state of the switch when it is first rendered. Use this when there is no need to control the checked state.
disabledboolean-Determines whether the switch is inactive.
inputPropsReact. InputHTMLAttributes <HTMLInputElement>-Attributes assigned to the input element.
onCheckedChange(checked: boolean) => void-This is an event handler that is triggered when the state of the switch changes.
Data attributeValues
[data-state]"checked" | "unchecked"
[data-disabled]Visible when disabled
[data-readonly]Visible when read-only

SwitchTrack

It represents the switch's background, indicating on and off states with different colors or styles.

Data attributeValues
[data-state]"checked" | "unchecked"
[data-disabled]Visible when disabled
[data-readonly]Visible when read-only

SwitchThumb

It is the interactive element that toggles between on and off states.

Data attributeValues
[data-state]"checked" | "unchecked"
[data-disabled]Visible when disabled
[data-readonly]Visible when read-only

SwitchLabel

It displays text or content next to the switch to clarify its purpose.

Data attributeValues
[data-state]"checked" | "unchecked"
[data-disabled]Visible when disabled
[data-readonly]Visible when read-only

Examples

defaultChecked

<SwitchRoot defaultChecked>
<SwitchTrack>
<SwitchThumb />
</SwitchTrack>
</SwitchRoot>

disabled

<SwitchRoot disabled>
<SwitchTrack>
<SwitchThumb />
</SwitchTrack>
</SwitchRoot>

Display Label

<SwitchRoot defaultChecked>
<SwitchTrack>
<SwitchThumb />
</SwitchTrack>
<SwitchLabel>Label</SwitchLabel>
</SwitchRoot>

Display checked/unchecked status text within the SwitchTrack.

function DisplayText() {
const [checked, setChecked] = React.useState(false);
return (
<SwitchRoot checked={checked} onCheckedChange={setChecked}>
<SwitchTrack>
<SwitchThumb />
{checked ? (
<span style={{ margin: '0 28px 0 5px', color: '#fff' }}>open</span>
) : (
<span style={{ margin: '0 5px 0 28px' }}>close</span>
)}
</SwitchTrack>
</SwitchRoot>
);
}
render(<DisplayText />);
Prev
Next
DocsMelioUI
Copyright © Ahn Co. All rights reserved.