"use client"; import { type ChangeEvent, type RefAttributes, useState, useEffect, } from "react"; import classNames from "classnames/dedupe"; import type { DOMProps } from "../../types/dom"; import { VisuallyHidden } from "../VisuallyHidden"; import "./Switch.css"; export type SwitchElementType = HTMLLabelElement; export interface SwitchProps extends DOMProps, RefAttributes { /** Custom class name for setting specific CSS */ className?: string; checked?: boolean; onChange?: (event: ChangeEvent) => void; isDisabled?: boolean; } const Switch = ({ ref, ...props }: SwitchProps) => { const { checked = false, className, onChange, isDisabled = false, ...otherProps } = props; const [enabled, setEnabled] = useState(checked); useEffect(() => { setEnabled(checked); }, [checked]); const classes = classNames( "mobius", "mobius-switch", { "mobius-switch--checked": enabled, "mobius-switch--disabled": isDisabled, }, className, ); const handleChange = (event: ChangeEvent) => { setEnabled(!enabled); if (onChange) { onChange(event); } }; return ( ); }; Switch.displayName = "Switch"; export { Switch };