From 1521c310a877a27c5ccbeb9ff66cd4092ca67033 Mon Sep 17 00:00:00 2001 From: Bill Parrott Date: Fri, 16 Aug 2024 09:41:35 -0500 Subject: [PATCH 1/2] [@mantine/hooks] use-idle: Start timer immediately instead of waiting for an event (#6682) --- .../@mantine/hooks/src/use-idle/use-idle.test.tsx | 14 ++++++++++++++ packages/@mantine/hooks/src/use-idle/use-idle.ts | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx b/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx index d4e7f693132..9d0dd6b7ac8 100644 --- a/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx +++ b/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx @@ -11,6 +11,20 @@ describe('@mantine/hooks/use-idle', () => { expect(hook.result.current).toBe(true); }); + it('Starts the timer immediately instead of waiting for the first event to happen', () => { + const spy = jest.spyOn(window, 'setTimeout'); + expect(spy).not.toHaveBeenCalled(); + + const hook = renderHook(() => useIdle(1000, { initialState: false, events: ['click', 'keypress'] })); + + expect(hook.result.current).toBe(false); + expect(spy).toHaveBeenCalledTimes(1); + setTimeout(() => { + expect(hook.result.current).toBe(true); + expect(spy).toHaveBeenCalledTimes(2); + }, 1001); + }); + it('Returns correct value on firing keypress event', () => { const hook = renderHook(() => useIdle(1000)); diff --git a/packages/@mantine/hooks/src/use-idle/use-idle.ts b/packages/@mantine/hooks/src/use-idle/use-idle.ts index 473f2c0ab6c..eeaa1d022ac 100644 --- a/packages/@mantine/hooks/src/use-idle/use-idle.ts +++ b/packages/@mantine/hooks/src/use-idle/use-idle.ts @@ -35,6 +35,11 @@ export function useIdle( events.forEach((event) => document.addEventListener(event, handleEvents)); + // Start the timer immediately instead of waiting for the first event to happen + timer.current = window.setTimeout(() => { + setIdle(true); + }, timeout); + return () => { events.forEach((event) => document.removeEventListener(event, handleEvents)); }; From 03831118dc517d15bf8c6f66710b7c649a30ea43 Mon Sep 17 00:00:00 2001 From: Bill Parrott Date: Mon, 26 Aug 2024 09:50:07 -0500 Subject: [PATCH 2/2] [@mantine/hooks] use-idle: Fix test formatting (#6682) --- packages/@mantine/hooks/src/use-idle/use-idle.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx b/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx index 9d0dd6b7ac8..ddef950d787 100644 --- a/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx +++ b/packages/@mantine/hooks/src/use-idle/use-idle.test.tsx @@ -15,7 +15,9 @@ describe('@mantine/hooks/use-idle', () => { const spy = jest.spyOn(window, 'setTimeout'); expect(spy).not.toHaveBeenCalled(); - const hook = renderHook(() => useIdle(1000, { initialState: false, events: ['click', 'keypress'] })); + const hook = renderHook(() => + useIdle(1000, { initialState: false, events: ['click', 'keypress'] }) + ); expect(hook.result.current).toBe(false); expect(spy).toHaveBeenCalledTimes(1);