import { cnb } from "cnbuilder"; import { type CSSProperties, type HTMLAttributes, type InputHTMLAttributes, type ReactElement, type ReactNode, type Ref, } from "react"; import { type PropsWithRef } from "../types.js"; import { useEnsuredId } from "../useEnsuredId.js"; import { FormMessageContainer } from "./FormMessageContainer.js"; import { type InputToggleLabelProps } from "./InputToggle.js"; import { Label } from "./Label.js"; import { SwitchTrack } from "./SwitchTrack.js"; import { switchStyles } from "./switchStyles.js"; import { type FormComponentStates, type FormMessageContainerExtension, } from "./types.js"; // NOTE: The augmentation appears in this file since no type definitions are // ever imported from the `switchStyles` file. declare module "react" { interface CSSProperties { "--rmd-switch-track-background-color"?: string; "--rmd-switch-ball-background-color"?: string; } } /** * @since 6.0.0 Added `containerProps` and support for the * `FormMessage` behavior. */ export interface SwitchProps extends InputHTMLAttributes, InputToggleLabelProps, FormMessageContainerExtension, FormComponentStates { ref?: Ref; containerProps?: PropsWithRef>; trackProps?: PropsWithRef>; trackStyle?: CSSProperties; trackClassName?: string; ballAddon?: ReactNode; ballProps?: PropsWithRef>; ballStyle?: CSSProperties; ballClassName?: string; } /** * @example Simple Example * ```tsx * import { Form } from "@react-md/core/form/Form"; * import { Switch } from "@react-md/core/form/Switch"; * import type { ReactElement } from "react"; * import { useState } from "react"; * * function Example(): ReactElement { * const [checked, setChecked] = useState(false); * * return ( *
* setChecked(event.currentTarget.checked)} * /> * * ); * } * ``` * * @see {@link https://react-md.dev/components/switch | Switch Demos} * @since 6.0.0 Added support for `FormMessage` behavior. */ export function Switch(props: SwitchProps): ReactElement { const { ref, id: propId, label, labelProps, style, className, containerProps, ballProps, ballStyle, ballClassName, ballAddon, trackProps, trackStyle, trackClassName, messageProps, messageContainerProps, disableLabelGap = false, error = false, active = false, stacked = false, iconAfter = false, disabled = false, readOnly = false, ...remaining } = props; const id = useEnsuredId(propId, "switch"); return ( ); }