mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
(nextjs) added timeout for loader
This commit is contained in:
parent
41b26e7d69
commit
bcaa9fde39
2 changed files with 26 additions and 5 deletions
|
@ -2,13 +2,34 @@ import { useEffect, useState } from 'react';
|
|||
|
||||
import Router from 'next/router';
|
||||
|
||||
export const useSSRLoadingIndicator = () => {
|
||||
/** decides to show loader on SSR loading */
|
||||
export const useSSRLoadingIndicator = (delay = 0) => {
|
||||
const [shown, setShown] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
Router.events.on('routeChangeStart', () => setShown(true));
|
||||
Router.events.on('routeChangeComplete', () => setShown(false));
|
||||
Router.events.on('routeChangeError', () => setShown(false));
|
||||
let timeout;
|
||||
|
||||
const show = () => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
setShown(true);
|
||||
}, delay);
|
||||
};
|
||||
|
||||
const hide = () => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
setShown(false);
|
||||
};
|
||||
|
||||
Router.events.on('routeChangeStart', show);
|
||||
Router.events.on('routeChangeComplete', hide);
|
||||
Router.events.on('routeChangeError', hide);
|
||||
}, []);
|
||||
|
||||
return shown;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue