-
Notifications
You must be signed in to change notification settings - Fork 4
feat/infinite-scroll #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat/infinite-scroll #170
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const dummyPosts = Array.from({ length: 100 }, (_, i) => ({ | ||
id: i + 1, | ||
avatar: 'https://www.gravatar.com/avatar/2c7d99fe281ecd3bcd65ab915bac6dd5?s=250', | ||
username: `user${i + 1}`, | ||
imgUri: 'https://picsum.photos/800', | ||
postAlt: 'Sample', | ||
text: `This is post number ${i + 1}. Loving how these shots came out! 📸`, | ||
time: `${i + 1} hours ago`, | ||
count: { | ||
likes: Math.floor(Math.random() * 500), | ||
comments: Math.floor(Math.random() * 200) | ||
}, | ||
callback: { | ||
like: () => alert(`Like clicked on post ${i + 1}`), | ||
comment: () => alert(`Comment clicked on post ${i + 1}`), | ||
menu: () => alert(`Menu clicked on post ${i + 1}`) | ||
} | ||
})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
<script lang="ts"> | ||
</script> | ||
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,2 +1,80 @@ | ||||||||||||||||||||
<script lang="ts"> | ||||||||||||||||||||
import { Post } from '$lib/fragments'; | ||||||||||||||||||||
import { dummyPosts } from '$lib/dummyData'; | ||||||||||||||||||||
import { onMount } from 'svelte'; | ||||||||||||||||||||
type PostData = { | ||||||||||||||||||||
id: number; | ||||||||||||||||||||
avatar: string; | ||||||||||||||||||||
username: string; | ||||||||||||||||||||
imgUri: string; | ||||||||||||||||||||
postAlt: string; | ||||||||||||||||||||
text: string; | ||||||||||||||||||||
time: string; | ||||||||||||||||||||
count: { | ||||||||||||||||||||
likes: number; | ||||||||||||||||||||
comments: number; | ||||||||||||||||||||
}; | ||||||||||||||||||||
callback: { | ||||||||||||||||||||
like: () => void; | ||||||||||||||||||||
comment: () => void; | ||||||||||||||||||||
menu: () => void; | ||||||||||||||||||||
}; | ||||||||||||||||||||
}; | ||||||||||||||||||||
let listElement: HTMLElement; | ||||||||||||||||||||
let visiblePosts: PostData[] = $state([]); | ||||||||||||||||||||
let maxVisiblePosts = $state(20); | ||||||||||||||||||||
const batchSize = 10; | ||||||||||||||||||||
let currentIndex = $state(0); | ||||||||||||||||||||
let loading = $state(false); | ||||||||||||||||||||
const loadMore = () => { | ||||||||||||||||||||
if (loading || currentIndex >= dummyPosts.length) return; | ||||||||||||||||||||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
loading = true; | ||||||||||||||||||||
setTimeout(() => { | ||||||||||||||||||||
const nextBatch = dummyPosts.slice(currentIndex, currentIndex + batchSize); | ||||||||||||||||||||
visiblePosts = [...visiblePosts, ...nextBatch]; | ||||||||||||||||||||
if (visiblePosts.length > maxVisiblePosts) { | ||||||||||||||||||||
visiblePosts = visiblePosts.slice(visiblePosts.length - maxVisiblePosts); | ||||||||||||||||||||
} | ||||||||||||||||||||
currentIndex += batchSize; | ||||||||||||||||||||
loading = false; | ||||||||||||||||||||
}, 500); | ||||||||||||||||||||
}; | ||||||||||||||||||||
const onScroll = () => { | ||||||||||||||||||||
if (listElement.scrollTop + listElement.clientHeight >= listElement.scrollHeight) | ||||||||||||||||||||
loadMore(); | ||||||||||||||||||||
}; | ||||||||||||||||||||
$effect(() => { | ||||||||||||||||||||
listElement.addEventListener('scroll', onScroll); | ||||||||||||||||||||
return () => listElement.removeEventListener('scroll', onScroll); | ||||||||||||||||||||
}); | ||||||||||||||||||||
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null check for listElement to prevent runtime errors. The $effect(() => {
+ if (!listElement) return;
listElement.addEventListener('scroll', onScroll);
return () => listElement.removeEventListener('scroll', onScroll);
}); 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||
onMount(() => { | ||||||||||||||||||||
loadMore(); | ||||||||||||||||||||
}); | ||||||||||||||||||||
</script> | ||||||||||||||||||||
|
||||||||||||||||||||
<ul bind:this={listElement} class="hide-scrollbar h-[600px] overflow-auto"> | ||||||||||||||||||||
{#each visiblePosts as post} | ||||||||||||||||||||
<li class="mb-6"> | ||||||||||||||||||||
<Post | ||||||||||||||||||||
avatar={post.avatar} | ||||||||||||||||||||
username={post.username} | ||||||||||||||||||||
imgUri={post.imgUri} | ||||||||||||||||||||
postAlt={post.postAlt} | ||||||||||||||||||||
text={post.text} | ||||||||||||||||||||
time={post.time} | ||||||||||||||||||||
count={post.count} | ||||||||||||||||||||
callback={post.callback} | ||||||||||||||||||||
/> | ||||||||||||||||||||
</li> | ||||||||||||||||||||
{/each} | ||||||||||||||||||||
</ul> | ||||||||||||||||||||
|
||||||||||||||||||||
{#if loading} | ||||||||||||||||||||
<p class="my-4 text-center">Loading more posts…</p> | ||||||||||||||||||||
{/if} |
Uh oh!
There was an error while loading. Please reload this page.