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,cAAc,aAAa,kBAAkB,CAAC,8CAAwB,CAAC,EAAE,GAAG,CAAC,AAAD;AACnH;AAKO,SAAS,0CAAgB,QAA2B,CAAC,CAAC;IAC3D,IAAI,eAAC,WAAW,aAAE,SAAS,EAAC,GAAG;IAC/B,IAAI,CAAC,qBAAqB,gBAAgB,GAAG,CAAA,GAAA,eAAO,EAAE,aAAa;IACnE,0CAAwB,CAAC;QACvB,gBAAgB;IAClB,GAAG;QAAC;KAAY,EAAE;qBAAC;IAAW;IAE9B,OAAO;QAAC,gBAAgB;IAAmB;AAC7C;AAKO,SAAS,0CAAwB,EAAuB,EAAE,IAAwB,EAAE,IAA8B;IACvH;IAEA,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,UAAU,CAAC,UAAoB;YACjC,IAAI,CAAC,2CAAqB,CAAC,EAAE,iBAAA,2BAAA,KAAM,WAAW,GAAG,UAAU,IACzD;YAEF,GAAG;QACL;QACA,qCAAe,GAAG,CAAC;QACnB,OAAO;YACL,qCAAe,MAAM,CAAC;QACxB;IACF,uDAAuD;IACvD,GAAG;AACL","sources":["packages/@react-aria/interactions/src/useFocusVisible.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\n// Portions of the code in this file are based on code from react.\n// Original licensing for the following can be found in the\n// NOTICE file in the root directory of this source tree.\n// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions\n\nimport {getOwnerDocument, getOwnerWindow, isMac, isVirtualClick} from '@react-aria/utils';\nimport {useEffect, useState} from 'react';\nimport {useIsSSR} from '@react-aria/ssr';\n\nexport type Modality = 'keyboard' | 'pointer' | 'virtual';\ntype HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent | null;\ntype Handler = (modality: Modality, e: HandlerEvent) => void;\nexport type FocusVisibleHandler = (isFocusVisible: boolean) => void;\nexport interface FocusVisibleProps {\n /** Whether the element is a text input. */\n isTextInput?: boolean,\n /** Whether the element will be auto focused. */\n autoFocus?: boolean\n}\n\nexport interface FocusVisibleResult {\n /** Whether keyboard focus is visible globally. */\n isFocusVisible: boolean\n}\n\nlet currentModality: null | Modality = null;\nlet changeHandlers = new Set<Handler>();\ninterface GlobalListenerData {\n focus: () => void\n}\nexport let hasSetupGlobalListeners = new Map<Window, GlobalListenerData>(); // We use a map here to support setting event listeners across multiple document objects.\nlet hasEventBeforeFocus = false;\nlet hasBlurredWindowRecently = false;\n\n// Only Tab or Esc keys will make focus visible on text input elements\nconst FOCUS_VISIBLE_INPUT_KEYS = {\n Tab: true,\n Escape: true\n};\n\nfunction triggerChangeHandlers(modality: Modality, e: HandlerEvent) {\n for (let handler of changeHandlers) {\n handler(modality, e);\n }\n}\n\n/**\n * Helper function to determine if a KeyboardEvent is unmodified and could make keyboard focus styles visible.\n */\nfunction isValidKey(e: KeyboardEvent) {\n // Control and Shift keys trigger when navigating back to the tab with keyboard.\n return !(e.metaKey || (!isMac() && e.altKey) || e.ctrlKey || e.key === 'Control' || e.key === 'Shift' || e.key === 'Meta');\n}\n\n\nfunction handleKeyboardEvent(e: KeyboardEvent) {\n hasEventBeforeFocus = true;\n if (isValidKey(e)) {\n currentModality = 'keyboard';\n triggerChangeHandlers('keyboard', e);\n }\n}\n\nfunction handlePointerEvent(e: PointerEvent | MouseEvent) {\n currentModality = 'pointer';\n if (e.type === 'mousedown' || e.type === 'pointerdown') {\n hasEventBeforeFocus = true;\n triggerChangeHandlers('pointer', e);\n }\n}\n\nfunction handleClickEvent(e: MouseEvent) {\n if (isVirtualClick(e)) {\n hasEventBeforeFocus = true;\n currentModality = 'virtual';\n }\n}\n\nfunction handleFocusEvent(e: FocusEvent) {\n // Firefox fires two extra focus events when the user first clicks into an iframe:\n // first on the window, then on the document. We ignore these events so they don't\n // cause keyboard focus rings to appear.\n if (e.target === window || e.target === document) {\n return;\n }\n\n // If a focus event occurs without a preceding keyboard or pointer event, switch to virtual modality.\n // This occurs, for example, when navigating a form with the next/previous buttons on iOS.\n if (!hasEventBeforeFocus && !hasBlurredWindowRecently) {\n currentModality = 'virtual';\n triggerChangeHandlers('virtual', e);\n }\n\n hasEventBeforeFocus = false;\n hasBlurredWindowRecently = false;\n}\n\nfunction handleWindowBlur() {\n // When the window is blurred, reset state. This is necessary when tabbing out of the window,\n // for example, since a subsequent focus event won't be fired.\n hasEventBeforeFocus = false;\n hasBlurredWindowRecently = true;\n}\n\n/**\n * Setup global event listeners to control when keyboard focus style should be visible.\n */\nfunction setupGlobalFocusEvents(element?: HTMLElement | null) {\n if (typeof window === 'undefined' || hasSetupGlobalListeners.get(getOwnerWindow(element))) {\n return;\n }\n\n const windowObject = getOwnerWindow(element);\n const documentObject = getOwnerDocument(element);\n\n // Programmatic focus() calls shouldn't affect the current input modality.\n // However, we need to detect other cases when a focus event occurs without\n // a preceding user event (e.g. screen reader focus). Overriding the focus\n // method on HTMLElement.prototype is a bit hacky, but works.\n let focus = windowObject.HTMLElement.prototype.focus;\n windowObject.HTMLElement.prototype.focus = function () {\n hasEventBeforeFocus = true;\n focus.apply(this, arguments as unknown as [options?: FocusOptions | undefined]);\n };\n\n documentObject.addEventListener('keydown', handleKeyboardEvent, true);\n documentObject.addEventListener('keyup', handleKeyboardEvent, true);\n documentObject.addEventListener('click', handleClickEvent, true);\n\n // Register focus events on the window so they are sure to happen\n // before React's event listeners (registered on the document).\n windowObject.addEventListener('focus', handleFocusEvent, true);\n windowObject.addEventListener('blur', handleWindowBlur, false);\n\n if (typeof PointerEvent !== 'undefined') {\n documentObject.addEventListener('pointerdown', handlePointerEvent, true);\n documentObject.addEventListener('pointermove', handlePointerEvent, true);\n documentObject.addEventListener('pointerup', handlePointerEvent, true);\n } else {\n documentObject.addEventListener('mousedown', handlePointerEvent, true);\n documentObject.addEventListener('mousemove', handlePointerEvent, true);\n documentObject.addEventListener('mouseup', handlePointerEvent, true);\n }\n\n // Add unmount handler\n windowObject.addEventListener('beforeunload', () => {\n tearDownWindowFocusTracking(element);\n }, {once: true});\n\n hasSetupGlobalListeners.set(windowObject, {focus});\n}\n\nconst tearDownWindowFocusTracking = (element, loadListener?: () => void) => {\n const windowObject = getOwnerWindow(element);\n const documentObject = getOwnerDocument(element);\n if (loadListener) {\n documentObject.removeEventListener('DOMContentLoaded', loadListener);\n }\n if (!hasSetupGlobalListeners.has(windowObject)) {\n return;\n }\n windowObject.HTMLElement.prototype.focus = hasSetupGlobalListeners.get(windowObject)!.focus;\n\n documentObject.removeEventListener('keydown', handleKeyboardEvent, true);\n documentObject.removeEventListener('keyup', handleKeyboardEvent, true);\n documentObject.removeEventListener('click', handleClickEvent, true);\n windowObject.removeEventListener('focus', handleFocusEvent, true);\n windowObject.removeEventListener('blur', handleWindowBlur, false);\n\n if (typeof PointerEvent !== 'undefined') {\n documentObject.removeEventListener('pointerdown', handlePointerEvent, true);\n documentObject.removeEventListener('pointermove', handlePointerEvent, true);\n documentObject.removeEventListener('pointerup', handlePointerEvent, true);\n } else {\n documentObject.removeEventListener('mousedown', handlePointerEvent, true);\n documentObject.removeEventListener('mousemove', handlePointerEvent, true);\n documentObject.removeEventListener('mouseup', handlePointerEvent, true);\n }\n\n hasSetupGlobalListeners.delete(windowObject);\n};\n\n/**\n * EXPERIMENTAL\n * Adds a window (i.e. iframe) to the list of windows that are being tracked for focus visible.\n *\n * Sometimes apps render portions of their tree into an iframe. In this case, we cannot accurately track if the focus\n * is visible because we cannot see interactions inside the iframe. If you have this in your application's architecture,\n * then this function will attach event listeners inside the iframe. You should call `addWindowFocusTracking` with an\n * element from inside the window you wish to add. We'll retrieve the relevant elements based on that.\n * Note, you do not need to call this for the default window, as we call it for you.\n *\n * When you are ready to stop listening, but you do not wish to unmount the iframe, you may call the cleanup function\n * returned by `addWindowFocusTracking`. Otherwise, when you unmount the iframe, all listeners and state will be cleaned\n * up automatically for you.\n *\n * @param element @default document.body - The element provided will be used to get the window to add.\n * @returns A function to remove the event listeners and cleanup the state.\n */\nexport function addWindowFocusTracking(element?: HTMLElement | null): () => void {\n const documentObject = getOwnerDocument(element);\n let loadListener;\n if (documentObject.readyState !== 'loading') {\n setupGlobalFocusEvents(element);\n } else {\n loadListener = () => {\n setupGlobalFocusEvents(element);\n };\n documentObject.addEventListener('DOMContentLoaded', loadListener);\n }\n\n return () => tearDownWindowFocusTracking(element, loadListener);\n}\n\n// Server-side rendering does not have the document object defined\n// eslint-disable-next-line no-restricted-globals\nif (typeof document !== 'undefined') {\n addWindowFocusTracking();\n}\n\n/**\n * If true, keyboard focus is visible.\n */\nexport function isFocusVisible(): boolean {\n return currentModality !== 'pointer';\n}\n\nexport function getInteractionModality(): Modality | null {\n return currentModality;\n}\n\nexport function setInteractionModality(modality: Modality) {\n currentModality = modality;\n triggerChangeHandlers(modality, null);\n}\n\n/**\n * Keeps state of the current modality.\n */\nexport function useInteractionModality(): Modality | null {\n setupGlobalFocusEvents();\n\n let [modality, setModality] = useState(currentModality);\n useEffect(() => {\n let handler = () => {\n setModality(currentModality);\n };\n\n changeHandlers.add(handler);\n return () => {\n changeHandlers.delete(handler);\n };\n }, []);\n\n return useIsSSR() ? null : modality;\n}\n\nconst nonTextInputTypes = new Set([\n 'checkbox',\n 'radio',\n 'range',\n 'color',\n 'file',\n 'image',\n 'button',\n 'submit',\n 'reset'\n]);\n\n/**\n * If this is attached to text input component, return if the event is a focus event (Tab/Escape keys pressed) so that\n * focus visible style can be properly set.\n */\nfunction isKeyboardFocusEvent(isTextInput: boolean, modality: Modality, e: HandlerEvent) {\n const IHTMLInputElement = typeof window !== 'undefined' ? getOwnerWindow(e?.target as Element).HTMLInputElement : HTMLInputElement;\n const IHTMLTextAreaElement = typeof window !== 'undefined' ? getOwnerWindow(e?.target as Element).HTMLTextAreaElement : HTMLTextAreaElement;\n const IHTMLElement = typeof window !== 'undefined' ? getOwnerWindow(e?.target as Element).HTMLElement : HTMLElement;\n const IKeyboardEvent = typeof window !== 'undefined' ? getOwnerWindow(e?.target as Element).KeyboardEvent : KeyboardEvent;\n\n isTextInput = isTextInput ||\n (e?.target instanceof IHTMLInputElement && !nonTextInputTypes.has(e?.target?.type)) ||\n e?.target instanceof IHTMLTextAreaElement ||\n (e?.target instanceof IHTMLElement && e?.target.isContentEditable);\n return !(isTextInput && modality === 'keyboard' && e instanceof IKeyboardEvent && !FOCUS_VISIBLE_INPUT_KEYS[e.key]);\n}\n\n/**\n * Manages focus visible state for the page, and subscribes individual components for updates.\n */\nexport function useFocusVisible(props: FocusVisibleProps = {}): FocusVisibleResult {\n let {isTextInput, autoFocus} = props;\n let [isFocusVisibleState, setFocusVisible] = useState(autoFocus || isFocusVisible());\n useFocusVisibleListener((isFocusVisible) => {\n setFocusVisible(isFocusVisible);\n }, [isTextInput], {isTextInput});\n\n return {isFocusVisible: isFocusVisibleState};\n}\n\n/**\n * Listens for trigger change and reports if focus is visible (i.e., modality is not pointer).\n */\nexport function useFocusVisibleListener(fn: FocusVisibleHandler, deps: ReadonlyArray<any>, opts?: {isTextInput?: boolean}): void {\n setupGlobalFocusEvents();\n\n useEffect(() => {\n let handler = (modality: Modality, e: HandlerEvent) => {\n if (!isKeyboardFocusEvent(!!(opts?.isTextInput), modality, e)) {\n return;\n }\n fn(isFocusVisible());\n };\n changeHandlers.add(handler);\n return () => {\n changeHandlers.delete(handler);\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, deps);\n}\n"],"names":[],"version":3,"file":"useFocusVisible.module.js.map"} |