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CAAa,EAAE,OAAO,iBAAiB,cAAc;YACvD,EAAE,wBAAwB;YAE1B,qEAAqE;YACrE,6CAA6C;YAC7C,IAAI,CAAC,UAAU,CAAC,cAAc;gBAC5B,eAAe;gBACf,CAAA,GAAA,+CAAoB,EAAE;gBACtB;YACF;QACF;IACF;IAEA,IAAI,UAAU,CAAC;QACb,IAAI,CAAA,GAAA,wCAAa,EAAE,OAAO,UAAU,cAClC,EAAE,wBAAwB;IAE9B;IAEA,IAAI,YAAY,CAAC;QACf,IAAI,CAAA,GAAA,wCAAa,EAAE,OAAO,UAAU,cAAc;YAChD,EAAE,wBAAwB;YAE1B,IAAI,CAAC,cAAc;gBACjB,eAAe;gBACf,CAAA,GAAA,+CAAoB,EAAE;gBACtB;YACF;QACF;IACF;IAEA,OAAO,gBAAgB,CAAC,QAAQ,QAAQ;IACxC,OAAO,gBAAgB,CAAC,YAAY,YAAY;IAChD,OAAO,gBAAgB,CAAC,WAAW,WAAW;IAC9C,OAAO,gBAAgB,CAAC,SAAS,SAAS;IAE1C,IAAI,UAAU;QACZ,qBAAqB;QACrB,OAAO,mBAAmB,CAAC,QAAQ,QAAQ;QAC3C,OAAO,mBAAmB,CAAC,YAAY,YAAY;QACnD,OAAO,mBAAmB,CAAC,WAAW,WAAW;QACjD,OAAO,mBAAmB,CAAC,SAAS,SAAS;QAC7C,4CAAmB;QACnB,eAAe;IACjB;IAEA,IAAI,MAAM,sBAAsB;IAChC,OAAO;AACT","sources":["packages/react-aria/src/interactions/utils.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 {FocusableElement} from '@react-types/shared';\nimport {focusWithoutScrolling} from '../utils/focusWithoutScrolling';\nimport {getActiveElement, getEventTarget} from '../utils/shadowdom/DOMFunctions';\nimport {getOwnerWindow} from '../utils/domHelpers';\nimport {isFocusable} from '../utils/isFocusable';\nimport {FocusEvent as ReactFocusEvent, SyntheticEvent, useCallback, useRef} from 'react';\nimport {useLayoutEffect} from '../utils/useLayoutEffect';\n\n// Turn a native event into a React synthetic event.\nexport function createSyntheticEvent<E extends SyntheticEvent>(nativeEvent: Event): E {\n let event = nativeEvent as any as E;\n event.nativeEvent = nativeEvent;\n event.isDefaultPrevented = () => event.defaultPrevented;\n // cancelBubble is technically deprecated in the spec, but still supported in all browsers.\n event.isPropagationStopped = () => (event as any).cancelBubble;\n event.persist = () => {};\n return event;\n}\n\nexport function setEventTarget(event: Event, target: Element): void {\n Object.defineProperty(event, 'target', {value: target});\n Object.defineProperty(event, 'currentTarget', {value: target});\n}\n\nexport function useSyntheticBlurEvent<Target extends Element = Element>(\n onBlur: (e: ReactFocusEvent<Target>) => void\n): (e: ReactFocusEvent<Target>) => void {\n let stateRef = useRef({\n isFocused: false,\n observer: null as MutationObserver | null\n });\n\n // Clean up MutationObserver on unmount. See below.\n\n useLayoutEffect(() => {\n const state = stateRef.current;\n return () => {\n if (state.observer) {\n state.observer.disconnect();\n state.observer = null;\n }\n };\n }, []);\n\n // This function is called during a React onFocus event.\n return useCallback(\n (e: ReactFocusEvent<Target>) => {\n // React does not fire onBlur when an element is disabled. https://github.com/facebook/react/issues/9142\n // Most browsers fire a native focusout event in this case, except for Firefox. In that case, we use a\n // MutationObserver to watch for the disabled attribute, and dispatch these events ourselves.\n // For browsers that do, focusout fires before the MutationObserver, so onBlur should not fire twice.\n let eventTarget = getEventTarget(e);\n if (\n eventTarget instanceof HTMLButtonElement ||\n eventTarget instanceof HTMLInputElement ||\n eventTarget instanceof HTMLTextAreaElement ||\n eventTarget instanceof HTMLSelectElement\n ) {\n stateRef.current.isFocused = true;\n\n let target = eventTarget;\n let onBlurHandler: EventListenerOrEventListenerObject | null = e => {\n stateRef.current.isFocused = false;\n\n if (target.disabled) {\n // For backward compatibility, dispatch a (fake) React synthetic event.\n let event = createSyntheticEvent<ReactFocusEvent<Target>>(e);\n onBlur?.(event);\n }\n\n // We no longer need the MutationObserver once the target is blurred.\n if (stateRef.current.observer) {\n stateRef.current.observer.disconnect();\n stateRef.current.observer = null;\n }\n };\n\n target.addEventListener('focusout', onBlurHandler, {once: true});\n\n stateRef.current.observer = new MutationObserver(() => {\n if (stateRef.current.isFocused && target.disabled) {\n stateRef.current.observer?.disconnect();\n let relatedTargetEl = target === getActiveElement() ? null : getActiveElement();\n target.dispatchEvent(new FocusEvent('blur', {relatedTarget: relatedTargetEl}));\n target.dispatchEvent(\n new FocusEvent('focusout', {bubbles: true, relatedTarget: relatedTargetEl})\n );\n }\n });\n\n stateRef.current.observer.observe(target, {\n attributes: true,\n attributeFilter: ['disabled']\n });\n }\n },\n [onBlur]\n );\n}\n\nexport let ignoreFocusEvent = false;\n\n/**\n * This function prevents the next focus event fired on `target`, without using\n * `event.preventDefault()`. It works by waiting for the series of focus events to occur, and\n * reverts focus back to where it was before. It also makes these events mostly non-observable by\n * using a capturing listener on the window and stopping propagation.\n */\nexport function preventFocus(target: FocusableElement | null): (() => void) | undefined {\n // The browser will focus the nearest focusable ancestor of our target.\n while (target && !isFocusable(target, {skipVisibilityCheck: true})) {\n target = target.parentElement;\n }\n\n let window = getOwnerWindow(target);\n let activeElement = window.document.activeElement as FocusableElement | null;\n if (!activeElement || activeElement === target) {\n return;\n }\n\n ignoreFocusEvent = true;\n let isRefocusing = false;\n let onBlur = (e: FocusEvent) => {\n if (getEventTarget(e) === activeElement || isRefocusing) {\n e.stopImmediatePropagation();\n }\n };\n\n let onFocusOut = (e: FocusEvent) => {\n if (getEventTarget(e) === activeElement || isRefocusing) {\n e.stopImmediatePropagation();\n\n // If there was no focusable ancestor, we don't expect a focus event.\n // Re-focus the original active element here.\n if (!target && !isRefocusing) {\n isRefocusing = true;\n focusWithoutScrolling(activeElement);\n cleanup();\n }\n }\n };\n\n let onFocus = (e: FocusEvent) => {\n if (getEventTarget(e) === target || isRefocusing) {\n e.stopImmediatePropagation();\n }\n };\n\n let onFocusIn = (e: FocusEvent) => {\n if (getEventTarget(e) === target || isRefocusing) {\n e.stopImmediatePropagation();\n\n if (!isRefocusing) {\n isRefocusing = true;\n focusWithoutScrolling(activeElement);\n cleanup();\n }\n }\n };\n\n window.addEventListener('blur', onBlur, true);\n window.addEventListener('focusout', onFocusOut, true);\n window.addEventListener('focusin', onFocusIn, true);\n window.addEventListener('focus', onFocus, true);\n\n let cleanup = () => {\n cancelAnimationFrame(raf);\n window.removeEventListener('blur', onBlur, true);\n window.removeEventListener('focusout', onFocusOut, true);\n window.removeEventListener('focusin', onFocusIn, true);\n window.removeEventListener('focus', onFocus, true);\n ignoreFocusEvent = false;\n isRefocusing = false;\n };\n\n let raf = requestAnimationFrame(cleanup);\n return cleanup;\n}\n"],"names":[],"version":3,"file":"utils.cjs.map"}