A two-state button that can be on or off. The Toggle component extends Base UI's Toggle. It adds a variant prop.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/toggle
// https://base-ui.com/react/components/toggle
import { Toggle as BaseToggle } from "@base-ui/react/toggle";
import clsx from "clsx";
import styles from "./Toggle.module.scss";
function Toggle({ className, variant = "secondary", ...props }: Toggle.Props) {
return (
<BaseToggle
className={clsx(
styles.toggle,
variant === "secondary" && styles.variantSecondary,
variant === "naked" && styles.variantNaked,
className,
)}
{...props}
/>
);
}
namespace Toggle {
export type Variant = "secondary" | "naked";
export interface Props extends Omit<BaseToggle.Props, "className"> {
className?: string;
variant?: Variant;
}
export type State = BaseToggle.State;
export type ChangeEventDetails = BaseToggle.ChangeEventDetails;
}
export default Toggle;Give an icon-only toggle an aria-label that names the action.
Use pressed and onPressedChange to control the toggle.
Set variant to secondary or naked. The default is secondary.
Place a text label after the icon. With a visible label, the toggle needs no aria-label.
Set disabled to prevent interaction.