-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Gallery): timeout inactive tab issue (#5954)
- Loading branch information
1 parent
6e6c438
commit 23168d6
Showing
2 changed files
with
41 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as React from 'react'; | ||
import { useTimeout } from '../../hooks/useTimeout'; | ||
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(); | ||
|
||
React.useEffect( | ||
() => (timeout ? setAutoPlay() : clearAutoPlay()), | ||
[timeout, slideIndex, clearAutoPlay, setAutoPlay], | ||
); | ||
|
||
// Отключаем прокрутку слайдов при неактивной вкладке | ||
React.useEffect( | ||
function preventSlideChange() { | ||
if (!document || !timeout) { | ||
return; | ||
} | ||
|
||
const changeAutoPlay = () => { | ||
if (document.visibilityState === 'visible') { | ||
clearAutoPlay(); | ||
setAutoPlay(); | ||
} | ||
if (document.visibilityState === 'hidden') { | ||
clearAutoPlay(); | ||
} | ||
}; | ||
|
||
document.addEventListener('visibilitychange', changeAutoPlay); | ||
|
||
return () => { | ||
document.removeEventListener('visibilitychange', changeAutoPlay); | ||
}; | ||
}, | ||
[document, timeout, clearAutoPlay, setAutoPlay], | ||
); | ||
} |