|
| 1 | +<script> |
| 2 | + // @ts-check |
| 3 | + /** |
| 4 | + * Array of HTML Elements to observe. |
| 5 | + * Use this for better performance when observing multiple elements. |
| 6 | + * @type {HTMLElement[]} |
| 7 | + */ |
| 8 | + export let elements = []; |
| 9 | +
|
| 10 | + /** |
| 11 | + * Set to `true` to unobserve the element |
| 12 | + * after it intersects the viewport. |
| 13 | + * @type {boolean} |
| 14 | + */ |
| 15 | + export let once = false; |
| 16 | +
|
| 17 | + /** |
| 18 | + * Specify the containing element. |
| 19 | + * Defaults to the browser viewport. |
| 20 | + * @type {null | HTMLElement} |
| 21 | + */ |
| 22 | + export let root = null; |
| 23 | +
|
| 24 | + /** Margin offset of the containing element. */ |
| 25 | + export let rootMargin = "0px"; |
| 26 | +
|
| 27 | + /** |
| 28 | + * Percentage of element visibility to trigger an event. |
| 29 | + * Value must be between 0 and 1. |
| 30 | + */ |
| 31 | + export let threshold = 0; |
| 32 | +
|
| 33 | + /** |
| 34 | + * Map of element to its intersection state. |
| 35 | + * @type {Map<HTMLElement, boolean>} |
| 36 | + */ |
| 37 | + export let elementIntersections = new Map(); |
| 38 | +
|
| 39 | + /** |
| 40 | + * Map of element to its latest entry. |
| 41 | + * @type {Map<HTMLElement, IntersectionObserverEntry>} |
| 42 | + */ |
| 43 | + export let elementEntries = new Map(); |
| 44 | +
|
| 45 | + /** |
| 46 | + * `IntersectionObserver` instance. |
| 47 | + * @type {null | IntersectionObserver} |
| 48 | + */ |
| 49 | + export let observer = null; |
| 50 | +
|
| 51 | + import { tick, createEventDispatcher, afterUpdate, onMount } from "svelte"; |
| 52 | +
|
| 53 | + const dispatch = createEventDispatcher(); |
| 54 | +
|
| 55 | + /** @type {null | string} */ |
| 56 | + let prevRootMargin = null; |
| 57 | +
|
| 58 | + /** @type {null | HTMLElement} */ |
| 59 | + let prevElement = null; |
| 60 | +
|
| 61 | + /** @type {HTMLElement[]} */ |
| 62 | + let prevElements = []; |
| 63 | +
|
| 64 | + const initialize = () => { |
| 65 | + observer = new IntersectionObserver( |
| 66 | + (entries) => { |
| 67 | + entries.forEach((_entry) => { |
| 68 | + const target = /** @type {HTMLElement} */ (_entry.target); |
| 69 | +
|
| 70 | + elementIntersections.set(target, _entry.isIntersecting); |
| 71 | + elementEntries.set(target, _entry); |
| 72 | +
|
| 73 | + // Trigger reactivity. |
| 74 | + elementIntersections = new Map(elementIntersections); |
| 75 | + elementEntries = new Map(elementEntries); |
| 76 | +
|
| 77 | + dispatch("observe", { entry: _entry, target }); |
| 78 | +
|
| 79 | + if (_entry.isIntersecting) { |
| 80 | + dispatch("intersect", { entry: _entry, target }); |
| 81 | + if (once) observer?.unobserve(target); |
| 82 | + } |
| 83 | + }); |
| 84 | + }, |
| 85 | + { root, rootMargin, threshold }, |
| 86 | + ); |
| 87 | + }; |
| 88 | +
|
| 89 | + onMount(() => { |
| 90 | + initialize(); |
| 91 | +
|
| 92 | + return () => { |
| 93 | + if (observer) { |
| 94 | + observer.disconnect(); |
| 95 | + observer = null; |
| 96 | + } |
| 97 | + }; |
| 98 | + }); |
| 99 | +
|
| 100 | + afterUpdate(async () => { |
| 101 | + await tick(); |
| 102 | +
|
| 103 | + if (elements.length > 0) { |
| 104 | + const newElements = elements.filter((el) => !prevElements.includes(el)); |
| 105 | + newElements.forEach((el) => { |
| 106 | + if (el) observer?.observe(el); |
| 107 | + }); |
| 108 | +
|
| 109 | + const removedElements = prevElements.filter( |
| 110 | + (el) => !elements.includes(el), |
| 111 | + ); |
| 112 | + removedElements.forEach((el) => { |
| 113 | + if (el) observer?.unobserve(el); |
| 114 | + }); |
| 115 | +
|
| 116 | + prevElements = [...elements]; |
| 117 | + } |
| 118 | +
|
| 119 | + if (prevRootMargin && rootMargin !== prevRootMargin) { |
| 120 | + observer?.disconnect(); |
| 121 | + prevElement = null; |
| 122 | + prevElements = []; |
| 123 | + initialize(); |
| 124 | +
|
| 125 | + elements.forEach((el) => { |
| 126 | + if (el) observer?.observe(el); |
| 127 | + }); |
| 128 | + } |
| 129 | +
|
| 130 | + prevRootMargin = rootMargin; |
| 131 | + }); |
| 132 | +</script> |
| 133 | +
|
| 134 | +<slot {observer} {elementIntersections} {elementEntries} /> |
0 commit comments