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EAAE,YAAY;YAAC,SAAS,CAAA,IAAK,EAAE,cAAc;QAAE;QACpE,YAAY,CAAA,GAAA,yCAAS,EAAE,UAAU;YAC/B,SAAS,MAAM,UAAU;YACzB,iBAAiB,AAAC,cAAc,uBAAuB,UAAW;YAClE,UAAU,cAAc,uBAAuB;YAC/C,gBAAgB,aAAa,MAAM,eAAe,KAAK,aAAa;YACpE,qBAAqB,KAAK,CAAC,oBAAoB;YAC/C,iBAAiB,KAAK,CAAC,gBAAgB;YACvC,iBAAiB,cAAc;YAC/B,oBACE;gBAAC,iBAAiB,EAAE;gBAAE,kBAAkB,EAAE;gBAAE;aAAgB,CAAC,MAAM,CAAC,SAAS,IAAI,CAAC,QAClF;sBACF;YACA,UAAU;YACV,GAAI,SAAS,OAAO,CAAC,IAAI;uBAAC;YAAK,CAAC;kBAChC;kBACA;YACA,MAAM;YACN,GAAG,YAAY;QACjB;0BACA;2BACA;QACA,YAAY,MAAM,UAAU;QAC5B,WAAW,aAAa;oBACxB;oBACA;QACA,WAAW,aAAa,MAAM,eAAe,KAAK;0BAClD;2BACA;IACF;AACF","sources":["packages/react-aria/src/toggle/useToggle.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {\n AriaLabelingProps,\n AriaValidationProps,\n DOMAttributesWithRef,\n FocusableDOMProps,\n InputDOMProps,\n PressEvents,\n RefObject,\n ValidationResult\n} from '@react-types/shared';\nimport {ChangeEventHandler, InputHTMLAttributes, LabelHTMLAttributes, useState} from 'react';\nimport {filterDOMProps} from '../utils/filterDOMProps';\nimport {getEventTarget} from '../utils/shadowdom/DOMFunctions';\nimport {mergeProps} from '../utils/mergeProps';\nimport {\n privateValidationStateProp,\n useFormValidationState\n} from 'react-stately/private/form/useFormValidationState';\nimport {ToggleProps, ToggleState} from 'react-stately/useToggleState';\nimport {useFocusable} from '../interactions/useFocusable';\nimport {useFormReset} from '../utils/useFormReset';\nimport {useFormValidation} from '../form/useFormValidation';\nimport {usePress} from '../interactions/usePress';\nimport {useSlotId2} from '../utils/useSlot';\n\nexport interface AriaToggleProps\n extends\n ToggleProps,\n FocusableDOMProps,\n AriaLabelingProps,\n AriaValidationProps,\n InputDOMProps,\n PressEvents {\n /**\n * Identifies the element (or elements) whose contents or presence are controlled by the current\n * element.\n */\n 'aria-controls'?: string;\n}\n\nexport interface ToggleAria extends ValidationResult {\n /** Props to be spread on the label element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>;\n /** Props to be spread on the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>;\n /** Props for the checkbox description element, if any. */\n descriptionProps: DOMAttributesWithRef<HTMLElement>;\n /** Props for the checkbox error message element, if any. */\n errorMessageProps: DOMAttributesWithRef<HTMLElement>;\n /** Whether the toggle is selected. */\n isSelected: boolean;\n /** Whether the toggle is in a pressed state. */\n isPressed: boolean;\n /** Whether the toggle is disabled. */\n isDisabled: boolean;\n /** Whether the toggle is read only. */\n isReadOnly: boolean;\n /** Whether the toggle is invalid. */\n isInvalid: boolean;\n}\n\n/**\n * Handles interactions for toggle elements, e.g. Checkboxes and Switches.\n */\nexport function useToggle(\n props: AriaToggleProps,\n state: ToggleState,\n ref: RefObject<HTMLInputElement | null>\n): ToggleAria {\n let {\n isDisabled = false,\n isReadOnly = false,\n value,\n name,\n form,\n children,\n isRequired,\n validationBehavior = 'aria',\n 'aria-label': ariaLabel,\n 'aria-labelledby': ariaLabelledby,\n 'aria-describedby': ariaDescribedby,\n onPressStart,\n onPressEnd,\n onPressChange,\n onPress,\n onPressUp,\n onClick\n } = props;\n\n // Create validation state here because it doesn't make sense to add to general useToggleState.\n let validationState = useFormValidationState({...props, value: state.isSelected});\n let {isInvalid, validationErrors, validationDetails} = validationState.displayValidation;\n\n useFormValidation(props, validationState, ref);\n\n let onChange: ChangeEventHandler<HTMLInputElement> = e => {\n // since we spread props on label, onChange will end up there as well as in here.\n // so we have to stop propagation at the lowest level that we care about\n e.stopPropagation();\n state.setSelected(getEventTarget(e).checked);\n };\n\n let hasChildren = children != null;\n let hasAriaLabel = ariaLabel != null || ariaLabelledby != null;\n if (!hasChildren && !hasAriaLabel && process.env.NODE_ENV !== 'production') {\n console.warn(\n 'If you do not provide children, you must specify an aria-label for accessibility'\n );\n }\n\n // Handle press state for keyboard interactions and cases where labelProps is not used.\n let {pressProps, isPressed} = usePress({\n onPressStart,\n onPressEnd,\n onPressChange,\n onPress,\n onPressUp,\n onClick,\n isDisabled\n });\n\n // Handle press state on the label.\n let [isLabelPressed, setLabelPressed] = useState(false);\n let {pressProps: labelProps} = usePress({\n onPressStart(e) {\n // Keyboard interactions are handled directly on the input.\n if (e.pointerType === 'keyboard' || e.pointerType === 'virtual') {\n e.continuePropagation();\n return;\n }\n\n onPressStart?.(e);\n onPressChange?.(true);\n setLabelPressed(true);\n },\n onPressEnd(e) {\n // Keyboard interactions are handled directly on the input.\n if (e.pointerType === 'keyboard' || e.pointerType === 'virtual') {\n e.continuePropagation();\n return;\n }\n\n onPressEnd?.(e);\n onPressChange?.(false);\n setLabelPressed(false);\n },\n onPressUp(e) {\n if (e.pointerType === 'keyboard' || e.pointerType === 'virtual') {\n e.continuePropagation();\n return;\n }\n\n onPressUp?.(e);\n },\n onClick,\n onPress(e) {\n if (e.pointerType === 'keyboard' || e.pointerType === 'virtual') {\n e.continuePropagation();\n return;\n }\n\n onPress?.(e);\n state.toggle();\n ref.current?.focus();\n\n // @ts-expect-error\n let {[privateValidationStateProp]: groupValidationState} = props;\n\n let {commitValidation} = groupValidationState ? groupValidationState : validationState;\n\n commitValidation();\n },\n isDisabled: isDisabled || isReadOnly\n });\n\n let {focusableProps} = useFocusable(props, ref);\n let interactions = mergeProps(pressProps, focusableProps);\n let domProps = filterDOMProps(props, {labelable: true});\n\n useFormReset(ref, state.defaultSelected, state.setSelected);\n\n // Copied from useField because we don't want the label behavior that provides.\n let descriptionProps = useSlotId2();\n let errorMessageProps = useSlotId2();\n\n return {\n labelProps: mergeProps(labelProps, {onClick: e => e.preventDefault()}),\n inputProps: mergeProps(domProps, {\n checked: state.isSelected,\n 'aria-required': (isRequired && validationBehavior === 'aria') || undefined,\n required: isRequired && validationBehavior === 'native',\n 'aria-invalid': isInvalid || props.validationState === 'invalid' || undefined,\n 'aria-errormessage': props['aria-errormessage'],\n 'aria-controls': props['aria-controls'],\n 'aria-readonly': isReadOnly || undefined,\n 'aria-describedby':\n [descriptionProps.id, errorMessageProps.id, ariaDescribedby].filter(Boolean).join(' ') ||\n undefined,\n onChange,\n disabled: isDisabled,\n ...(value == null ? {} : {value}),\n name,\n form,\n type: 'checkbox',\n ...interactions\n }),\n descriptionProps,\n errorMessageProps,\n isSelected: state.isSelected,\n isPressed: isPressed || isLabelPressed,\n isDisabled,\n isReadOnly,\n isInvalid: isInvalid || props.validationState === 'invalid',\n validationErrors,\n validationDetails\n };\n}\n"],"names":[],"version":3,"file":"useToggle.mjs.map"}