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Aa,CAAC;IAExB;IAEA,IAAI,cAAC,UAAU,EAAE,GAAG,YAAW,GAAG,CAAA,GAAA,+CAAoB,EACpD;QACE,GAAG,KAAK;QACR,IAAI;QACJ,OAAO;QACP,iFAAiF;QACjF,oCAAoC;QACpC,cAAc;QACd,UAAU;QACV,CAAC,CAAA,GAAA,+EAAyB,EAAE,EAAE;QAC9B,MAAM;QACN,cAAc;kBACd;IACF,GACA,OACA;IAGF,CAAA,GAAA,sCAAW,EAAE,KAAK,MAAM,iBAAiB,EAAE,MAAM,aAAa;IAE9D,aAAa,CAAA,GAAA,oCAAS,EAAE,YAAY,iBAAiB,kBAAkB;QACrE,MAAM;QACN,iBAAiB;QACjB,iBAAiB;QACjB,iBAAiB;QACjB,kBAAkB;QAClB,aAAa;QACb,YAAY;QACZ,QAAQ;IACV;IAEA,IAAI,uBAAuB,UACzB,UAAU,CAAC,gBAAgB,GAAG;IAGhC,OAAO;oBACL;QACA,GAAG,UAAU;IACf;AACF","sources":["packages/react-aria/src/color/useColorField.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 DOMAttributes,\n FocusableDOMProps,\n TextInputDOMProps,\n ValidationResult\n} from '@react-types/shared';\nimport {ColorFieldProps, ColorFieldState} from 'react-stately/useColorFieldState';\nimport {InputHTMLAttributes, LabelHTMLAttributes, RefObject, useCallback, useState} from 'react';\nimport {mergeProps} from '../utils/mergeProps';\nimport {privateValidationStateProp} from 'react-stately/private/form/useFormValidationState';\nimport {useFocusWithin} from '../interactions/useFocusWithin';\nimport {useFormattedTextField} from '../textfield/useFormattedTextField';\nimport {useFormReset} from '../utils/useFormReset';\nimport {useId} from '../utils/useId';\nimport {useScrollWheel} from '../interactions/useScrollWheel';\nimport {useSpinButton} from '../spinbutton/useSpinButton';\n\nexport interface AriaColorFieldProps\n extends\n ColorFieldProps,\n AriaLabelingProps,\n FocusableDOMProps,\n Omit<\n TextInputDOMProps,\n | 'minLength'\n | 'maxLength'\n | 'pattern'\n | 'type'\n | 'inputMode'\n | 'autoComplete'\n | 'autoCorrect'\n | 'spellCheck'\n >,\n AriaValidationProps {\n /** Enables or disables changing the value with scroll. */\n isWheelDisabled?: boolean;\n}\n\nexport interface ColorFieldAria extends ValidationResult {\n /** Props for the label element. */\n labelProps: LabelHTMLAttributes<HTMLLabelElement>;\n /** Props for the input element. */\n inputProps: InputHTMLAttributes<HTMLInputElement>;\n /** Props for the text field's description element, if any. */\n descriptionProps: DOMAttributes;\n /** Props for the text field's error message element, if any. */\n errorMessageProps: DOMAttributes;\n}\n\n/**\n * Provides the behavior and accessibility implementation for a color field component.\n * Color fields allow users to enter and adjust a hex color value.\n */\nexport function useColorField(\n props: AriaColorFieldProps,\n state: ColorFieldState,\n ref: RefObject<HTMLInputElement | null>\n): ColorFieldAria {\n let {isDisabled, isReadOnly, isRequired, isWheelDisabled, validationBehavior = 'aria'} = props;\n\n let {colorValue, inputValue, increment, decrement, incrementToMax, decrementToMin, commit} =\n state;\n\n let inputId = useId();\n let {spinButtonProps} = useSpinButton({\n isDisabled,\n isReadOnly,\n isRequired,\n maxValue: 0xffffff,\n minValue: 0,\n onIncrement: increment,\n onIncrementToMax: incrementToMax,\n onDecrement: decrement,\n onDecrementToMin: decrementToMin,\n value: colorValue ? colorValue.toHexInt() : undefined,\n textValue: colorValue ? colorValue.toString('hex') : undefined\n });\n\n let [focusWithin, setFocusWithin] = useState(false);\n let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin});\n\n let onWheel = useCallback(\n e => {\n if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {\n return;\n }\n if (e.deltaY > 0) {\n increment();\n } else if (e.deltaY < 0) {\n decrement();\n }\n },\n [decrement, increment]\n );\n // If the input isn't supposed to receive input, disable scrolling.\n let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;\n useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, ref);\n\n let onChange = value => {\n if (state.validate(value)) {\n state.setInputValue(value);\n }\n };\n\n let {inputProps, ...otherProps} = useFormattedTextField(\n {\n ...props,\n id: inputId,\n value: inputValue,\n // Intentionally invalid value that will be ignored by onChange during form reset\n // This is handled separately below.\n defaultValue: '!',\n validate: undefined,\n [privateValidationStateProp]: state,\n type: 'text',\n autoComplete: 'off',\n onChange\n },\n state,\n ref\n );\n\n useFormReset(ref, state.defaultColorValue, state.setColorValue);\n\n inputProps = mergeProps(inputProps, spinButtonProps, focusWithinProps, {\n role: 'textbox',\n 'aria-valuemax': null,\n 'aria-valuemin': null,\n 'aria-valuenow': null,\n 'aria-valuetext': null,\n autoCorrect: 'off',\n spellCheck: 'false',\n onBlur: commit\n });\n\n if (validationBehavior === 'native') {\n inputProps['aria-required'] = undefined;\n }\n\n return {\n inputProps,\n ...otherProps\n };\n}\n"],"names":[],"version":3,"file":"useColorField.cjs.map"}