From 4a087462f33d0648aaa8c8269546849810e29df8 Mon Sep 17 00:00:00 2001 From: Victoria Zhizhonkova Date: Wed, 14 Feb 2024 13:33:19 +0700 Subject: [PATCH] fix(Gallery): ignore false slideIndex change --- packages/vkui/src/components/Gallery/hooks.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/vkui/src/components/Gallery/hooks.ts b/packages/vkui/src/components/Gallery/hooks.ts index 211b4b4c7b3..4c9dc3fc122 100644 --- a/packages/vkui/src/components/Gallery/hooks.ts +++ b/packages/vkui/src/components/Gallery/hooks.ts @@ -5,9 +5,10 @@ import { useDOM } from '../../lib/dom'; export function useAutoPlay(timeout: number, slideIndex: number, callbackFn: VoidFunction) { const { clear: clearAutoPlay, set: setAutoPlay } = useTimeout(callbackFn, timeout); const { document } = useDOM(); + const idle = React.useRef(false); React.useEffect( - () => (timeout ? setAutoPlay() : clearAutoPlay()), + () => (timeout && !idle.current ? setAutoPlay() : clearAutoPlay()), [timeout, slideIndex, clearAutoPlay, setAutoPlay], ); @@ -19,13 +20,13 @@ export function useAutoPlay(timeout: number, slideIndex: number, callbackFn: Voi } const changeAutoPlay = () => { - if (document.visibilityState === 'visible') { + if (document.hidden) { + idle.current = true; clearAutoPlay(); + } else { + idle.current = false; setAutoPlay(); } - if (document.visibilityState === 'hidden') { - clearAutoPlay(); - } }; document.addEventListener('visibilitychange', changeAutoPlay);