-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseHover.ts
More file actions
187 lines (154 loc) · 5.17 KB
/
Copy pathuseHover.ts
File metadata and controls
187 lines (154 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
// Portions of the code in this file are based on code from react.
// Original licensing for the following can be found in the
// NOTICE file in the root directory of this source tree.
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
import {HoverEvents} from '@react-types/shared';
import {HTMLAttributes, useEffect, useMemo, useRef, useState} from 'react';
export interface HoverProps extends HoverEvents {
/** Whether the hover events should be disabled. */
isDisabled?: boolean
}
interface HoverResult {
/** Props to spread on the target element. */
hoverProps: HTMLAttributes<HTMLElement>,
isHovered: boolean
}
// iOS fires onPointerEnter twice: once with pointerType="touch" and again with pointerType="mouse".
// We want to ignore these emulated events so they do not trigger hover behavior.
// See https://bugs.webkit.org/show_bug.cgi?id=214609.
let globalIgnoreEmulatedMouseEvents = false;
let hoverCount = 0;
function setGlobalIgnoreEmulatedMouseEvents() {
globalIgnoreEmulatedMouseEvents = true;
// Clear globalIgnoreEmulatedMouseEvents after a short timeout. iOS fires onPointerEnter
// with pointerType="mouse" immediately after onPointerUp and before onFocus. On other
// devices that don't have this quirk, we don't want to ignore a mouse hover sometime in
// the distant future because a user previously touched the element.
setTimeout(() => {
globalIgnoreEmulatedMouseEvents = false;
}, 50);
}
function handleGlobalPointerEvent(e) {
if (e.pointerType === 'touch') {
setGlobalIgnoreEmulatedMouseEvents();
}
}
function setupGlobalTouchEvents() {
if (typeof document === 'undefined') {
return;
}
if (typeof PointerEvent !== 'undefined') {
document.addEventListener('pointerup', handleGlobalPointerEvent);
} else {
document.addEventListener('touchend', setGlobalIgnoreEmulatedMouseEvents);
}
hoverCount++;
return () => {
hoverCount--;
if (hoverCount > 0) {
return;
}
if (typeof PointerEvent !== 'undefined') {
document.removeEventListener('pointerup', handleGlobalPointerEvent);
} else {
document.removeEventListener('touchend', setGlobalIgnoreEmulatedMouseEvents);
}
};
}
/**
* Handles pointer hover interactions for an element. Normalizes behavior
* across browsers and platforms, and ignores emulated mouse events on touch devices.
*/
export function useHover(props: HoverProps): HoverResult {
let {
onHoverStart,
onHoverChange,
onHoverEnd,
isDisabled
} = props;
let [isHovered, setHovered] = useState(false);
let state = useRef({
isHovered: false,
ignoreEmulatedMouseEvents: false
}).current;
useEffect(setupGlobalTouchEvents, []);
let hoverProps = useMemo(() => {
let triggerHoverStart = (event, pointerType) => {
if (isDisabled || pointerType === 'touch' || state.isHovered) {
return;
}
state.isHovered = true;
let target = event.target;
if (onHoverStart) {
onHoverStart({
type: 'hoverstart',
target,
pointerType
});
}
if (onHoverChange) {
onHoverChange(true);
}
setHovered(true);
};
let triggerHoverEnd = (event, pointerType) => {
if (pointerType === 'touch' || !state.isHovered) {
return;
}
state.isHovered = false;
let target = event.target;
if (onHoverEnd) {
onHoverEnd({
type: 'hoverend',
target,
pointerType
});
}
if (onHoverChange) {
onHoverChange(false);
}
setHovered(false);
};
let hoverProps: HTMLAttributes<HTMLElement> = {};
if (typeof PointerEvent !== 'undefined') {
hoverProps.onPointerEnter = (e) => {
if (globalIgnoreEmulatedMouseEvents && e.pointerType === 'mouse') {
return;
}
triggerHoverStart(e, e.pointerType);
};
hoverProps.onPointerLeave = (e) => {
triggerHoverEnd(e, e.pointerType);
};
} else {
hoverProps.onTouchStart = () => {
state.ignoreEmulatedMouseEvents = true;
};
hoverProps.onMouseEnter = (e) => {
if (!state.ignoreEmulatedMouseEvents && !globalIgnoreEmulatedMouseEvents) {
triggerHoverStart(e, 'mouse');
}
state.ignoreEmulatedMouseEvents = false;
};
hoverProps.onMouseLeave = (e) => {
triggerHoverEnd(e, 'mouse');
};
}
return hoverProps;
}, [onHoverStart, onHoverChange, onHoverEnd, isDisabled, state]);
return {
hoverProps,
isHovered
};
}