|
| 1 | +<script lang="ts"> |
| 2 | + import { NumericalIdGenerator, type Id } from '$lib/ids.js'; |
| 3 | + import { mount, onMount, unmount, type Component, type Snippet } from 'svelte'; |
| 4 | +
|
| 5 | + const ID_ATTR = 'data-clui-list-item-id'; |
| 6 | + const ID_GENERATOR = new NumericalIdGenerator(); |
| 7 | +
|
| 8 | + declare type ItemData = any; |
| 9 | +
|
| 10 | + declare type ItemRenderer = Component<() => ItemData, any, any> | Snippet<[ItemData]>; |
| 11 | +
|
| 12 | + interface Props { |
| 13 | + itemsVisible?: number; |
| 14 | + startWith?: ItemData[]; |
| 15 | + itemRenderer: ItemRenderer; |
| 16 | + } |
| 17 | +
|
| 18 | + let { itemRenderer, startWith, itemsVisible = $bindable(0) }: Props = $props(); |
| 19 | +
|
| 20 | + let unorderedList: HTMLElement; |
| 21 | + let observer: IntersectionObserver; |
| 22 | +
|
| 23 | + let allItems: Record<Id, TrackedItem> = {}; |
| 24 | + let visibleItems: Record<Id, TrackedItem> = {}; |
| 25 | + let hiddenItems: Record<Id, TrackedItem> = {}; |
| 26 | +
|
| 27 | + class TrackedItem { |
| 28 | + public id: Id; |
| 29 | +
|
| 30 | + public data: ItemData; |
| 31 | + private li: HTMLLIElement; |
| 32 | + private component?: Record<string, any>; |
| 33 | +
|
| 34 | + constructor(li: HTMLLIElement, data: ItemData) { |
| 35 | + this.id = ID_GENERATOR.generate(); |
| 36 | + this.data = data; |
| 37 | +
|
| 38 | + this.li = li; |
| 39 | + this.li.setAttribute(ID_ATTR, this.id); |
| 40 | +
|
| 41 | + allItems[this.id] = this; |
| 42 | + } |
| 43 | +
|
| 44 | + get mounted() { |
| 45 | + return this.component ? true : false; |
| 46 | + } |
| 47 | +
|
| 48 | + public mount(intro?: boolean): void { |
| 49 | + if (this.component) { |
| 50 | + // console.debug(`[ListRenderer | ${this.id}]`, 'Element is already mounted!'); |
| 51 | + return; |
| 52 | + } |
| 53 | + // console.debug(`[ListRenderer | ${this.id}]`, 'Mounting element.'); |
| 54 | +
|
| 55 | + visibleItems[this.id] = this; |
| 56 | + delete hiddenItems[this.id]; |
| 57 | +
|
| 58 | + this.li.style.height = 'auto'; |
| 59 | +
|
| 60 | + this.component = mount(itemRenderer, { |
| 61 | + target: this.li, |
| 62 | + props: () => this.data, |
| 63 | + intro: intro |
| 64 | + }); |
| 65 | + } |
| 66 | +
|
| 67 | + public unmount(height: number): void { |
| 68 | + if (!this.component) { |
| 69 | + // console.debug(`[ListRenderer | ${this.id}]`, "Element isn't mounted!"); |
| 70 | + return; |
| 71 | + } |
| 72 | + // console.debug(`[ListRenderer | ${this.id}]`, 'Unmounting element.'); |
| 73 | +
|
| 74 | + delete visibleItems[this.id]; |
| 75 | + hiddenItems[this.id] = this; |
| 76 | +
|
| 77 | + this.li.style.height = height + 'px'; |
| 78 | +
|
| 79 | + unmount(this.component, { |
| 80 | + outro: false |
| 81 | + }); |
| 82 | + this.component = undefined; |
| 83 | + } |
| 84 | +
|
| 85 | + public destroy() { |
| 86 | + this.unmount(0); |
| 87 | +
|
| 88 | + delete allItems[this.id]; |
| 89 | + delete visibleItems[this.id]; |
| 90 | + delete hiddenItems[this.id]; |
| 91 | +
|
| 92 | + observer.unobserve(this.li); |
| 93 | + this.li.remove(); |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + onMount(() => { |
| 98 | + const callback: IntersectionObserverCallback = (entries) => { |
| 99 | + entries.forEach((entry) => { |
| 100 | + const id = (entry.target as HTMLLIElement).getAttribute(ID_ATTR) as string; |
| 101 | + const isVisible = entry.isIntersecting; |
| 102 | +
|
| 103 | + if (isVisible) { |
| 104 | + const trackedItem = hiddenItems[id]; |
| 105 | + if (!trackedItem) return; // Already mounted. |
| 106 | +
|
| 107 | + trackedItem.mount(); |
| 108 | + } else { |
| 109 | + const trackedItem = visibleItems[id]; |
| 110 | + if (!trackedItem) return; // Already unmounted. |
| 111 | +
|
| 112 | + const height = entry.boundingClientRect.height; // The height of the element when it is fully visible. |
| 113 | + trackedItem.unmount(height); |
| 114 | + } |
| 115 | +
|
| 116 | + itemsVisible = Object.keys(visibleItems).length; |
| 117 | + }); |
| 118 | + }; |
| 119 | +
|
| 120 | + observer = new IntersectionObserver(callback, { |
| 121 | + root: unorderedList |
| 122 | + }); |
| 123 | +
|
| 124 | + startWith?.forEach(addItem); |
| 125 | +
|
| 126 | + return () => { |
| 127 | + observer.disconnect(); |
| 128 | +
|
| 129 | + for (const item of Object.values(visibleItems)) { |
| 130 | + // console.debug("We're going away! Unmounting:", item.id); |
| 131 | + item.unmount(0); |
| 132 | + } |
| 133 | + }; |
| 134 | + }); |
| 135 | +
|
| 136 | + export function addItem(data: ItemData) { |
| 137 | + const li = document.createElement('li'); |
| 138 | + const tracked = new TrackedItem(li, data); |
| 139 | +
|
| 140 | + unorderedList.appendChild(li); |
| 141 | + tracked.mount(true); |
| 142 | + observer.observe(li); |
| 143 | + } |
| 144 | +
|
| 145 | + /** |
| 146 | + * Note that this function is useless if `data` is an object. It is only recommended to use this function if you're using some form of identifier, such as a string or a number. |
| 147 | + */ |
| 148 | + export function removeItem(data: ItemData) { |
| 149 | + for (const tracked of Object.values(allItems)) { |
| 150 | + if (tracked.data === data) { |
| 151 | + tracked.destroy(); |
| 152 | + return; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +</script> |
| 157 | + |
| 158 | +<ul bind:this={unorderedList}></ul> |
| 159 | + |
| 160 | +<style> |
| 161 | + ul { |
| 162 | + overflow-y: auto; |
| 163 | + height: 100%; |
| 164 | + margin: 0; |
| 165 | + } |
| 166 | +</style> |
0 commit comments