The Pagination component is used to navigate through a list of items that are split across multiple pages. It is a common pattern used in web applications to help users navigate through a large dataset.
This component’s root element is based on the <nav> element and can take in any additional HTML attributes. The following are the custom props that can be passed to the component.
Prop
Type
Default
totalPages *
The total number of pages for the dataset.
number
—
currentPage *
The current page number.
number
—
size
The size of the pagination component.
"sm" | "md" | "lg"
"md"
Pagination Logic
There are some core rules associated to pagination components that should be considered based on the total number of pages as well as the current page. These rules should be modified to fit your project’s use case, but in the case of this documentation the rules are as follows:
If there are less than 6 pages, all pages should be visible.
The first and last page should always be visible.
Show the current page and at least one page before and after it.
If there are 6 or more pages and the current page is more than 3 away from the start or end, ellipsis should be shown to indicate that there are more pages in between.
The previous and next buttons should be disabled if the current page is the first or last page, respectively.
With these rules defined, you end up getting all of the following variations:
The <Pagination> component has some predefined accessibility features that are important to consider when implementing it in your project. The following are some of the key accessibility features that are included in the component:
The root element is given an aria-label attribute providing context to screen readers on what the element is.
There are sr-only elements in each of the page buttons to provide context to screen readers on what the button is for. This includes both the Previous and Next buttons as well as the page numbers themselves.
Examples
Sizes
Use the size prop to adjust the size. Pagination comes in the following different sizes: sm, md, and lg. By default, pagination will use md unless otherwise specified. Sizes should be added, removed, and tweaked as necessary based on the needs of the project.
Pagination can be used on dark backgrounds by adding the dark class to the an ancestor DOM node. THis will invert the colors of the pagination component.
Due to the display logic necessary for pagination, it is recommended to use it as a component. This will allow you to pass in the total number of pages and the current page as props and have the component handle the display logic for you. In this case, we have put together the following Astro component to serve as a blueprint for how you possibly handle the logic in your own project.