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AI,YACF,gBAAgB;YAAC,eAAe,MAAM,MAAM;QAAA,GAAG,MAAM,WAAW;IAEpE,uDAAuD;IACvD,GAAG;QAAC;KAAW;IAEf,OAAO;oBACL;mBACA;IACF;AACF","sources":["packages/@react-aria/interactions/src/useHover.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 {DOMAttributes, HoverEvents} from '@react-types/shared';\nimport {useEffect, useMemo, useRef, useState} from 'react';\n\nexport interface HoverProps extends HoverEvents {\n /** Whether the hover events should be disabled. */\n isDisabled?: boolean\n}\n\nexport interface HoverResult {\n /** Props to spread on the target element. */\n hoverProps: DOMAttributes,\n isHovered: boolean\n}\n\n// iOS fires onPointerEnter twice: once with pointerType=\"touch\" and again with pointerType=\"mouse\".\n// We want to ignore these emulated events so they do not trigger hover behavior.\n// See https://bugs.webkit.org/show_bug.cgi?id=214609.\nlet globalIgnoreEmulatedMouseEvents = false;\nlet hoverCount = 0;\n\nfunction setGlobalIgnoreEmulatedMouseEvents() {\n globalIgnoreEmulatedMouseEvents = true;\n\n // Clear globalIgnoreEmulatedMouseEvents after a short timeout. iOS fires onPointerEnter\n // with pointerType=\"mouse\" immediately after onPointerUp and before onFocus. On other\n // devices that don't have this quirk, we don't want to ignore a mouse hover sometime in\n // the distant future because a user previously touched the element.\n setTimeout(() => {\n globalIgnoreEmulatedMouseEvents = false;\n }, 50);\n}\n\nfunction handleGlobalPointerEvent(e) {\n if (e.pointerType === 'touch') {\n setGlobalIgnoreEmulatedMouseEvents();\n }\n}\n\nfunction setupGlobalTouchEvents() {\n if (typeof document === 'undefined') {\n return;\n }\n\n if (typeof PointerEvent !== 'undefined') {\n document.addEventListener('pointerup', handleGlobalPointerEvent);\n } else {\n document.addEventListener('touchend', setGlobalIgnoreEmulatedMouseEvents);\n }\n\n hoverCount++;\n return () => {\n hoverCount--;\n if (hoverCount > 0) {\n return;\n }\n\n if (typeof PointerEvent !== 'undefined') {\n document.removeEventListener('pointerup', handleGlobalPointerEvent);\n } else {\n document.removeEventListener('touchend', setGlobalIgnoreEmulatedMouseEvents);\n }\n };\n}\n\n/**\n * Handles pointer hover interactions for an element. Normalizes behavior\n * across browsers and platforms, and ignores emulated mouse events on touch devices.\n */\nexport function useHover(props: HoverProps): HoverResult {\n let {\n onHoverStart,\n onHoverChange,\n onHoverEnd,\n isDisabled\n } = props;\n\n let [isHovered, setHovered] = useState(false);\n let state = useRef({\n isHovered: false,\n ignoreEmulatedMouseEvents: false,\n pointerType: '',\n target: null\n }).current;\n\n useEffect(setupGlobalTouchEvents, []);\n\n let {hoverProps, triggerHoverEnd} = useMemo(() => {\n let triggerHoverStart = (event, pointerType) => {\n state.pointerType = pointerType;\n if (isDisabled || pointerType === 'touch' || state.isHovered || !event.currentTarget.contains(event.target)) {\n return;\n }\n\n state.isHovered = true;\n let target = event.currentTarget;\n state.target = target;\n\n if (onHoverStart) {\n onHoverStart({\n type: 'hoverstart',\n target,\n pointerType\n });\n }\n\n if (onHoverChange) {\n onHoverChange(true);\n }\n\n setHovered(true);\n };\n\n let triggerHoverEnd = (event, pointerType) => {\n state.pointerType = '';\n state.target = null;\n\n if (pointerType === 'touch' || !state.isHovered) {\n return;\n }\n\n state.isHovered = false;\n let target = event.currentTarget;\n if (onHoverEnd) {\n onHoverEnd({\n type: 'hoverend',\n target,\n pointerType\n });\n }\n\n if (onHoverChange) {\n onHoverChange(false);\n }\n\n setHovered(false);\n };\n\n let hoverProps: DOMAttributes = {};\n\n if (typeof PointerEvent !== 'undefined') {\n hoverProps.onPointerEnter = (e) => {\n if (globalIgnoreEmulatedMouseEvents && e.pointerType === 'mouse') {\n return;\n }\n\n triggerHoverStart(e, e.pointerType);\n };\n\n hoverProps.onPointerLeave = (e) => {\n if (!isDisabled && e.currentTarget.contains(e.target as Element)) {\n triggerHoverEnd(e, e.pointerType);\n }\n };\n } else {\n hoverProps.onTouchStart = () => {\n state.ignoreEmulatedMouseEvents = true;\n };\n\n hoverProps.onMouseEnter = (e) => {\n if (!state.ignoreEmulatedMouseEvents && !globalIgnoreEmulatedMouseEvents) {\n triggerHoverStart(e, 'mouse');\n }\n\n state.ignoreEmulatedMouseEvents = false;\n };\n\n hoverProps.onMouseLeave = (e) => {\n if (!isDisabled && e.currentTarget.contains(e.target as Element)) {\n triggerHoverEnd(e, 'mouse');\n }\n };\n }\n return {hoverProps, triggerHoverEnd};\n }, [onHoverStart, onHoverChange, onHoverEnd, isDisabled, state]);\n\n useEffect(() => {\n // Call the triggerHoverEnd as soon as isDisabled changes to true\n // Safe to call triggerHoverEnd, it will early return if we aren't currently hovering\n if (isDisabled) {\n triggerHoverEnd({currentTarget: state.target}, state.pointerType);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isDisabled]);\n\n return {\n hoverProps,\n isHovered\n };\n}\n"],"names":[],"version":3,"file":"useHover.module.js.map"} |