2021-04-23 10:59:11 +02:00
|
|
|
import { createRef, useLayoutEffect } from 'react';
|
2021-04-19 10:55:15 +02:00
|
|
|
|
|
|
|
type refElement = HTMLDivElement;
|
|
|
|
|
|
|
|
const useLoading = (loading: boolean) => {
|
|
|
|
const ref = createRef<refElement>();
|
2021-04-23 10:59:11 +02:00
|
|
|
useLayoutEffect(() => {
|
2021-04-19 10:55:15 +02:00
|
|
|
if (ref.current) {
|
|
|
|
const elements = ref.current.querySelectorAll('[data-loading]');
|
|
|
|
|
|
|
|
elements.forEach(element => {
|
|
|
|
if (loading) {
|
|
|
|
element.classList.add('skeleton');
|
|
|
|
} else {
|
|
|
|
element.classList.remove('skeleton');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [loading, ref]);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useLoading;
|