Tailwind CSS Switch - React

Use our Tailwind CSS Switch component to let users adjust settings on/off. The option that the Switch controls, as well as the state it's in, should be made clear from the corresponding inline label.

See below our example that will help you create simple and easy-to-use Switch component for your Tailwind CSS and React project.


Switch Demo

Here's how to implement a Switch component. It can be used for forms or other components.

import { Switch } from "@material-tailwind/react";

export function SwitchDemo() {
return <Switch />;
}
import { Switch } from "@material-tailwind/react";

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

Switch Checked

You can use the defaultChecked prop for making the Switch component checked by default.

import { Switch } from "@material-tailwind/react";

export function SwitchChecked() {
return <Switch defaultChecked />;
}
import { Switch } from "@material-tailwind/react";

export function SwitchChecked() {
return <Switch defaultChecked />;
}

Switch Colors

We provide different colors for Switch component like primary, secondary, info, success, warning, and error so you can adapt it easily to your needs. You can simply use the color prop to change the Switch component color.

In the example below, we've showcased the different colors for Switch component that you can use in your project.

import { Switch } from "@material-tailwind/react";

export function SwitchColors() {
return (
<div className="flex flex-wrap justify-center gap-4">
<Switch color="primary" />
<Switch color="secondary" />
<Switch color="info" />
<Switch color="success" />
<Switch color="warning" />
<Switch color="error" />
</div>
);
}
import { Switch } from "@material-tailwind/react";

export function SwitchColors() {
return (
<div className="flex flex-wrap justify-center gap-4">
<Switch color="primary" />
<Switch color="secondary" />
<Switch color="info" />
<Switch color="success" />
<Switch color="warning" />
<Switch color="error" />
</div>
);
}

Switch with Label

The Switch component can be used with a label to provide more context to the user. In the example below, we've added a label by using the Switch and Typography components together.

import { Switch, Typography } from "@material-tailwind/react";

const ID = "switch";

export function SwitchWithLabel() {
return (
<div className="flex items-center gap-2">
<Switch id={ID} />
<Typography
as="label"
htmlFor={ID}
className="text-foreground"
>
Dark Mode
</Typography>
</div>
);
}
import { Switch, Typography } from "@material-tailwind/react";

const ID = "switch";

export function SwitchWithLabel() {
return (
<div className="flex items-center gap-2">
<Switch id={ID} />
<Typography
as="label"
htmlFor={ID}
className="text-foreground"
>
Dark Mode
</Typography>
</div>
);
}

Disabled Switch

You can disable the Switch component by adding the disabled prop. This will prevent the user from interacting with the Switch component.

import { Switch } from "@material-tailwind/react";

export function DisabledSwitch() {
return <Switch disabled />;
}
import { Switch } from "@material-tailwind/react";

export function DisabledSwitch() {
return <Switch disabled />;
}

Use the example below to create a Switch component with a link to the terms and conditions inside it's label.

import { Switch, Typography } from "@material-tailwind/react";

const ID = "switch-link";

export function SwitchWithLink() {
return (
<div className="flex items-center gap-2">
<Switch id={ID} />
<Typography
as="label"
htmlFor={ID}
className="flex gap-1 text-foreground"
>
I agree with the
<Typography as="a" href="#" color="primary">
terms and conditions
</Typography>
</Typography>
</div>
);
}
import { Switch, Typography } from "@material-tailwind/react";

const ID = "switch-link";

export function SwitchWithLink() {
return (
<div className="flex items-center gap-2">
<Switch id={ID} />
<Typography
as="label"
htmlFor={ID}
className="flex gap-1 text-foreground"
>
I agree with the
<Typography as="a" href="#" color="primary">
terms and conditions
</Typography>
</Typography>
</div>
);
}

Switch With Description

Use the example below to create a Switch component with a title and description as it's label to provide more information about the Switch component action.

import { Switch, Typography } from "@material-tailwind/react";

const ID = "switch-description";

export function SwitchWithDescription() {
return (
<div className="flex gap-2">
<Switch id={ID} />
<label htmlFor={ID} className="-translate-y-1">
<Typography color="default" className="font-medium">
Remember Me
</Typography>
<Typography type="small" className="text-foreground">
You&apos;ll be able to login without password for 24 hours.
</Typography>
</label>
</div>
);
}
import { Switch, Typography } from "@material-tailwind/react";

const ID = "switch-description";

export function SwitchWithDescription() {
return (
<div className="flex gap-2">
<Switch id={ID} />
<label htmlFor={ID} className="-translate-y-1">
<Typography color="default" className="font-medium">
Remember Me
</Typography>
<Typography type="small" className="text-foreground">
You&apos;ll be able to login without password for 24 hours.
</Typography>
</label>
</div>
);
}

Switch Custom Styles

You can use className prop to customize the Switch component style. In the example below, we make a Switch component similar to IOS switch component.

import { Switch } from "@material-tailwind/react";

export function CustomSwitch() {
return (
<Switch className="after:shadow-sm after:shadow-black/10 checked:before:bg-[#2ec946]" />
);
}
import { Switch } from "@material-tailwind/react";

export function CustomSwitch() {
return (
<Switch className="after:shadow-sm after:shadow-black/10 checked:before:bg-[#2ec946]" />
);
}