From 01ca31b4e72c78f3b7e40bbd58161e47f966258b Mon Sep 17 00:00:00 2001 From: ryan-ziqi <877520264@qq.com> Date: Sat, 8 Mar 2025 23:43:51 +0800 Subject: [PATCH 1/3] fix: panel not follow when scroll is visible --- src/hooks/useTarget.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hooks/useTarget.ts b/src/hooks/useTarget.ts index 840cbf3..10d4c11 100644 --- a/src/hooks/useTarget.ts +++ b/src/hooks/useTarget.ts @@ -85,8 +85,12 @@ export default function useTarget( updatePos(); // update when window resize window.addEventListener('resize', updatePos); + // update when `document.body.style.overflow` is set to visible + // by default, it will be set to hidden + window.addEventListener('scroll', updatePos); return () => { window.removeEventListener('resize', updatePos); + window.removeEventListener('scroll', updatePos); }; }, [targetElement, open, updatePos]); From 19cf2fc7a4d43851d71a6e649f873f8ebd50caf4 Mon Sep 17 00:00:00 2001 From: yellworyan <877520264@qq.com> Date: Tue, 11 Mar 2025 21:33:52 +0800 Subject: [PATCH 2/3] test: add scroll test case --- tests/index.test.tsx | 66 +++++++++++++++++++++++++++++++++++++++++++- tests/utils.ts | 8 +++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 0dffea0..6f27163 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -7,7 +7,7 @@ import type { TourProps } from '../src/index'; import Tour from '../src/index'; import { getPlacements, placements } from '../src/placements'; import { getPlacement } from '../src/util'; -import { resizeWindow } from './utils'; +import { resizeWindow, scrollWindow } from './utils'; const mockBtnRect = ( rect: { @@ -1225,4 +1225,68 @@ describe('Tour', () => { // Check container has children expect(children).toBeTruthy(); }); + + it('should update position when window scroll', async () => { + mockBtnRect({ + x: 80, + y: 333, + width: 230, + height: 180, + }); + const Demo = () => { + const btnRef = useRef(null); + + return ( +
+ + btnRef.current, + }, + ]} + /> +
+ ); + }; + const { baseElement, unmount } = render(); + expect( + baseElement.querySelector('.rc-tour-target-placeholder'), + ).toHaveStyle('left: 74px; top: 327px; width: 242px; height: 192px;'); + fireEvent.click(baseElement.querySelector('.btn2')); + await act(() => { + jest.runAllTimers(); + }); + expect( + baseElement.querySelector('.rc-tour-target-placeholder'), + ).toHaveStyle('left: 74px; top: 794px; width: 242px; height: 192px;'); + + expect(baseElement).toMatchSnapshot(); + + unmount(); + mockBtnRect({ + x: 0, + y: 0, + width: 0, + height: 0, + }); + }); }); diff --git a/tests/utils.ts b/tests/utils.ts index 523b7e9..fb3bc6b 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -4,4 +4,10 @@ export const resizeWindow = (x: number, y: number) => { // @ts-ignore window.innerHeight = y; window.dispatchEvent(new Event('resize')); -}; \ No newline at end of file +}; + + +export const scrollWindow = (x: number, y: number) => { + window.scrollTo(x, y); + window.dispatchEvent(new Event('scroll')) +} \ No newline at end of file From 7e99bd2e28b24a806aa730fbd05decbee38b7b83 Mon Sep 17 00:00:00 2001 From: yellworyan <877520264@qq.com> Date: Tue, 11 Mar 2025 21:34:03 +0800 Subject: [PATCH 3/3] chore: update snapshot --- tests/__snapshots__/index.test.tsx.snap | 155 ++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/tests/__snapshots__/index.test.tsx.snap b/tests/__snapshots__/index.test.tsx.snap index 281c083..f4c6b02 100644 --- a/tests/__snapshots__/index.test.tsx.snap +++ b/tests/__snapshots__/index.test.tsx.snap @@ -1280,6 +1280,161 @@ exports[`Tour should update position when window resize 1`] = ` `; +exports[`Tour should update position when window scroll 1`] = ` + +
+
+ +
+
+
+
+ + + + + + + + + + + + + +
+
+
+
+
+
+
+
+
+
+ +
+
+ 创建 +
+
+
+ 创建一条数据 +
+ +
+
+
+ +`; + exports[`Tour showArrow should show tooltip arrow default 1`] = `