-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: 为弹窗组件添加动画可见性控制 #7045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: 为弹窗组件添加动画可见性控制 #7045
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import { renderHook, act } from '@testing-library/react' | ||
| import { useSpringVisibility } from '../use-spring-visibility' | ||
|
|
||
| describe('useSpringVisibility', () => { | ||
| const mockSetActive = jest.fn() | ||
| const mockAfterClose = jest.fn() | ||
| const mockUnmountedRef = { current: false } | ||
| const originalVisibilityState = Object.getOwnPropertyDescriptor( | ||
| document, | ||
| 'visibilityState' | ||
| ) | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockUnmountedRef.current = false | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| if (originalVisibilityState) { | ||
| Object.defineProperty( | ||
| document, | ||
| 'visibilityState', | ||
| originalVisibilityState | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| it('should call setActive(false) and afterClose when page becomes visible after close while hidden', () => { | ||
| const { result } = renderHook(() => | ||
| useSpringVisibility({ | ||
| visible: false, | ||
| active: true, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }) | ||
| ) | ||
|
|
||
| // Simulate page becoming visible (visibilitychange event) | ||
| Object.defineProperty(document, 'visibilityState', { | ||
| value: 'visible', | ||
| writable: true, | ||
| }) | ||
| act(() => { | ||
| document.dispatchEvent(new Event('visibilitychange')) | ||
| }) | ||
|
|
||
| expect(mockSetActive).toHaveBeenCalledWith(false) | ||
| expect(mockAfterClose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('should not call afterClose when page is already visible and active matches visible', () => { | ||
| renderHook(() => | ||
| useSpringVisibility({ | ||
| visible: false, | ||
| active: false, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }) | ||
| ) | ||
|
|
||
| Object.defineProperty(document, 'visibilityState', { | ||
| value: 'visible', | ||
| writable: true, | ||
| }) | ||
| act(() => { | ||
| document.dispatchEvent(new Event('visibilitychange')) | ||
| }) | ||
|
|
||
| expect(mockSetActive).not.toHaveBeenCalled() | ||
| expect(mockAfterClose).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should not call afterClose when component is unmounted', () => { | ||
| mockUnmountedRef.current = true | ||
|
|
||
| renderHook(() => | ||
| useSpringVisibility({ | ||
| visible: false, | ||
| active: true, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }) | ||
| ) | ||
|
|
||
| Object.defineProperty(document, 'visibilityState', { | ||
| value: 'visible', | ||
| writable: true, | ||
| }) | ||
| act(() => { | ||
| document.dispatchEvent(new Event('visibilitychange')) | ||
| }) | ||
|
|
||
| expect(mockSetActive).not.toHaveBeenCalled() | ||
| expect(mockAfterClose).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('shouldCallAfterClose should prevent double-calling afterClose', () => { | ||
| const { result } = renderHook(() => | ||
| useSpringVisibility({ | ||
| visible: false, | ||
| active: true, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }) | ||
| ) | ||
|
|
||
| // Simulate visibilitychange handler calling afterClose first | ||
| Object.defineProperty(document, 'visibilityState', { | ||
| value: 'visible', | ||
| writable: true, | ||
| }) | ||
| act(() => { | ||
| document.dispatchEvent(new Event('visibilitychange')) | ||
| }) | ||
|
|
||
| expect(mockAfterClose).toHaveBeenCalledTimes(1) | ||
|
|
||
| // Now onRest fires later - shouldCallAfterClose should return false | ||
| expect(result.current.shouldCallAfterClose()).toBe(false) | ||
| }) | ||
|
|
||
| it('shouldCallAfterClose should return true when afterClose has not been called', () => { | ||
| const { result } = renderHook(() => | ||
| useSpringVisibility({ | ||
| visible: false, | ||
| active: false, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }) | ||
| ) | ||
|
|
||
| // onRest fires normally (no visibilitychange handler intervention) | ||
| expect(result.current.shouldCallAfterClose()).toBe(true) | ||
| // Second call should return false | ||
| expect(result.current.shouldCallAfterClose()).toBe(false) | ||
| }) | ||
|
|
||
| it('should reset closedRef when visible becomes true', () => { | ||
| const { result, rerender } = renderHook( | ||
| ({ visible }: { visible: boolean }) => | ||
| useSpringVisibility({ | ||
| visible, | ||
| active: true, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }), | ||
| { initialProps: { visible: false } } | ||
| ) | ||
|
|
||
| // Simulate afterClose being called (via onRest or visibilitychange) | ||
| // shouldCallAfterClose returns true on first call, then false on subsequent calls | ||
| expect(result.current.shouldCallAfterClose()).toBe(true) | ||
| expect(result.current.shouldCallAfterClose()).toBe(false) | ||
|
|
||
| // Now visible becomes true (new show cycle) - closedRef should be reset | ||
| rerender({ visible: true }) | ||
|
|
||
| // shouldCallAfterClose should be reset and return true again | ||
| expect(result.current.shouldCallAfterClose()).toBe(true) | ||
| }) | ||
|
|
||
| it('should not trigger on visibilitychange when document is hidden', () => { | ||
| renderHook(() => | ||
| useSpringVisibility({ | ||
| visible: false, | ||
| active: true, | ||
| setActive: mockSetActive, | ||
| afterClose: mockAfterClose, | ||
| unmountedRef: mockUnmountedRef, | ||
| }) | ||
| ) | ||
|
|
||
| Object.defineProperty(document, 'visibilityState', { | ||
| value: 'hidden', | ||
| writable: true, | ||
| }) | ||
| act(() => { | ||
| document.dispatchEvent(new Event('visibilitychange')) | ||
| }) | ||
|
|
||
| expect(mockSetActive).not.toHaveBeenCalled() | ||
| expect(mockAfterClose).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||||||||||||||||
| import { useIsomorphicLayoutEffect } from 'ahooks' | ||||||||||||||||||||||
| import type { RefObject } from 'react' | ||||||||||||||||||||||
| import { useEffect, useRef } from 'react' | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export function useSpringVisibility({ | ||||||||||||||||||||||
| visible, | ||||||||||||||||||||||
| active, | ||||||||||||||||||||||
| setActive, | ||||||||||||||||||||||
| afterClose, | ||||||||||||||||||||||
| unmountedRef, | ||||||||||||||||||||||
| }: { | ||||||||||||||||||||||
| visible: boolean | ||||||||||||||||||||||
| active: boolean | ||||||||||||||||||||||
| setActive: (value: boolean) => void | ||||||||||||||||||||||
| afterClose?: () => void | ||||||||||||||||||||||
| unmountedRef: RefObject<boolean> | ||||||||||||||||||||||
| }) { | ||||||||||||||||||||||
| const closedRef = useRef(false) | ||||||||||||||||||||||
| const afterCloseRef = useRef(afterClose) | ||||||||||||||||||||||
| afterCloseRef.current = afterClose | ||||||||||||||||||||||
| const activeRef = useRef(active) | ||||||||||||||||||||||
| activeRef.current = active | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Reset closedRef when a new show cycle begins | ||||||||||||||||||||||
| useIsomorphicLayoutEffect(() => { | ||||||||||||||||||||||
| if (visible) { | ||||||||||||||||||||||
| closedRef.current = false | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }, [visible]) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||
| const handler = () => { | ||||||||||||||||||||||
| if (document.visibilityState !== 'visible') return | ||||||||||||||||||||||
| if (unmountedRef.current) return | ||||||||||||||||||||||
| if (!visible && activeRef.current && !closedRef.current) { | ||||||||||||||||||||||
| closedRef.current = true | ||||||||||||||||||||||
| setActive(false) | ||||||||||||||||||||||
| afterCloseRef.current?.() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| document.addEventListener('visibilitychange', handler) | ||||||||||||||||||||||
| return () => document.removeEventListener('visibilitychange', handler) | ||||||||||||||||||||||
| }, [visible, setActive, unmountedRef]) | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议使用 const visibleRef = useRef(visible)
visibleRef.current = visible
useEffect(() => {
const handler = () => {
if (document.visibilityState !== 'visible') return
if (unmountedRef.current) return
if (!visibleRef.current && activeRef.current && !closedRef.current) {
closedRef.current = true
setActive(false)
afterCloseRef.current?.()
}
}
document.addEventListener('visibilitychange', handler)
return () => document.removeEventListener('visibilitychange', handler)
}, [setActive, unmountedRef])
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function shouldCallAfterClose(): boolean { | ||||||||||||||||||||||
| if (closedRef.current) return false | ||||||||||||||||||||||
| closedRef.current = true | ||||||||||||||||||||||
| return true | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议使用
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return { shouldCallAfterClose } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议导入
useCallback以便对 Hook 返回的函数进行记忆化处理,保持引用稳定。