|
1 | 1 | import { useIsomorphicLayoutEffect } from 'ahooks' |
2 | 2 | import type { RefObject } from 'react' |
3 | | -import { useEffect, useRef } from 'react' |
| 3 | +import { useCallback, useEffect, useRef } from 'react' |
4 | 4 |
|
5 | 5 | export function useSpringVisibility({ |
6 | 6 | visible, |
@@ -28,25 +28,28 @@ export function useSpringVisibility({ |
28 | 28 | } |
29 | 29 | }, [visible]) |
30 | 30 |
|
| 31 | + const visibleRef = useRef(visible) |
| 32 | + visibleRef.current = visible |
| 33 | + |
31 | 34 | useEffect(() => { |
32 | 35 | const handler = () => { |
33 | 36 | if (document.visibilityState !== 'visible') return |
34 | 37 | if (unmountedRef.current) return |
35 | | - if (!visible && activeRef.current && !closedRef.current) { |
| 38 | + if (!visibleRef.current && activeRef.current && !closedRef.current) { |
36 | 39 | closedRef.current = true |
37 | 40 | setActive(false) |
38 | 41 | afterCloseRef.current?.() |
39 | 42 | } |
40 | 43 | } |
41 | 44 | document.addEventListener('visibilitychange', handler) |
42 | 45 | return () => document.removeEventListener('visibilitychange', handler) |
43 | | - }, [visible, setActive, unmountedRef]) |
| 46 | + }, [setActive, unmountedRef]) |
44 | 47 |
|
45 | | - function shouldCallAfterClose(): boolean { |
| 48 | + const shouldCallAfterClose = useCallback((): boolean => { |
46 | 49 | if (closedRef.current) return false |
47 | 50 | closedRef.current = true |
48 | 51 | return true |
49 | | - } |
| 52 | + }, []) |
50 | 53 |
|
51 | 54 | return { shouldCallAfterClose } |
52 | 55 | } |
0 commit comments