leptos_pagination/
lib.rs

1//! Pagination for Leptos.
2//!
3//! This crate contains hooks and components for easy pagination of data.
4//! It provides efficient loading, caching and displaying of large data. At the same time it
5//! is very easy to use even for small datasets.
6//!
7//! ## Usage
8//!
9//! ```
10//! # use leptos::prelude::*;
11//! # use leptos_pagination::*;
12//! # use std::ops::Range;
13//! #
14//! pub struct Book {
15//!     title: String,
16//! }
17//!
18//! // Implement one of the loader traits for this struct (not shown here, see below).
19//! pub struct BookLoader;
20//!
21//! # // Dummy implementation for demonstration purposes
22//! # impl MemoryLoader for BookLoader {
23//! #     type Item = Book;
24//! #     type Query = ();
25//! #     fn load_items(&self, range: Range<usize>, _query: &()) -> Vec<Self::Item> {
26//! #         vec![]
27//! #     }
28//! #     fn item_count(&self, _query: &()) -> usize {
29//! #         0
30//! #     }
31//! # }
32//! #
33//! # #[component]
34//! # pub fn App() -> impl IntoView {
35//! let state = PaginationState::new_store();
36//!
37//! view! {
38//!     <ul>
39//!         <PaginatedFor loader=BookLoader query=() state item_count_per_page=10 let:idx_book>
40//!             // idx_book is a tuple containing the index and the book data
41//!             <li>{idx_book.1.title.clone()}</li>
42//!         </PaginatedFor>
43//!     </ul>
44//!
45//!     <PaginationPrev state>"Prev"</PaginationPrev>
46//!     <PaginationNext state>"Next"</PaginationNext>
47//!
48//!     <nav>
49//!         <PaginationPages state />
50//!     </nav>
51//! }
52//! # }
53//! ```
54//!
55//! ## Loading data
56//!
57//! Loading data is done through implementing one of the various `Loader` traits. Depending on your use case
58//! you should implement the trait that best fits your needs:
59//!
60//! - [`MemoryLoader`]: If your dataset is already in memory like in a `Vec`, `HashSet`, array, ...
61//! - [`PaginatedLoader`]: If your data source provides data in pages (independent of if you use UI pagination or virtualization).
62//! - [`ExactLoader`]: If your data source can provide an exact range of items (start index to end index).
63//! - [`Loader`]: If none of the above fit your needs, you can implement this trait to provide your own loading logic.
64//!
65//! Please refer to the documentation and the examples to see how to implement these traits.
66//!
67//! ## Components
68//!
69//! This crate provides several components designed to help you with the pagination of data.
70//! These components are:
71//!
72//! - [`PaginatedFor`]: A component that displays a list of items in a paginated manner.
73//! - [`PaginationPages`]: A component that displays the buttons to jump to a certain page.
74//! - [`PaginationNext`]: A component that displays a button to navigate to the next page.
75//! - [`PaginationPrev`]: A component that displays a button to navigate to the previous page.
76//!
77//! Please refer to the examples to see how to use these components.
78//!
79//! ## Hooks
80//!
81//! All components are just thin wrappers that add commonly used html to hook functions that implement the actual logic.
82//! So if you want to customize your markup more than what the pre-made components allow you can use these hooks directly.
83//!
84//! These are the hooks:
85//!
86//! - [`use_pagination`]: Logic for [`PaginatedFor`]. Handles loading items on-demand from the data source and caching them.
87//! - [`use_pagination_controls`]: Logic for [`PaginationPages`]. Returns page ranges that can be used to display pagination controls.
88//!
89//! If you want to implement your own custom components using these hooks, please have a look at the pre-made components in this crate.
90//! You'll see that there is really nothing special about them.
91
92mod components;
93mod hooks;
94mod state;
95
96pub use components::*;
97pub use hooks::*;
98pub use state::*;
99
100pub use leptos_windowing::*;