@react-aria/interactions 3.26.0 → 3.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PressResponder.main.js +2 -3
- package/dist/PressResponder.main.js.map +1 -1
- package/dist/PressResponder.mjs +3 -4
- package/dist/PressResponder.module.js +3 -4
- package/dist/PressResponder.module.js.map +1 -1
- package/dist/focusSafely.main.js +1 -0
- package/dist/focusSafely.main.js.map +1 -1
- package/dist/focusSafely.mjs +1 -0
- package/dist/focusSafely.module.js +1 -0
- package/dist/focusSafely.module.js.map +1 -1
- package/dist/main.js.map +1 -1
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useFocus.main.js +4 -3
- package/dist/useFocus.main.js.map +1 -1
- package/dist/useFocus.mjs +5 -4
- package/dist/useFocus.module.js +5 -4
- package/dist/useFocus.module.js.map +1 -1
- package/dist/useFocusVisible.main.js +16 -13
- package/dist/useFocusVisible.main.js.map +1 -1
- package/dist/useFocusVisible.mjs +18 -15
- package/dist/useFocusVisible.module.js +18 -15
- package/dist/useFocusVisible.module.js.map +1 -1
- package/dist/useFocusWithin.main.js +9 -7
- package/dist/useFocusWithin.main.js.map +1 -1
- package/dist/useFocusWithin.mjs +10 -8
- package/dist/useFocusWithin.module.js +10 -8
- package/dist/useFocusWithin.module.js.map +1 -1
- package/dist/useHover.main.js +5 -5
- package/dist/useHover.main.js.map +1 -1
- package/dist/useHover.mjs +6 -6
- package/dist/useHover.module.js +6 -6
- package/dist/useHover.module.js.map +1 -1
- package/dist/useInteractOutside.main.js +5 -4
- package/dist/useInteractOutside.main.js.map +1 -1
- package/dist/useInteractOutside.mjs +6 -5
- package/dist/useInteractOutside.module.js +6 -5
- package/dist/useInteractOutside.module.js.map +1 -1
- package/dist/usePress.main.js +31 -23
- package/dist/usePress.main.js.map +1 -1
- package/dist/usePress.mjs +31 -23
- package/dist/usePress.module.js +31 -23
- package/dist/usePress.module.js.map +1 -1
- package/dist/utils.main.js +8 -7
- package/dist/utils.main.js.map +1 -1
- package/dist/utils.mjs +9 -8
- package/dist/utils.module.js +9 -8
- package/dist/utils.module.js.map +1 -1
- package/package.json +4 -4
- package/src/PressResponder.tsx +3 -4
- package/src/focusSafely.ts +4 -0
- package/src/index.ts +1 -1
- package/src/useFocus.ts +4 -3
- package/src/useFocusVisible.ts +18 -12
- package/src/useFocusWithin.ts +9 -7
- package/src/useHover.ts +6 -6
- package/src/useInteractOutside.ts +6 -5
- package/src/usePress.ts +46 -32
- package/src/utils.ts +12 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC,GAED,kEAAkE;AAClE,2DAA2D;AAC3D,yDAAyD;AACzD,kHAAkH;;;;;AAwBlH,IAAI,wCAAmC;AACvC,IAAI,2CAAkC;AACtC,IAAI,uCAAiB,IAAI;AAIlB,IAAI,4CAA2D,IAAI,OAAmC,yFAAyF;AACtM,IAAI,4CAAsB;AAC1B,IAAI,iDAA2B;AAE/B,sEAAsE;AACtE,MAAM,iDAA2B;IAC/B,KAAK;IACL,QAAQ;AACV;AAEA,SAAS,4CAAsB,QAAkB,EAAE,CAAe;IAChE,KAAK,IAAI,WAAW,qCAClB,QAAQ,UAAU;AAEtB;AAEA;;CAEC,GACD,SAAS,iCAAW,CAAgB;IAClC,gFAAgF;IAChF,OAAO,CAAE,CAAA,EAAE,OAAO,IAAK,CAAC,CAAA,GAAA,2BAAI,OAAO,EAAE,MAAM,IAAK,EAAE,OAAO,IAAI,EAAE,GAAG,KAAK,aAAa,EAAE,GAAG,KAAK,WAAW,EAAE,GAAG,KAAK,MAAK;AAC1H;AAGA,SAAS,0CAAoB,CAAgB;IAC3C,4CAAsB;IACtB,IAAI,CAAC,AAAC,CAAA,GAAA,8BAAO,EAAU,SAAS,IAAI,iCAAW,IAAI;QACjD,wCAAkB;QAClB,2CAAqB;QACrB,4CAAsB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S,0CAAwB,EAAuB,EAAE,IAAwB,EAAE,IAA8B;IACvH;IAEA,CAAA,GAAA,sBAAQ,EAAE;QACR,IAAI,UAAU,CAAC,UAAoB;YACjC,0GAA0G;YAC1G,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, openLink} from '@react-aria/utils';\nimport {ignoreFocusEvent} from './utils';\nimport {PointerType} from '@react-types/shared';\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 currentPointerType: PointerType = 'keyboard';\nlet changeHandlers = new Set<Handler>();\ninterface GlobalListenerData {\n focus: () => void\n}\nexport let hasSetupGlobalListeners: Map<Window, GlobalListenerData> = 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 (!(openLink as any).isOpening && isValidKey(e)) {\n currentModality = 'keyboard';\n currentPointerType = 'keyboard';\n triggerChangeHandlers('keyboard', e);\n }\n}\n\nfunction handlePointerEvent(e: PointerEvent | MouseEvent) {\n currentModality = 'pointer';\n currentPointerType = 'pointerType' in e ? e.pointerType as PointerType : 'mouse';\n if (e.type === 'mousedown' || e.type === 'pointerdown') {\n hasEventBeforeFocus = true;\n triggerChangeHandlers('pointer', e);\n }\n}\n\nfunction handleClickEvent(e: MouseEvent) {\n if (!(openLink as any).isOpening && isVirtualClick(e)) {\n hasEventBeforeFocus = true;\n currentModality = 'virtual';\n currentPointerType = '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 || ignoreFocusEvent || !e.isTrusted) {\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 currentPointerType = 'virtual';\n triggerChangeHandlers('virtual', e);\n }\n\n hasEventBeforeFocus = false;\n hasBlurredWindowRecently = false;\n}\n\nfunction handleWindowBlur() {\n if (ignoreFocusEvent) {\n return;\n }\n\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' || typeof document === '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 if (process.env.NODE_ENV === 'test') {\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\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 if (process.env.NODE_ENV === 'test') {\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): void {\n currentModality = modality;\n currentPointerType = modality === 'pointer' ? 'mouse' : modality;\n triggerChangeHandlers(modality, null);\n}\n\n/** @private */\nexport function getPointerType(): PointerType {\n return currentPointerType;\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 let document = getOwnerDocument(e?.target as Element);\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 // For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)\n // we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element\n isTextInput = isTextInput ||\n (document.activeElement instanceof IHTMLInputElement && !nonTextInputTypes.has(document.activeElement.type)) ||\n document.activeElement instanceof IHTMLTextAreaElement ||\n (document.activeElement instanceof IHTMLElement && document.activeElement.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 // We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape\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.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;CAUC,GAED,kEAAkE;AAClE,2DAA2D;AAC3D,yDAAyD;AACzD,kHAAkH;;;;;AAwBlH,IAAI,wCAAmC;AACvC,IAAI,2CAAkC;AAC/B,MAAM,4CAAiB,IAAI;AAI3B,IAAI,4CAA2D,IAAI,OAAmC,yFAAyF;AACtM,IAAI,4CAAsB;AAC1B,IAAI,iDAA2B;AAE/B,sEAAsE;AACtE,MAAM,iDAA2B;IAC/B,KAAK;IACL,QAAQ;AACV;AAEA,SAAS,4CAAsB,QAAkB,EAAE,CAAe;IAChE,KAAK,IAAI,WAAW,0CAClB,QAAQ,UAAU;AAEtB;AAEA;;CAEC,GACD,SAAS,iCAAW,CAAgB;IAClC,gFAAgF;IAChF,OAAO,CAAE,CAAA,EAAE,OAAO,IAAK,CAAC,CAAA,GAAA,2BAAI,OAAO,EAAE,MAAM,IAAK,EAAE,OAAO,IAAI,EAAE,GAAG,KAAK,aAAa,EAAE,GAAG,KAAK,WAAW,EAAE,GAAG,KAAK,MAAK;AAC1H;AAGA,SAAS,0CAAoB,CAAgB;IAC3C,4CAAsB;IACtB,IAAI,CAAC,AAAC,CAAA,GAAA,8BAAO,EAAU,SAAS,IAAI,iCAAW,IAAI;QACjD,wCAAkB;QAClB,2CAAqB;QACrB,4CAAsB,YAAY;IACpC;AACF;AAEA,SAAS,yCAAmB,CAA4B;IACtD,wCAAkB;IAClB,2CAAqB,iBAAiB,IAAI,EAAE,WAAW,GAAkB;IACzE,IAAI,EAAE,IAAI,KAAK,eAAe,EAAE,IAAI,KAAK,eAAe;QACtD,4CAAsB;QACtB,4CAAsB,WAAW;IACnC;AACF;AAEA,SAAS,uCAAiB,CAAa;IACr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G,UAAU,IACzD;YAEF,GAAG;QACL;QACA,0CAAe,GAAG,CAAC;QACnB,OAAO;YACL,0CAAe,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 {getActiveElement, getEventTarget, getOwnerDocument, getOwnerWindow, isMac, isVirtualClick, openLink} from '@react-aria/utils';\nimport {ignoreFocusEvent} from './utils';\nimport {PointerType} from '@react-types/shared';\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 currentPointerType: PointerType = 'keyboard';\nexport const changeHandlers = new Set<Handler>();\ninterface GlobalListenerData {\n focus: () => void\n}\nexport let hasSetupGlobalListeners: Map<Window, GlobalListenerData> = 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 (!(openLink as any).isOpening && isValidKey(e)) {\n currentModality = 'keyboard';\n currentPointerType = 'keyboard';\n triggerChangeHandlers('keyboard', e);\n }\n}\n\nfunction handlePointerEvent(e: PointerEvent | MouseEvent) {\n currentModality = 'pointer';\n currentPointerType = 'pointerType' in e ? e.pointerType as PointerType : 'mouse';\n if (e.type === 'mousedown' || e.type === 'pointerdown') {\n hasEventBeforeFocus = true;\n triggerChangeHandlers('pointer', e);\n }\n}\n\nfunction handleClickEvent(e: MouseEvent) {\n if (!(openLink as any).isOpening && isVirtualClick(e)) {\n hasEventBeforeFocus = true;\n currentModality = 'virtual';\n currentPointerType = '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 (getEventTarget(e) === window || getEventTarget(e) === document || ignoreFocusEvent || !e.isTrusted) {\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 currentPointerType = 'virtual';\n triggerChangeHandlers('virtual', e);\n }\n\n hasEventBeforeFocus = false;\n hasBlurredWindowRecently = false;\n}\n\nfunction handleWindowBlur() {\n if (ignoreFocusEvent) {\n return;\n }\n\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' || typeof document === '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 if (process.env.NODE_ENV === 'test') {\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\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 if (process.env.NODE_ENV === 'test') {\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): void {\n currentModality = modality;\n currentPointerType = modality === 'pointer' ? 'mouse' : modality;\n triggerChangeHandlers(modality, null);\n}\n\n/** @private */\nexport function getPointerType(): PointerType {\n return currentPointerType;\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 let document = getOwnerDocument(e ? getEventTarget(e) as Element : undefined);\n let eventTarget = e ? getEventTarget(e) as Element : undefined;\n const IHTMLInputElement = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).HTMLInputElement : HTMLInputElement;\n const IHTMLTextAreaElement = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).HTMLTextAreaElement : HTMLTextAreaElement;\n const IHTMLElement = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).HTMLElement : HTMLElement;\n const IKeyboardEvent = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).KeyboardEvent : KeyboardEvent;\n\n // For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)\n // we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element\n let activeElement = getActiveElement(document);\n isTextInput = isTextInput ||\n (activeElement instanceof IHTMLInputElement && !nonTextInputTypes.has(activeElement.type)) ||\n activeElement instanceof IHTMLTextAreaElement ||\n (activeElement instanceof IHTMLElement && activeElement.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?: {enabled?: boolean, isTextInput?: boolean}): void {\n setupGlobalFocusEvents();\n\n useEffect(() => {\n if (opts?.enabled === false) {\n return;\n }\n let handler = (modality: Modality, e: HandlerEvent) => {\n // We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape\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\n"],"names":[],"version":3,"file":"useFocusVisible.main.js.map"}
|
package/dist/useFocusVisible.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ignoreFocusEvent as $8a9cb279dc87e130$export$fda7da73ab5d4c48} from "./utils.mjs";
|
|
2
|
-
import {isMac as $28AnR$isMac, openLink as $28AnR$openLink, isVirtualClick as $28AnR$isVirtualClick, getOwnerWindow as $28AnR$getOwnerWindow, getOwnerDocument as $28AnR$getOwnerDocument} from "@react-aria/utils";
|
|
2
|
+
import {isMac as $28AnR$isMac, openLink as $28AnR$openLink, isVirtualClick as $28AnR$isVirtualClick, getEventTarget as $28AnR$getEventTarget, getOwnerWindow as $28AnR$getOwnerWindow, getOwnerDocument as $28AnR$getOwnerDocument, getActiveElement as $28AnR$getActiveElement} from "@react-aria/utils";
|
|
3
3
|
import {useState as $28AnR$useState, useEffect as $28AnR$useEffect} from "react";
|
|
4
4
|
import {useIsSSR as $28AnR$useIsSSR} from "@react-aria/ssr";
|
|
5
5
|
|
|
@@ -23,7 +23,7 @@ import {useIsSSR as $28AnR$useIsSSR} from "@react-aria/ssr";
|
|
|
23
23
|
|
|
24
24
|
let $507fabe10e71c6fb$var$currentModality = null;
|
|
25
25
|
let $507fabe10e71c6fb$var$currentPointerType = 'keyboard';
|
|
26
|
-
|
|
26
|
+
const $507fabe10e71c6fb$export$901e90a13c50a14e = new Set();
|
|
27
27
|
let $507fabe10e71c6fb$export$d90243b58daecda7 = new Map(); // We use a map here to support setting event listeners across multiple document objects.
|
|
28
28
|
let $507fabe10e71c6fb$var$hasEventBeforeFocus = false;
|
|
29
29
|
let $507fabe10e71c6fb$var$hasBlurredWindowRecently = false;
|
|
@@ -33,7 +33,7 @@ const $507fabe10e71c6fb$var$FOCUS_VISIBLE_INPUT_KEYS = {
|
|
|
33
33
|
Escape: true
|
|
34
34
|
};
|
|
35
35
|
function $507fabe10e71c6fb$var$triggerChangeHandlers(modality, e) {
|
|
36
|
-
for (let handler of $507fabe10e71c6fb$
|
|
36
|
+
for (let handler of $507fabe10e71c6fb$export$901e90a13c50a14e)handler(modality, e);
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
39
|
* Helper function to determine if a KeyboardEvent is unmodified and could make keyboard focus styles visible.
|
|
@@ -68,7 +68,7 @@ function $507fabe10e71c6fb$var$handleFocusEvent(e) {
|
|
|
68
68
|
// Firefox fires two extra focus events when the user first clicks into an iframe:
|
|
69
69
|
// first on the window, then on the document. We ignore these events so they don't
|
|
70
70
|
// cause keyboard focus rings to appear.
|
|
71
|
-
if (e
|
|
71
|
+
if ((0, $28AnR$getEventTarget)(e) === window || (0, $28AnR$getEventTarget)(e) === document || (0, $8a9cb279dc87e130$export$fda7da73ab5d4c48) || !e.isTrusted) return;
|
|
72
72
|
// If a focus event occurs without a preceding keyboard or pointer event, switch to virtual modality.
|
|
73
73
|
// This occurs, for example, when navigating a form with the next/previous buttons on iOS.
|
|
74
74
|
if (!$507fabe10e71c6fb$var$hasEventBeforeFocus && !$507fabe10e71c6fb$var$hasBlurredWindowRecently) {
|
|
@@ -185,9 +185,9 @@ function $507fabe10e71c6fb$export$98e20ec92f614cfe() {
|
|
|
185
185
|
let handler = ()=>{
|
|
186
186
|
setModality($507fabe10e71c6fb$var$currentModality);
|
|
187
187
|
};
|
|
188
|
-
$507fabe10e71c6fb$
|
|
188
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.add(handler);
|
|
189
189
|
return ()=>{
|
|
190
|
-
$507fabe10e71c6fb$
|
|
190
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.delete(handler);
|
|
191
191
|
};
|
|
192
192
|
}, []);
|
|
193
193
|
return (0, $28AnR$useIsSSR)() ? null : modality;
|
|
@@ -207,14 +207,16 @@ const $507fabe10e71c6fb$var$nonTextInputTypes = new Set([
|
|
|
207
207
|
* If this is attached to text input component, return if the event is a focus event (Tab/Escape keys pressed) so that
|
|
208
208
|
* focus visible style can be properly set.
|
|
209
209
|
*/ function $507fabe10e71c6fb$var$isKeyboardFocusEvent(isTextInput, modality, e) {
|
|
210
|
-
let document1 = (0, $28AnR$getOwnerDocument)(e
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
const
|
|
214
|
-
const
|
|
210
|
+
let document1 = (0, $28AnR$getOwnerDocument)(e ? (0, $28AnR$getEventTarget)(e) : undefined);
|
|
211
|
+
let eventTarget = e ? (0, $28AnR$getEventTarget)(e) : undefined;
|
|
212
|
+
const IHTMLInputElement = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).HTMLInputElement : HTMLInputElement;
|
|
213
|
+
const IHTMLTextAreaElement = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).HTMLTextAreaElement : HTMLTextAreaElement;
|
|
214
|
+
const IHTMLElement = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).HTMLElement : HTMLElement;
|
|
215
|
+
const IKeyboardEvent = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).KeyboardEvent : KeyboardEvent;
|
|
215
216
|
// For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)
|
|
216
217
|
// we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element
|
|
217
|
-
|
|
218
|
+
let activeElement = (0, $28AnR$getActiveElement)(document1);
|
|
219
|
+
isTextInput = isTextInput || activeElement instanceof IHTMLInputElement && !$507fabe10e71c6fb$var$nonTextInputTypes.has(activeElement.type) || activeElement instanceof IHTMLTextAreaElement || activeElement instanceof IHTMLElement && activeElement.isContentEditable;
|
|
218
220
|
return !(isTextInput && modality === 'keyboard' && e instanceof IKeyboardEvent && !$507fabe10e71c6fb$var$FOCUS_VISIBLE_INPUT_KEYS[e.key]);
|
|
219
221
|
}
|
|
220
222
|
function $507fabe10e71c6fb$export$ffd9e5021c1fb2d6(props = {}) {
|
|
@@ -234,19 +236,20 @@ function $507fabe10e71c6fb$export$ffd9e5021c1fb2d6(props = {}) {
|
|
|
234
236
|
function $507fabe10e71c6fb$export$ec71b4b83ac08ec3(fn, deps, opts) {
|
|
235
237
|
$507fabe10e71c6fb$var$setupGlobalFocusEvents();
|
|
236
238
|
(0, $28AnR$useEffect)(()=>{
|
|
239
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.enabled) === false) return;
|
|
237
240
|
let handler = (modality, e)=>{
|
|
238
241
|
// We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape
|
|
239
242
|
if (!$507fabe10e71c6fb$var$isKeyboardFocusEvent(!!(opts === null || opts === void 0 ? void 0 : opts.isTextInput), modality, e)) return;
|
|
240
243
|
fn($507fabe10e71c6fb$export$b9b3dfddab17db27());
|
|
241
244
|
};
|
|
242
|
-
$507fabe10e71c6fb$
|
|
245
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.add(handler);
|
|
243
246
|
return ()=>{
|
|
244
|
-
$507fabe10e71c6fb$
|
|
247
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.delete(handler);
|
|
245
248
|
};
|
|
246
249
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
247
250
|
}, deps);
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
|
|
251
|
-
export {$507fabe10e71c6fb$export$d90243b58daecda7 as hasSetupGlobalListeners, $507fabe10e71c6fb$export$2f1888112f558a7d as addWindowFocusTracking, $507fabe10e71c6fb$export$b9b3dfddab17db27 as isFocusVisible, $507fabe10e71c6fb$export$630ff653c5ada6a9 as getInteractionModality, $507fabe10e71c6fb$export$8397ddfc504fdb9a as setInteractionModality, $507fabe10e71c6fb$export$887a228355cf7d95 as getPointerType, $507fabe10e71c6fb$export$98e20ec92f614cfe as useInteractionModality, $507fabe10e71c6fb$export$ffd9e5021c1fb2d6 as useFocusVisible, $507fabe10e71c6fb$export$ec71b4b83ac08ec3 as useFocusVisibleListener};
|
|
254
|
+
export {$507fabe10e71c6fb$export$901e90a13c50a14e as changeHandlers, $507fabe10e71c6fb$export$d90243b58daecda7 as hasSetupGlobalListeners, $507fabe10e71c6fb$export$2f1888112f558a7d as addWindowFocusTracking, $507fabe10e71c6fb$export$b9b3dfddab17db27 as isFocusVisible, $507fabe10e71c6fb$export$630ff653c5ada6a9 as getInteractionModality, $507fabe10e71c6fb$export$8397ddfc504fdb9a as setInteractionModality, $507fabe10e71c6fb$export$887a228355cf7d95 as getPointerType, $507fabe10e71c6fb$export$98e20ec92f614cfe as useInteractionModality, $507fabe10e71c6fb$export$ffd9e5021c1fb2d6 as useFocusVisible, $507fabe10e71c6fb$export$ec71b4b83ac08ec3 as useFocusVisibleListener};
|
|
252
255
|
//# sourceMappingURL=useFocusVisible.module.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {ignoreFocusEvent as $8a9cb279dc87e130$export$fda7da73ab5d4c48} from "./utils.module.js";
|
|
2
|
-
import {isMac as $28AnR$isMac, openLink as $28AnR$openLink, isVirtualClick as $28AnR$isVirtualClick, getOwnerWindow as $28AnR$getOwnerWindow, getOwnerDocument as $28AnR$getOwnerDocument} from "@react-aria/utils";
|
|
2
|
+
import {isMac as $28AnR$isMac, openLink as $28AnR$openLink, isVirtualClick as $28AnR$isVirtualClick, getEventTarget as $28AnR$getEventTarget, getOwnerWindow as $28AnR$getOwnerWindow, getOwnerDocument as $28AnR$getOwnerDocument, getActiveElement as $28AnR$getActiveElement} from "@react-aria/utils";
|
|
3
3
|
import {useState as $28AnR$useState, useEffect as $28AnR$useEffect} from "react";
|
|
4
4
|
import {useIsSSR as $28AnR$useIsSSR} from "@react-aria/ssr";
|
|
5
5
|
|
|
@@ -23,7 +23,7 @@ import {useIsSSR as $28AnR$useIsSSR} from "@react-aria/ssr";
|
|
|
23
23
|
|
|
24
24
|
let $507fabe10e71c6fb$var$currentModality = null;
|
|
25
25
|
let $507fabe10e71c6fb$var$currentPointerType = 'keyboard';
|
|
26
|
-
|
|
26
|
+
const $507fabe10e71c6fb$export$901e90a13c50a14e = new Set();
|
|
27
27
|
let $507fabe10e71c6fb$export$d90243b58daecda7 = new Map(); // We use a map here to support setting event listeners across multiple document objects.
|
|
28
28
|
let $507fabe10e71c6fb$var$hasEventBeforeFocus = false;
|
|
29
29
|
let $507fabe10e71c6fb$var$hasBlurredWindowRecently = false;
|
|
@@ -33,7 +33,7 @@ const $507fabe10e71c6fb$var$FOCUS_VISIBLE_INPUT_KEYS = {
|
|
|
33
33
|
Escape: true
|
|
34
34
|
};
|
|
35
35
|
function $507fabe10e71c6fb$var$triggerChangeHandlers(modality, e) {
|
|
36
|
-
for (let handler of $507fabe10e71c6fb$
|
|
36
|
+
for (let handler of $507fabe10e71c6fb$export$901e90a13c50a14e)handler(modality, e);
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
39
|
* Helper function to determine if a KeyboardEvent is unmodified and could make keyboard focus styles visible.
|
|
@@ -68,7 +68,7 @@ function $507fabe10e71c6fb$var$handleFocusEvent(e) {
|
|
|
68
68
|
// Firefox fires two extra focus events when the user first clicks into an iframe:
|
|
69
69
|
// first on the window, then on the document. We ignore these events so they don't
|
|
70
70
|
// cause keyboard focus rings to appear.
|
|
71
|
-
if (e
|
|
71
|
+
if ((0, $28AnR$getEventTarget)(e) === window || (0, $28AnR$getEventTarget)(e) === document || (0, $8a9cb279dc87e130$export$fda7da73ab5d4c48) || !e.isTrusted) return;
|
|
72
72
|
// If a focus event occurs without a preceding keyboard or pointer event, switch to virtual modality.
|
|
73
73
|
// This occurs, for example, when navigating a form with the next/previous buttons on iOS.
|
|
74
74
|
if (!$507fabe10e71c6fb$var$hasEventBeforeFocus && !$507fabe10e71c6fb$var$hasBlurredWindowRecently) {
|
|
@@ -185,9 +185,9 @@ function $507fabe10e71c6fb$export$98e20ec92f614cfe() {
|
|
|
185
185
|
let handler = ()=>{
|
|
186
186
|
setModality($507fabe10e71c6fb$var$currentModality);
|
|
187
187
|
};
|
|
188
|
-
$507fabe10e71c6fb$
|
|
188
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.add(handler);
|
|
189
189
|
return ()=>{
|
|
190
|
-
$507fabe10e71c6fb$
|
|
190
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.delete(handler);
|
|
191
191
|
};
|
|
192
192
|
}, []);
|
|
193
193
|
return (0, $28AnR$useIsSSR)() ? null : modality;
|
|
@@ -207,14 +207,16 @@ const $507fabe10e71c6fb$var$nonTextInputTypes = new Set([
|
|
|
207
207
|
* If this is attached to text input component, return if the event is a focus event (Tab/Escape keys pressed) so that
|
|
208
208
|
* focus visible style can be properly set.
|
|
209
209
|
*/ function $507fabe10e71c6fb$var$isKeyboardFocusEvent(isTextInput, modality, e) {
|
|
210
|
-
let document1 = (0, $28AnR$getOwnerDocument)(e
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
const
|
|
214
|
-
const
|
|
210
|
+
let document1 = (0, $28AnR$getOwnerDocument)(e ? (0, $28AnR$getEventTarget)(e) : undefined);
|
|
211
|
+
let eventTarget = e ? (0, $28AnR$getEventTarget)(e) : undefined;
|
|
212
|
+
const IHTMLInputElement = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).HTMLInputElement : HTMLInputElement;
|
|
213
|
+
const IHTMLTextAreaElement = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).HTMLTextAreaElement : HTMLTextAreaElement;
|
|
214
|
+
const IHTMLElement = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).HTMLElement : HTMLElement;
|
|
215
|
+
const IKeyboardEvent = typeof window !== 'undefined' ? (0, $28AnR$getOwnerWindow)(eventTarget).KeyboardEvent : KeyboardEvent;
|
|
215
216
|
// For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)
|
|
216
217
|
// we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element
|
|
217
|
-
|
|
218
|
+
let activeElement = (0, $28AnR$getActiveElement)(document1);
|
|
219
|
+
isTextInput = isTextInput || activeElement instanceof IHTMLInputElement && !$507fabe10e71c6fb$var$nonTextInputTypes.has(activeElement.type) || activeElement instanceof IHTMLTextAreaElement || activeElement instanceof IHTMLElement && activeElement.isContentEditable;
|
|
218
220
|
return !(isTextInput && modality === 'keyboard' && e instanceof IKeyboardEvent && !$507fabe10e71c6fb$var$FOCUS_VISIBLE_INPUT_KEYS[e.key]);
|
|
219
221
|
}
|
|
220
222
|
function $507fabe10e71c6fb$export$ffd9e5021c1fb2d6(props = {}) {
|
|
@@ -234,19 +236,20 @@ function $507fabe10e71c6fb$export$ffd9e5021c1fb2d6(props = {}) {
|
|
|
234
236
|
function $507fabe10e71c6fb$export$ec71b4b83ac08ec3(fn, deps, opts) {
|
|
235
237
|
$507fabe10e71c6fb$var$setupGlobalFocusEvents();
|
|
236
238
|
(0, $28AnR$useEffect)(()=>{
|
|
239
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.enabled) === false) return;
|
|
237
240
|
let handler = (modality, e)=>{
|
|
238
241
|
// We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape
|
|
239
242
|
if (!$507fabe10e71c6fb$var$isKeyboardFocusEvent(!!(opts === null || opts === void 0 ? void 0 : opts.isTextInput), modality, e)) return;
|
|
240
243
|
fn($507fabe10e71c6fb$export$b9b3dfddab17db27());
|
|
241
244
|
};
|
|
242
|
-
$507fabe10e71c6fb$
|
|
245
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.add(handler);
|
|
243
246
|
return ()=>{
|
|
244
|
-
$507fabe10e71c6fb$
|
|
247
|
+
$507fabe10e71c6fb$export$901e90a13c50a14e.delete(handler);
|
|
245
248
|
};
|
|
246
249
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
247
250
|
}, deps);
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
|
|
251
|
-
export {$507fabe10e71c6fb$export$d90243b58daecda7 as hasSetupGlobalListeners, $507fabe10e71c6fb$export$2f1888112f558a7d as addWindowFocusTracking, $507fabe10e71c6fb$export$b9b3dfddab17db27 as isFocusVisible, $507fabe10e71c6fb$export$630ff653c5ada6a9 as getInteractionModality, $507fabe10e71c6fb$export$8397ddfc504fdb9a as setInteractionModality, $507fabe10e71c6fb$export$887a228355cf7d95 as getPointerType, $507fabe10e71c6fb$export$98e20ec92f614cfe as useInteractionModality, $507fabe10e71c6fb$export$ffd9e5021c1fb2d6 as useFocusVisible, $507fabe10e71c6fb$export$ec71b4b83ac08ec3 as useFocusVisibleListener};
|
|
254
|
+
export {$507fabe10e71c6fb$export$901e90a13c50a14e as changeHandlers, $507fabe10e71c6fb$export$d90243b58daecda7 as hasSetupGlobalListeners, $507fabe10e71c6fb$export$2f1888112f558a7d as addWindowFocusTracking, $507fabe10e71c6fb$export$b9b3dfddab17db27 as isFocusVisible, $507fabe10e71c6fb$export$630ff653c5ada6a9 as getInteractionModality, $507fabe10e71c6fb$export$8397ddfc504fdb9a as setInteractionModality, $507fabe10e71c6fb$export$887a228355cf7d95 as getPointerType, $507fabe10e71c6fb$export$98e20ec92f614cfe as useInteractionModality, $507fabe10e71c6fb$export$ffd9e5021c1fb2d6 as useFocusVisible, $507fabe10e71c6fb$export$ec71b4b83ac08ec3 as useFocusVisibleListener};
|
|
252
255
|
//# sourceMappingURL=useFocusVisible.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;AAAA;;;;;;;;;;CAUC,GAED,kEAAkE;AAClE,2DAA2D;AAC3D,yDAAyD;AACzD,kHAAkH;;;;;AAwBlH,IAAI,wCAAmC;AACvC,IAAI,2CAAkC;AACtC,IAAI,uCAAiB,IAAI;AAIlB,IAAI,4CAA2D,IAAI,OAAmC,yFAAyF;AACtM,IAAI,4CAAsB;AAC1B,IAAI,iDAA2B;AAE/B,sEAAsE;AACtE,MAAM,iDAA2B;IAC/B,KAAK;IACL,QAAQ;AACV;AAEA,SAAS,4CAAsB,QAAkB,EAAE,CAAe;IAChE,KAAK,IAAI,WAAW,qCA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AAa,CAAC,iBAAiB;IAC7F,OAAO,CAAE,CAAA,eAAe,aAAa,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,0GAA0G;YAC1G,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, openLink} from '@react-aria/utils';\nimport {ignoreFocusEvent} from './utils';\nimport {PointerType} from '@react-types/shared';\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 currentPointerType: PointerType = 'keyboard';\nlet changeHandlers = new Set<Handler>();\ninterface GlobalListenerData {\n focus: () => void\n}\nexport let hasSetupGlobalListeners: Map<Window, GlobalListenerData> = 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 (!(openLink as any).isOpening && isValidKey(e)) {\n currentModality = 'keyboard';\n currentPointerType = 'keyboard';\n triggerChangeHandlers('keyboard', e);\n }\n}\n\nfunction handlePointerEvent(e: PointerEvent | MouseEvent) {\n currentModality = 'pointer';\n currentPointerType = 'pointerType' in e ? e.pointerType as PointerType : 'mouse';\n if (e.type === 'mousedown' || e.type === 'pointerdown') {\n hasEventBeforeFocus = true;\n triggerChangeHandlers('pointer', e);\n }\n}\n\nfunction handleClickEvent(e: MouseEvent) {\n if (!(openLink as any).isOpening && isVirtualClick(e)) {\n hasEventBeforeFocus = true;\n currentModality = 'virtual';\n currentPointerType = '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 || ignoreFocusEvent || !e.isTrusted) {\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 currentPointerType = 'virtual';\n triggerChangeHandlers('virtual', e);\n }\n\n hasEventBeforeFocus = false;\n hasBlurredWindowRecently = false;\n}\n\nfunction handleWindowBlur() {\n if (ignoreFocusEvent) {\n return;\n }\n\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' || typeof document === '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 if (process.env.NODE_ENV === 'test') {\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\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 if (process.env.NODE_ENV === 'test') {\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): void {\n currentModality = modality;\n currentPointerType = modality === 'pointer' ? 'mouse' : modality;\n triggerChangeHandlers(modality, null);\n}\n\n/** @private */\nexport function getPointerType(): PointerType {\n return currentPointerType;\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 let document = getOwnerDocument(e?.target as Element);\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 // For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)\n // we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element\n isTextInput = isTextInput ||\n (document.activeElement instanceof IHTMLInputElement && !nonTextInputTypes.has(document.activeElement.type)) ||\n document.activeElement instanceof IHTMLTextAreaElement ||\n (document.activeElement instanceof IHTMLElement && document.activeElement.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 // We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape\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"}
|
|
1
|
+
{"mappings":";;;;;AAAA;;;;;;;;;;CAUC,GAED,kEAAkE;AAClE,2DAA2D;AAC3D,yDAAyD;AACzD,kHAAkH;;;;;AAwBlH,IAAI,wCAAmC;AACvC,IAAI,2CAAkC;AAC/B,MAAM,4CAAiB,IAAI;AAI3B,IAAI,4CAA2D,IAAI,OAAmC,yFAAyF;AACtM,IAAI,4CAAsB;AAC1B,IAAI,iDAA2B;AAE/B,sEAAsE;AACtE,MAAM,iDAA2B;IAC/B,KAAK;IACL,QAAQ;AACV;AAEA,SAAS,4CAAsB,QAAkB,EAAE,CAAe;IAChE,KAAK,IAAI,WAAW,0CAClB,QAAQ,UAAU;AAEtB;AAEA;;CAEC,GACD,SAAS,iCAAW,CAAgB;IAClC,gFAAgF;IAChF,OAAO,CAAE,CAAA,EAAE,OAAO,IAAK,CAAC,CAAA,GAAA,YAAI,OAAO,EAAE,MAAM,IAAK,EAAE,OAAO,IAAI,EAAE,GAAG,KAAK,aAAa,EAAE,GAAG,KAAK,WAAW,EAAE,GAAG,KAAK,MAAK;AAC1H;AA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AAE,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,IAAiD;IAC1I;IAEA,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,CAAA,iBAAA,2BAAA,KAAM,OAAO,MAAK,OACpB;QAEF,IAAI,UAAU,CAAC,UAAoB;YACjC,0GAA0G;YAC1G,IAAI,CAAC,2CAAqB,CAAC,EAAE,iBAAA,2BAAA,KAAM,WAAW,GAAG,UAAU,IACzD;YAEF,GAAG;QACL;QACA,0CAAe,GAAG,CAAC;QACnB,OAAO;YACL,0CAAe,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 {getActiveElement, getEventTarget, getOwnerDocument, getOwnerWindow, isMac, isVirtualClick, openLink} from '@react-aria/utils';\nimport {ignoreFocusEvent} from './utils';\nimport {PointerType} from '@react-types/shared';\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 currentPointerType: PointerType = 'keyboard';\nexport const changeHandlers = new Set<Handler>();\ninterface GlobalListenerData {\n focus: () => void\n}\nexport let hasSetupGlobalListeners: Map<Window, GlobalListenerData> = 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 (!(openLink as any).isOpening && isValidKey(e)) {\n currentModality = 'keyboard';\n currentPointerType = 'keyboard';\n triggerChangeHandlers('keyboard', e);\n }\n}\n\nfunction handlePointerEvent(e: PointerEvent | MouseEvent) {\n currentModality = 'pointer';\n currentPointerType = 'pointerType' in e ? e.pointerType as PointerType : 'mouse';\n if (e.type === 'mousedown' || e.type === 'pointerdown') {\n hasEventBeforeFocus = true;\n triggerChangeHandlers('pointer', e);\n }\n}\n\nfunction handleClickEvent(e: MouseEvent) {\n if (!(openLink as any).isOpening && isVirtualClick(e)) {\n hasEventBeforeFocus = true;\n currentModality = 'virtual';\n currentPointerType = '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 (getEventTarget(e) === window || getEventTarget(e) === document || ignoreFocusEvent || !e.isTrusted) {\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 currentPointerType = 'virtual';\n triggerChangeHandlers('virtual', e);\n }\n\n hasEventBeforeFocus = false;\n hasBlurredWindowRecently = false;\n}\n\nfunction handleWindowBlur() {\n if (ignoreFocusEvent) {\n return;\n }\n\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' || typeof document === '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 if (process.env.NODE_ENV === 'test') {\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\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 if (process.env.NODE_ENV === 'test') {\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): void {\n currentModality = modality;\n currentPointerType = modality === 'pointer' ? 'mouse' : modality;\n triggerChangeHandlers(modality, null);\n}\n\n/** @private */\nexport function getPointerType(): PointerType {\n return currentPointerType;\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 let document = getOwnerDocument(e ? getEventTarget(e) as Element : undefined);\n let eventTarget = e ? getEventTarget(e) as Element : undefined;\n const IHTMLInputElement = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).HTMLInputElement : HTMLInputElement;\n const IHTMLTextAreaElement = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).HTMLTextAreaElement : HTMLTextAreaElement;\n const IHTMLElement = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).HTMLElement : HTMLElement;\n const IKeyboardEvent = typeof window !== 'undefined' ? getOwnerWindow(eventTarget).KeyboardEvent : KeyboardEvent;\n\n // For keyboard events that occur on a non-input element that will move focus into input element (aka ArrowLeft going from Datepicker button to the main input group)\n // we need to rely on the user passing isTextInput into here. This way we can skip toggling focus visiblity for said input element\n let activeElement = getActiveElement(document);\n isTextInput = isTextInput ||\n (activeElement instanceof IHTMLInputElement && !nonTextInputTypes.has(activeElement.type)) ||\n activeElement instanceof IHTMLTextAreaElement ||\n (activeElement instanceof IHTMLElement && activeElement.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?: {enabled?: boolean, isTextInput?: boolean}): void {\n setupGlobalFocusEvents();\n\n useEffect(() => {\n if (opts?.enabled === false) {\n return;\n }\n let handler = (modality: Modality, e: HandlerEvent) => {\n // We want to early return for any keyboard events that occur inside text inputs EXCEPT for Tab and Escape\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\n"],"names":[],"version":3,"file":"useFocusVisible.module.js.map"}
|
|
@@ -33,11 +33,11 @@ function $d16842bbd0359d1b$export$420e68273165f4ec(props) {
|
|
|
33
33
|
let { addGlobalListener: addGlobalListener, removeAllGlobalListeners: removeAllGlobalListeners } = (0, $kDAhS$reactariautils.useGlobalListeners)();
|
|
34
34
|
let onBlur = (0, $kDAhS$react.useCallback)((e)=>{
|
|
35
35
|
// Ignore events bubbling through portals.
|
|
36
|
-
if (!e.currentTarget.
|
|
36
|
+
if (!(0, $kDAhS$reactariautils.nodeContains)(e.currentTarget, (0, $kDAhS$reactariautils.getEventTarget)(e))) return;
|
|
37
37
|
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
38
38
|
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
39
39
|
// include the relatedTarget (where focus is moving).
|
|
40
|
-
if (state.current.isFocusWithin && !e.currentTarget
|
|
40
|
+
if (state.current.isFocusWithin && !(0, $kDAhS$reactariautils.nodeContains)(e.currentTarget, e.relatedTarget)) {
|
|
41
41
|
state.current.isFocusWithin = false;
|
|
42
42
|
removeAllGlobalListeners();
|
|
43
43
|
if (onBlurWithin) onBlurWithin(e);
|
|
@@ -52,12 +52,13 @@ function $d16842bbd0359d1b$export$420e68273165f4ec(props) {
|
|
|
52
52
|
let onSyntheticFocus = (0, $625cf83917e112ad$exports.useSyntheticBlurEvent)(onBlur);
|
|
53
53
|
let onFocus = (0, $kDAhS$react.useCallback)((e)=>{
|
|
54
54
|
// Ignore events bubbling through portals.
|
|
55
|
-
if (!e.currentTarget.
|
|
55
|
+
if (!(0, $kDAhS$reactariautils.nodeContains)(e.currentTarget, (0, $kDAhS$reactariautils.getEventTarget)(e))) return;
|
|
56
56
|
// Double check that document.activeElement actually matches e.target in case a previously chained
|
|
57
57
|
// focus handler already moved focus somewhere else.
|
|
58
|
-
|
|
58
|
+
let eventTarget = (0, $kDAhS$reactariautils.getEventTarget)(e);
|
|
59
|
+
const ownerDocument = (0, $kDAhS$reactariautils.getOwnerDocument)(eventTarget);
|
|
59
60
|
const activeElement = (0, $kDAhS$reactariautils.getActiveElement)(ownerDocument);
|
|
60
|
-
if (!state.current.isFocusWithin && activeElement ===
|
|
61
|
+
if (!state.current.isFocusWithin && activeElement === eventTarget) {
|
|
61
62
|
if (onFocusWithin) onFocusWithin(e);
|
|
62
63
|
if (onFocusWithinChange) onFocusWithinChange(true);
|
|
63
64
|
state.current.isFocusWithin = true;
|
|
@@ -67,9 +68,10 @@ function $d16842bbd0359d1b$export$420e68273165f4ec(props) {
|
|
|
67
68
|
// can manually fire onBlur.
|
|
68
69
|
let currentTarget = e.currentTarget;
|
|
69
70
|
addGlobalListener(ownerDocument, 'focus', (e)=>{
|
|
70
|
-
|
|
71
|
+
let eventTarget = (0, $kDAhS$reactariautils.getEventTarget)(e);
|
|
72
|
+
if (state.current.isFocusWithin && !(0, $kDAhS$reactariautils.nodeContains)(currentTarget, eventTarget)) {
|
|
71
73
|
let nativeEvent = new ownerDocument.defaultView.FocusEvent('blur', {
|
|
72
|
-
relatedTarget:
|
|
74
|
+
relatedTarget: eventTarget
|
|
73
75
|
});
|
|
74
76
|
(0, $625cf83917e112ad$exports.setEventTarget)(nativeEvent, currentTarget);
|
|
75
77
|
let event = (0, $625cf83917e112ad$exports.createSynthetic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kBAAkB;qBAChB;oBACA;QACF;IACF;AACF","sources":["packages/@react-aria/interactions/src/useFocusWithin.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 {createSyntheticEvent, setEventTarget, useSyntheticBlurEvent} from './utils';\nimport {DOMAttributes} from '@react-types/shared';\nimport {FocusEvent, useCallback, useRef} from 'react';\nimport {getActiveElement, getEventTarget, getOwnerDocument, nodeContains, useGlobalListeners} from '@react-aria/utils';\n\nexport interface FocusWithinProps {\n /** Whether the focus within events should be disabled. */\n isDisabled?: boolean,\n /** Handler that is called when the target element or a descendant receives focus. */\n onFocusWithin?: (e: FocusEvent) => void,\n /** Handler that is called when the target element and all descendants lose focus. */\n onBlurWithin?: (e: FocusEvent) => void,\n /** Handler that is called when the the focus within state changes. */\n onFocusWithinChange?: (isFocusWithin: boolean) => void\n}\n\nexport interface FocusWithinResult {\n /** Props to spread onto the target element. */\n focusWithinProps: DOMAttributes\n}\n\n/**\n * Handles focus events for the target and its descendants.\n */\nexport function useFocusWithin(props: FocusWithinProps): FocusWithinResult {\n let {\n isDisabled,\n onBlurWithin,\n onFocusWithin,\n onFocusWithinChange\n } = props;\n let state = useRef({\n isFocusWithin: false\n });\n\n let {addGlobalListener, removeAllGlobalListeners} = useGlobalListeners();\n\n let onBlur = useCallback((e: FocusEvent) => {\n // Ignore events bubbling through portals.\n if (!nodeContains(e.currentTarget, getEventTarget(e))) {\n return;\n }\n\n // We don't want to trigger onBlurWithin and then immediately onFocusWithin again\n // when moving focus inside the element. Only trigger if the currentTarget doesn't\n // include the relatedTarget (where focus is moving).\n if (state.current.isFocusWithin && !nodeContains(e.currentTarget as Element, e.relatedTarget as Element)) {\n state.current.isFocusWithin = false;\n removeAllGlobalListeners();\n\n if (onBlurWithin) {\n onBlurWithin(e);\n }\n\n if (onFocusWithinChange) {\n onFocusWithinChange(false);\n }\n }\n }, [onBlurWithin, onFocusWithinChange, state, removeAllGlobalListeners]);\n\n let onSyntheticFocus = useSyntheticBlurEvent(onBlur);\n let onFocus = useCallback((e: FocusEvent) => {\n // Ignore events bubbling through portals.\n if (!nodeContains(e.currentTarget, getEventTarget(e))) {\n return;\n }\n\n // Double check that document.activeElement actually matches e.target in case a previously chained\n // focus handler already moved focus somewhere else.\n let eventTarget = getEventTarget(e);\n const ownerDocument = getOwnerDocument(eventTarget);\n const activeElement = getActiveElement(ownerDocument);\n if (!state.current.isFocusWithin && activeElement === eventTarget) {\n if (onFocusWithin) {\n onFocusWithin(e);\n }\n\n if (onFocusWithinChange) {\n onFocusWithinChange(true);\n }\n\n state.current.isFocusWithin = true;\n onSyntheticFocus(e);\n\n // Browsers don't fire blur events when elements are removed from the DOM.\n // However, if a focus event occurs outside the element we're tracking, we\n // can manually fire onBlur.\n let currentTarget = e.currentTarget;\n addGlobalListener(ownerDocument, 'focus', e => {\n let eventTarget = getEventTarget(e);\n if (state.current.isFocusWithin && !nodeContains(currentTarget, eventTarget as Element)) {\n let nativeEvent = new ownerDocument.defaultView!.FocusEvent('blur', {relatedTarget: eventTarget});\n setEventTarget(nativeEvent, currentTarget);\n let event = createSyntheticEvent<FocusEvent>(nativeEvent);\n onBlur(event);\n }\n }, {capture: true});\n }\n }, [onFocusWithin, onFocusWithinChange, onSyntheticFocus, addGlobalListener, onBlur]);\n\n if (isDisabled) {\n return {\n focusWithinProps: {\n // These cannot be null, that would conflict in mergeProps\n onFocus: undefined,\n onBlur: undefined\n }\n };\n }\n\n return {\n focusWithinProps: {\n onFocus,\n onBlur\n }\n };\n}\n"],"names":[],"version":3,"file":"useFocusWithin.main.js.map"}
|
package/dist/useFocusWithin.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {createSyntheticEvent as $8a9cb279dc87e130$export$525bc4921d56d4a, setEventTarget as $8a9cb279dc87e130$export$c2b7abe5d61ec696, useSyntheticBlurEvent as $8a9cb279dc87e130$export$715c682d09d639cc} from "./utils.mjs";
|
|
2
2
|
import {useRef as $3b9Q0$useRef, useCallback as $3b9Q0$useCallback} from "react";
|
|
3
|
-
import {useGlobalListeners as $3b9Q0$useGlobalListeners,
|
|
3
|
+
import {useGlobalListeners as $3b9Q0$useGlobalListeners, nodeContains as $3b9Q0$nodeContains, getEventTarget as $3b9Q0$getEventTarget, getOwnerDocument as $3b9Q0$getOwnerDocument, getActiveElement as $3b9Q0$getActiveElement} from "@react-aria/utils";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
6
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -27,11 +27,11 @@ function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
|
|
|
27
27
|
let { addGlobalListener: addGlobalListener, removeAllGlobalListeners: removeAllGlobalListeners } = (0, $3b9Q0$useGlobalListeners)();
|
|
28
28
|
let onBlur = (0, $3b9Q0$useCallback)((e)=>{
|
|
29
29
|
// Ignore events bubbling through portals.
|
|
30
|
-
if (!e.currentTarget
|
|
30
|
+
if (!(0, $3b9Q0$nodeContains)(e.currentTarget, (0, $3b9Q0$getEventTarget)(e))) return;
|
|
31
31
|
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
32
32
|
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
33
33
|
// include the relatedTarget (where focus is moving).
|
|
34
|
-
if (state.current.isFocusWithin && !e.currentTarget
|
|
34
|
+
if (state.current.isFocusWithin && !(0, $3b9Q0$nodeContains)(e.currentTarget, e.relatedTarget)) {
|
|
35
35
|
state.current.isFocusWithin = false;
|
|
36
36
|
removeAllGlobalListeners();
|
|
37
37
|
if (onBlurWithin) onBlurWithin(e);
|
|
@@ -46,12 +46,13 @@ function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
|
|
|
46
46
|
let onSyntheticFocus = (0, $8a9cb279dc87e130$export$715c682d09d639cc)(onBlur);
|
|
47
47
|
let onFocus = (0, $3b9Q0$useCallback)((e)=>{
|
|
48
48
|
// Ignore events bubbling through portals.
|
|
49
|
-
if (!e.currentTarget
|
|
49
|
+
if (!(0, $3b9Q0$nodeContains)(e.currentTarget, (0, $3b9Q0$getEventTarget)(e))) return;
|
|
50
50
|
// Double check that document.activeElement actually matches e.target in case a previously chained
|
|
51
51
|
// focus handler already moved focus somewhere else.
|
|
52
|
-
|
|
52
|
+
let eventTarget = (0, $3b9Q0$getEventTarget)(e);
|
|
53
|
+
const ownerDocument = (0, $3b9Q0$getOwnerDocument)(eventTarget);
|
|
53
54
|
const activeElement = (0, $3b9Q0$getActiveElement)(ownerDocument);
|
|
54
|
-
if (!state.current.isFocusWithin && activeElement ===
|
|
55
|
+
if (!state.current.isFocusWithin && activeElement === eventTarget) {
|
|
55
56
|
if (onFocusWithin) onFocusWithin(e);
|
|
56
57
|
if (onFocusWithinChange) onFocusWithinChange(true);
|
|
57
58
|
state.current.isFocusWithin = true;
|
|
@@ -61,9 +62,10 @@ function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
|
|
|
61
62
|
// can manually fire onBlur.
|
|
62
63
|
let currentTarget = e.currentTarget;
|
|
63
64
|
addGlobalListener(ownerDocument, 'focus', (e)=>{
|
|
64
|
-
|
|
65
|
+
let eventTarget = (0, $3b9Q0$getEventTarget)(e);
|
|
66
|
+
if (state.current.isFocusWithin && !(0, $3b9Q0$nodeContains)(currentTarget, eventTarget)) {
|
|
65
67
|
let nativeEvent = new ownerDocument.defaultView.FocusEvent('blur', {
|
|
66
|
-
relatedTarget:
|
|
68
|
+
relatedTarget: eventTarget
|
|
67
69
|
});
|
|
68
70
|
(0, $8a9cb279dc87e130$export$c2b7abe5d61ec696)(nativeEvent, currentTarget);
|
|
69
71
|
let event = (0, $8a9cb279dc87e130$export$525bc4921d56d4a)(nativeEvent);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {createSyntheticEvent as $8a9cb279dc87e130$export$525bc4921d56d4a, setEventTarget as $8a9cb279dc87e130$export$c2b7abe5d61ec696, useSyntheticBlurEvent as $8a9cb279dc87e130$export$715c682d09d639cc} from "./utils.module.js";
|
|
2
2
|
import {useRef as $3b9Q0$useRef, useCallback as $3b9Q0$useCallback} from "react";
|
|
3
|
-
import {useGlobalListeners as $3b9Q0$useGlobalListeners,
|
|
3
|
+
import {useGlobalListeners as $3b9Q0$useGlobalListeners, nodeContains as $3b9Q0$nodeContains, getEventTarget as $3b9Q0$getEventTarget, getOwnerDocument as $3b9Q0$getOwnerDocument, getActiveElement as $3b9Q0$getActiveElement} from "@react-aria/utils";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
6
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -27,11 +27,11 @@ function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
|
|
|
27
27
|
let { addGlobalListener: addGlobalListener, removeAllGlobalListeners: removeAllGlobalListeners } = (0, $3b9Q0$useGlobalListeners)();
|
|
28
28
|
let onBlur = (0, $3b9Q0$useCallback)((e)=>{
|
|
29
29
|
// Ignore events bubbling through portals.
|
|
30
|
-
if (!e.currentTarget
|
|
30
|
+
if (!(0, $3b9Q0$nodeContains)(e.currentTarget, (0, $3b9Q0$getEventTarget)(e))) return;
|
|
31
31
|
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
32
32
|
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
33
33
|
// include the relatedTarget (where focus is moving).
|
|
34
|
-
if (state.current.isFocusWithin && !e.currentTarget
|
|
34
|
+
if (state.current.isFocusWithin && !(0, $3b9Q0$nodeContains)(e.currentTarget, e.relatedTarget)) {
|
|
35
35
|
state.current.isFocusWithin = false;
|
|
36
36
|
removeAllGlobalListeners();
|
|
37
37
|
if (onBlurWithin) onBlurWithin(e);
|
|
@@ -46,12 +46,13 @@ function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
|
|
|
46
46
|
let onSyntheticFocus = (0, $8a9cb279dc87e130$export$715c682d09d639cc)(onBlur);
|
|
47
47
|
let onFocus = (0, $3b9Q0$useCallback)((e)=>{
|
|
48
48
|
// Ignore events bubbling through portals.
|
|
49
|
-
if (!e.currentTarget
|
|
49
|
+
if (!(0, $3b9Q0$nodeContains)(e.currentTarget, (0, $3b9Q0$getEventTarget)(e))) return;
|
|
50
50
|
// Double check that document.activeElement actually matches e.target in case a previously chained
|
|
51
51
|
// focus handler already moved focus somewhere else.
|
|
52
|
-
|
|
52
|
+
let eventTarget = (0, $3b9Q0$getEventTarget)(e);
|
|
53
|
+
const ownerDocument = (0, $3b9Q0$getOwnerDocument)(eventTarget);
|
|
53
54
|
const activeElement = (0, $3b9Q0$getActiveElement)(ownerDocument);
|
|
54
|
-
if (!state.current.isFocusWithin && activeElement ===
|
|
55
|
+
if (!state.current.isFocusWithin && activeElement === eventTarget) {
|
|
55
56
|
if (onFocusWithin) onFocusWithin(e);
|
|
56
57
|
if (onFocusWithinChange) onFocusWithinChange(true);
|
|
57
58
|
state.current.isFocusWithin = true;
|
|
@@ -61,9 +62,10 @@ function $9ab94262bd0047c7$export$420e68273165f4ec(props) {
|
|
|
61
62
|
// can manually fire onBlur.
|
|
62
63
|
let currentTarget = e.currentTarget;
|
|
63
64
|
addGlobalListener(ownerDocument, 'focus', (e)=>{
|
|
64
|
-
|
|
65
|
+
let eventTarget = (0, $3b9Q0$getEventTarget)(e);
|
|
66
|
+
if (state.current.isFocusWithin && !(0, $3b9Q0$nodeContains)(currentTarget, eventTarget)) {
|
|
65
67
|
let nativeEvent = new ownerDocument.defaultView.FocusEvent('blur', {
|
|
66
|
-
relatedTarget:
|
|
68
|
+
relatedTarget: eventTarget
|
|
67
69
|
});
|
|
68
70
|
(0, $8a9cb279dc87e130$export$c2b7abe5d61ec696)(nativeEvent, currentTarget);
|
|
69
71
|
let event = (0, $8a9cb279dc87e130$export$525bc4921d56d4a)(nativeEvent);
|