9
9
useState ,
10
10
} from "react" ;
11
11
12
- import { useIsUserInteractionMode } from "../mode" ;
12
+ import { useUserInteractionMode } from "../mode" ;
13
13
import { useOnUnmount } from "../useOnUnmount" ;
14
14
import { DEFAULT_HOVER_MODE_STICKY_EXIT_TIME } from "./constants" ;
15
15
import { HoverModeActions , useHoverModeContext } from "./useHoverModeContext" ;
@@ -226,11 +226,12 @@ export function useHoverMode<E extends HTMLElement>({
226
226
defaultVisible = false ,
227
227
exitVisibilityDelay = sticky ? DEFAULT_HOVER_MODE_STICKY_EXIT_TIME : 0 ,
228
228
} : HoverModeOptions < E > = { } ) : HoverModeReturnValue < E > {
229
- const isTouch = useIsUserInteractionMode ( "touch" ) ;
229
+ const mode = useUserInteractionMode ( ) ;
230
+ const isTouch = mode === "touch" ;
230
231
const [ visible , setVisible ] = useState ( defaultVisible ) ;
231
232
const [ stuck , setStuck ] = useState ( false ) ;
232
- const enterTimeoutRef = useRef < number > ( ) ;
233
- const exitTimeoutRef = useRef < number > ( ) ;
233
+ const timeoutRef = useRef < number > ( ) ;
234
+ const skipReset = useRef ( defaultVisible ) ;
234
235
const {
235
236
visibleInTime,
236
237
enableHoverMode,
@@ -246,15 +247,31 @@ export function useHoverMode<E extends HTMLElement>({
246
247
} , [ visible , sticky ] ) ;
247
248
248
249
useOnUnmount ( ( ) => {
249
- window . clearTimeout ( enterTimeoutRef . current ) ;
250
- window . clearTimeout ( exitTimeoutRef . current ) ;
250
+ window . clearTimeout ( timeoutRef . current ) ;
251
251
} ) ;
252
252
253
253
useEffect ( ( ) => {
254
- if ( isTouch || disabled ) {
254
+ if ( disabled ) {
255
+ return ;
256
+ }
257
+
258
+ const reset = ( ) : void => {
259
+ setVisible ( false ) ;
255
260
disableHoverMode ( ) ;
261
+ window . clearTimeout ( timeoutRef . current ) ;
262
+ } ;
263
+
264
+ // this is just used so the `defaultOption` can be used
265
+ if ( ! skipReset . current ) {
266
+ reset ( ) ;
256
267
}
257
- } , [ disableHoverMode , isTouch , disabled ] ) ;
268
+ skipReset . current = false ;
269
+
270
+ window . addEventListener ( "mousedown" , reset ) ;
271
+ return ( ) => {
272
+ window . removeEventListener ( "mousedown" , reset ) ;
273
+ } ;
274
+ } , [ disableHoverMode , mode , disabled ] ) ;
258
275
259
276
const onMouseEnter = useCallback (
260
277
( event : MouseEvent < E > ) => {
@@ -263,15 +280,14 @@ export function useHoverMode<E extends HTMLElement>({
263
280
return ;
264
281
}
265
282
266
- window . clearTimeout ( enterTimeoutRef . current ) ;
267
- window . clearTimeout ( exitTimeoutRef . current ) ;
283
+ window . clearTimeout ( timeoutRef . current ) ;
268
284
if ( visibleInTime === 0 ) {
269
285
enableHoverMode ( ) ;
270
286
setVisible ( true ) ;
271
287
return ;
272
288
}
273
289
274
- enterTimeoutRef . current = window . setTimeout ( ( ) => {
290
+ timeoutRef . current = window . setTimeout ( ( ) => {
275
291
enableHoverMode ( ) ;
276
292
setVisible ( true ) ;
277
293
} , visibleInTime ) ;
@@ -287,14 +303,13 @@ export function useHoverMode<E extends HTMLElement>({
287
303
}
288
304
289
305
startDisableTimer ( ) ;
290
- window . clearTimeout ( enterTimeoutRef . current ) ;
291
- window . clearTimeout ( exitTimeoutRef . current ) ;
306
+ window . clearTimeout ( timeoutRef . current ) ;
292
307
if ( exitVisibilityDelay === 0 ) {
293
308
setVisible ( false ) ;
294
309
return ;
295
310
}
296
311
297
- exitTimeoutRef . current = window . setTimeout ( ( ) => {
312
+ timeoutRef . current = window . setTimeout ( ( ) => {
298
313
setVisible ( false ) ;
299
314
} , exitVisibilityDelay ) ;
300
315
} ,
@@ -316,8 +331,7 @@ export function useHoverMode<E extends HTMLElement>({
316
331
}
317
332
318
333
startDisableTimer ( ) ;
319
- window . clearTimeout ( enterTimeoutRef . current ) ;
320
- window . clearTimeout ( exitTimeoutRef . current ) ;
334
+ window . clearTimeout ( timeoutRef . current ) ;
321
335
} ,
322
336
[ disabled , propOnClick , startDisableTimer ]
323
337
) ;
0 commit comments