2021-04-23 10:59:11 +02:00
|
|
|
import { createRef, useLayoutEffect } from 'react';
|
2021-04-19 10:55:15 +02:00
|
|
|
|
|
|
|
type refElement = HTMLDivElement;
|
|
|
|
|
2023-02-07 08:57:28 +01:00
|
|
|
const useLoading = (loading: boolean, selector = '[data-loading=true]') => {
|
2021-04-19 10:55:15 +02:00
|
|
|
const ref = createRef<refElement>();
|
2021-04-23 10:59:11 +02:00
|
|
|
useLayoutEffect(() => {
|
2021-04-19 10:55:15 +02:00
|
|
|
if (ref.current) {
|
2023-02-07 08:57:28 +01:00
|
|
|
const elements = ref.current.querySelectorAll(selector);
|
2021-04-19 10:55:15 +02:00
|
|
|
|
2023-10-02 14:25:46 +02:00
|
|
|
elements.forEach((element) => {
|
2021-04-19 10:55:15 +02:00
|
|
|
if (loading) {
|
|
|
|
element.classList.add('skeleton');
|
|
|
|
} else {
|
2023-11-13 14:08:48 +01:00
|
|
|
setTimeout(() => element.classList.remove('skeleton'), 10);
|
2021-04-19 10:55:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-04-03 10:20:04 +02:00
|
|
|
}, [loading, selector, ref]);
|
2021-04-19 10:55:15 +02:00
|
|
|
|
|
|
|
return ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useLoading;
|