-
Notifications
You must be signed in to change notification settings - Fork 527
Introduction of the <InfiniteScroll>
component
#2580
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces the
<InfiniteScroll>
component, building upon the work that @joetannenbaum started.It depends on PR #2561.
See also: inertiajs/inertia-laravel#774
Basic usage
On the server-side, there's a new scroll prop that provides metadata about the paginated resource. It also helps with deciding whether to append or prepend the items to the existing items, based on the scroll direction (more on that later).
On the front-end, all you have to do is wrap your items in the
<InfiniteScroll>
component and pass thedata
prop, similar to the<Deferred>
and<WhenVisible>
components.Loading direction and query string updates
The component automatically updates the URL as you scroll through the content. So even when you've scrolled all the way to, say, page 10, and you scroll up again, it will update the
page
param to9
,8
, etc. If you don't want to update the URL, you can pass thepreserve-url
prop. The nice thing about this is that when you refresh or freshly visit?page=8
, it will also load the previous pages as you scroll up, so by default, it triggers requests in both directions. You can change this by passing theonly-next
oronly-previous
prop.Buffer
To provide your users a smooth experience, you may pass a number of pixels to the
buffer
prop. When the scroll position reaches the specified number of pixels above the end of your content, the next request will be triggered.Manual mode
Instead of triggering the requests automatically, you may also use manual mode. In the
previous
andnext
slots, you may provide your own UI to load more content.The slot properties include:
loading
- Whether the content is currently loadingloadingPrevious
- Whether the previous content is loadingloadingNext
- Whether the next content is loadingfetch
- Function to manually trigger loading for this sidehasMore
- Whether more content is available in this sidemanualMode
- Whether manual mode is currently activeautoMode
- Whether automatic loading is currently activeYou may also choose to trigger a number of requests automatically, and then switch to manual mode. You can do that with the
manual-after
prop.Loading slot
If you don't use the
previous
ornext
slot, you may also pass aloading
slot to show an indicator.Custom element
By default, the
<InfiniteScroll>
component renders as adiv
, but you may change this by passing anas
prop.Reverse mode
Besides using this component for things like social feeds, photo grids and data tables, you may also use it for chat interfaces. There you'd typically paginate the messages in descending order.
On the front-end, you can pass the
reverse
prop, which will flip the triggers. Theprevious
trigger will be at the bottom, and thenext
trigger will be at the top.This means that scrolling up will trigger a request to the second page. It will also automatically prepend the incoming items instead of appending them. In reverse mode, it will automatically scroll to the bottom of the content on mount. You may disable this with the
auto-scroll
prop.Scroll preservation
When loading items before the current viewport, the component automatically preserves your visual scroll position so the content doesn't jump around as new items are prepended. This happens automatically. The component calculates the height difference and adjusts the scroll position to maintain visual stability.
Custom triggers
By default, the
<InfiniteScroll>
component renders the start and end triggers for you. For complex templates where the default trigger placement doesn't work, you can specify the triggers and slot with a CSS query selector.You may also use a
ref
.Scroll container
The component automatically detects when it's wrapped in a scroll container.
Programmatic access
You can get programmatic access to the component by using a
ref
.It exposes the following methods:
fetchNext()
- Manually fetch the next pagefetchPrevious()
- Manually fetch the previous pagehasNext()
- Whether there's a next pagehasPrevious()
- Whether there's a previous page