(
ref: Solid.Accessor<T | null>,
callback: (entry: IntersectionObserverEntry | undefined) => void,
intersectionObserverOptions: IntersectionObserverInit = {},
options: { disabled?: boolean } = {},
)
| 46 | * ``` |
| 47 | */ |
| 48 | export function useIntersectionObserver<T extends Element>( |
| 49 | ref: Solid.Accessor<T | null>, |
| 50 | callback: (entry: IntersectionObserverEntry | undefined) => void, |
| 51 | intersectionObserverOptions: IntersectionObserverInit = {}, |
| 52 | options: { disabled?: boolean } = {}, |
| 53 | ): Solid.Accessor<IntersectionObserver | null> { |
| 54 | const isIntersectionObserverAvailable = |
| 55 | typeof IntersectionObserver === 'function' |
| 56 | let observerRef: IntersectionObserver | null = null |
| 57 | |
| 58 | Solid.createEffect(() => { |
| 59 | const r = ref() |
| 60 | if (!r || !isIntersectionObserverAvailable || options.disabled) { |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | observerRef = new IntersectionObserver(([entry]) => { |
| 65 | callback(entry) |
| 66 | }, intersectionObserverOptions) |
| 67 | |
| 68 | observerRef.observe(r) |
| 69 | |
| 70 | Solid.onCleanup(() => { |
| 71 | observerRef?.disconnect() |
| 72 | }) |
| 73 | }) |
| 74 | |
| 75 | return () => observerRef |
| 76 | } |
no test coverage detected