UNPKG

@ark-ui/react

Version:

A collection of unstyled, accessible UI components for React, utilizing state machines for seamless interaction.

33 lines (30 loc) 998 B
'use client'; import { useMachine, normalizeProps } from '@zag-js/react'; import * as zagSwitch from '@zag-js/switch'; import { useId } from 'react'; import { useEnvironmentContext } from '../../providers/environment/use-environment-context.js'; import { useLocaleContext } from '../../providers/locale/use-locale-context.js'; import { useFieldContext } from '../field/use-field-context.js'; const useSwitch = (props) => { const id = useId(); const { getRootNode } = useEnvironmentContext(); const { dir } = useLocaleContext(); const field = useFieldContext(); const machineProps = { id, ids: { label: field?.ids.label, hiddenInput: field?.ids.control }, dir, disabled: field?.disabled, readOnly: field?.readOnly, invalid: field?.invalid, required: field?.required, getRootNode, ...props }; const service = useMachine(zagSwitch.machine, machineProps); return zagSwitch.connect(service, normalizeProps); }; export { useSwitch };