( ref: Solid.Accessor<T | null>, callback: (entry?: IntersectionObserverEntry) => void, disabled: Solid.Accessor<boolean>, )
| 24 | * ``` |
| 25 | */ |
| 26 | export function useIntersectionObserver<T extends Element>( |
| 27 | ref: Solid.Accessor<T | null>, |
| 28 | callback: (entry?: IntersectionObserverEntry) => void, |
| 29 | disabled: Solid.Accessor<boolean>, |
| 30 | ): Solid.Accessor<IntersectionObserver | null> { |
| 31 | const isIntersectionObserverAvailable = |
| 32 | typeof IntersectionObserver === 'function' |
| 33 | let observerRef: IntersectionObserver | null = null |
| 34 | |
| 35 | Solid.createEffect(() => { |
| 36 | const r = ref() |
| 37 | if (disabled() || !r || !isIntersectionObserverAvailable) { |
| 38 | Solid.onCleanup(() => callback()) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | observerRef = new IntersectionObserver( |
| 43 | (entries) => { |
| 44 | callback(entries.pop()) |
| 45 | }, |
| 46 | { rootMargin: '100px' }, |
| 47 | ) |
| 48 | |
| 49 | observerRef.observe(r) |
| 50 | |
| 51 | Solid.onCleanup(() => { |
| 52 | observerRef?.disconnect() |
| 53 | callback() |
| 54 | }) |
| 55 | }) |
| 56 | |
| 57 | return () => observerRef |
| 58 | } |
no test coverage detected