Skip to content

Instantly share code, notes, and snippets.

@Tombarr
Created May 1, 2023 22:04
Show Gist options
  • Save Tombarr/5db694a3690c9e0083d1511a519a9b23 to your computer and use it in GitHub Desktop.
Save Tombarr/5db694a3690c9e0083d1511a519a9b23 to your computer and use it in GitHub Desktop.
Infinite scroll Svelte component that appends DOM elements in increments of page sizes as the user scrolls beyond a threshold
<!--
InfiniteScroll.svelte
Author: Tom Barrasso
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
// Sample Usage
<script>
const THRESHOLD = 100; // px
function incrementPage() {
if (pagedItems.length < items.length) {
dispatch('page', { page: ++page });
}
}
</script>
<ol class="list">
{#each pagedItems as subitem, i}
<li class="list-item">
<slot {subitem} {i} />
</li>
{/each}
</ol>
<InfiniteScroll threshold={THRESHOLD} on:loadMore={incrementPage} />
-->
<script>
// Modified from: https://github.com/andrelmlins/svelte-infinite-scroll
import { onMount, onDestroy, createEventDispatcher } from "svelte";
export let threshold = 0;
export let hasMore = true;
const dispatch = createEventDispatcher();
function supportsListenerOption(attr) {
let optionSupported = false;
try {
const options = {
get [attr]() {
optionSupported = true;
return false;
}
};
window.addEventListener("test", null, options);
window.removeEventListener("test", null, options);
} catch (err) {
optionSupported = false;
}
return optionSupported;
}
let isLoadMore = false;
const supportsPassive = supportsListenerOption("passive");
onMount(() => {
requestAnimationFrame(() => {
const element = getElement();
const options = (supportsPassive) ? { passive: true } : false;
element.addEventListener("scroll", onScroll, options);
element.addEventListener("resize", onScroll, options);
});
});
const onScroll = (e) => {
if (!hasMore) return;
const offset = calcOffset(e);
if (offset <= threshold) {
if (!isLoadMore && hasMore) {
dispatch("loadMore");
}
isLoadMore = true;
} else {
isLoadMore = false;
}
};
const calcOffset = (e, reverse = false, horizontal = false) => {
const element = e.target.documentElement
? e.target.documentElement
: e.target;
if (reverse) {
return horizontal ? element.scrollLeft : element.scrollTop;
} else {
return horizontal
? element.scrollWidth - element.clientWidth - element.scrollLeft
: element.scrollHeight - element.clientHeight - element.scrollTop;
}
};
const getElement = () =>
(document.getElementById('viewList') || document.body);
onDestroy(() => {
const element = getElement();
if (element) {
element.removeEventListener("scroll", onScroll);
element.removeEventListener("resize", onScroll);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment