use_infinite_scroll
Infinite scrolling of the element.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use_infinite_scroll::{use_infinite_scroll_with_options, UseInfiniteScrollOptions};
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let (data, set_data) = signal(vec![1, 2, 3, 4, 5, 6]);
let _ = use_infinite_scroll_with_options(
el,
move |_| async move {
let len = data.with(|d| d.len());
set_data.update(|data| *data = (1..len+6).collect());
},
UseInfiniteScrollOptions::default().distance(10.0),
);
view! {
<div node_ref=el>
<For each=move || data.get() key=|i| *i let:item>{ item }</For>
</div>
}
}
The returned signal is true while new data is being loaded.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server-side, use_infinite_scroll will return a signal that is always false.
It effectively does nothing on the server, as there are no scroll events to listen to.
Installation
cargo add leptos-use-infinite-scroll
Options
UseInfiniteScrollOptions
Options for use_infinite_scroll_with_options.
Every option is set with a builder method of the same name, e.g. UseInfiniteScrollOptions::default().on_scroll(...). See also the API docs.
on_scroll
Type: Arc<dyn Fn(web_sys::Event) + Send + Sync>
Callback when scrolling is happening.
event_listener_options
Type: UseEventListenerOptions
Options passed to the addEventListener("scroll", ...) call
distance
Type: f64
The minimum distance between the bottom of the element and the bottom of the viewport. Default is 0.0.
direction
Type: Direction
The direction in which to listen the scroll. Defaults to Direction::Bottom.
interval
Type: f64
The interval time between two load more (to avoid too many invokes). Default is 100.0.