Files
suwayomi-material-you-webui/src/base/hooks/useIntersectionObserver.tsx

53 lines
1.6 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2026-03-11 00:11:29 +01:00
import type { RefObject } from 'react';
import { useLayoutEffect, useState } from 'react';
export const useIntersectionObserver = (
2025-04-18 20:42:41 +02:00
ref: RefObject<HTMLElement | null> | HTMLElement | undefined | null,
callback: IntersectionObserverCallback,
{
ignoreInitialObserve = false,
root,
rootMargin,
threshold,
}: IntersectionObserverInit & { ignoreInitialObserve?: boolean } = {},
): (() => void) => {
const [disconnect, setDisconnect] = useState<() => void>(() => {});
useLayoutEffect(() => {
const element = ref instanceof HTMLElement ? ref : ref?.current;
if (!element) {
return () => {};
}
// gets immediately observed once on initial render
let isInitialObserve = true;
const intersectionObserver = new IntersectionObserver(
(...args) => {
if (ignoreInitialObserve && isInitialObserve) {
isInitialObserve = false;
return;
}
callback(...args);
},
{ root, rootMargin, threshold },
);
intersectionObserver.observe(element);
setDisconnect(() => () => intersectionObserver.disconnect());
return () => intersectionObserver.disconnect();
}, [ref, callback, ignoreInitialObserve, root, rootMargin, threshold]);
return disconnect;
};