A switch that indicates whether a setting is on or off. The Switch component extends Base UI's Switch. It styles the Switch.Root and Switch.Thumb subcomponents.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/switch
// https://base-ui.com/react/components/switch
import { Switch as BaseSwitch } from "@base-ui/react/switch";
import clsx from "clsx";
import styles from "./Switch.module.scss";
function Root({ className, ...props }: Switch.Root.Props) {
return (
<BaseSwitch.Root className={clsx(styles.root, className)} {...props} />
);
}
function Thumb({ className, ...props }: Switch.Thumb.Props) {
return (
<BaseSwitch.Thumb className={clsx(styles.thumb, className)} {...props} />
);
}
const Switch = {
Root,
Thumb,
};
namespace Switch {
export namespace Root {
export type Props = Omit<BaseSwitch.Root.Props, "className"> & {
className?: string;
};
export type State = BaseSwitch.Root.State;
export type ChangeEventDetails = BaseSwitch.Root.ChangeEventDetails;
}
export namespace Thumb {
export type Props = Omit<BaseSwitch.Thumb.Props, "className"> & {
className?: string;
};
}
}
export default Switch;Place a Switch.Thumb inside Switch.Root. Set defaultChecked, or use checked and onCheckedChange to control it.
Set disabled on Switch.Root to prevent interaction.