@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
84 lines • 2.7 kB
TypeScript
import * as React from 'react';
import type { BaseUIComponentProps, NonNativeButtonProps } from "../../utils/types.js";
import type { FieldRoot } from "../../field/root/FieldRoot.js";
import { REASONS } from "../../utils/reasons.js";
import type { BaseUIChangeEventDetails } from "../../types/index.js";
/**
* Represents the switch itself.
* Renders a `<span>` element and a hidden `<input>` beside.
*
* Documentation: [Base UI Switch](https://base-ui.com/react/components/switch)
*/
export declare const SwitchRoot: React.ForwardRefExoticComponent<SwitchRootProps & React.RefAttributes<HTMLElement>>;
export interface SwitchRootState extends FieldRoot.State {
/**
* Whether the switch is currently active.
*/
checked: boolean;
/**
* Whether the component should ignore user interaction.
*/
disabled: boolean;
/**
* Whether the user should be unable to activate or deactivate the switch.
*/
readOnly: boolean;
/**
* Whether the user must activate the switch before submitting a form.
*/
required: boolean;
}
export interface SwitchRootProps extends NonNativeButtonProps, Omit<BaseUIComponentProps<'span', SwitchRoot.State>, 'onChange'> {
/**
* The id of the switch element.
*/
id?: string;
/**
* Whether the switch is currently active.
*
* To render an uncontrolled switch, use the `defaultChecked` prop instead.
*/
checked?: boolean;
/**
* Whether the switch is initially active.
*
* To render a controlled switch, use the `checked` prop instead.
* @default false
*/
defaultChecked?: boolean;
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled?: boolean;
/**
* A ref to access the hidden `<input>` element.
*/
inputRef?: React.Ref<HTMLInputElement>;
/**
* Identifies the field when a form is submitted.
*/
name?: string;
/**
* Event handler called when the switch is activated or deactivated.
*/
onCheckedChange?: (checked: boolean, eventDetails: SwitchRoot.ChangeEventDetails) => void;
/**
* Whether the user should be unable to activate or deactivate the switch.
* @default false
*/
readOnly?: boolean;
/**
* Whether the user must activate the switch before submitting a form.
* @default false
*/
required?: boolean;
}
export type SwitchRootChangeEventReason = typeof REASONS.none;
export type SwitchRootChangeEventDetails = BaseUIChangeEventDetails<SwitchRoot.ChangeEventReason>;
export declare namespace SwitchRoot {
type State = SwitchRootState;
type Props = SwitchRootProps;
type ChangeEventReason = SwitchRootChangeEventReason;
type ChangeEventDetails = SwitchRootChangeEventDetails;
}