UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

49 lines (48 loc) 1.39 kB
import * as Solid from "solid-js"; //#region src/utils.ts /** * React hook to wrap `IntersectionObserver`. * * This hook will create an `IntersectionObserver` and observe the ref passed to it. * * When the intersection changes, the callback will be called with the `IntersectionObserverEntry`. * * @param ref - The ref to observe * @param callback - The callback to call when the intersection changes * @param disabled - Whether observation is disabled * @returns The IntersectionObserver instance * @example * ```tsx * const MyComponent = () => { * const ref = React.useRef<HTMLDivElement>(null) * useIntersectionObserver( * ref, * (entry) => { doSomething(entry) }, * false * ) * return <div ref={ref} /> * ``` */ function useIntersectionObserver(ref, callback, disabled) { const isIntersectionObserverAvailable = typeof IntersectionObserver === "function"; let observerRef = null; Solid.createEffect(() => { const r = ref(); if (disabled() || !r || !isIntersectionObserverAvailable) { Solid.onCleanup(() => callback()); return; } observerRef = new IntersectionObserver((entries) => { callback(entries.pop()); }, { rootMargin: "100px" }); observerRef.observe(r); Solid.onCleanup(() => { observerRef?.disconnect(); callback(); }); }); return () => observerRef; } //#endregion export { useIntersectionObserver }; //# sourceMappingURL=utils.js.map