-
Notifications
You must be signed in to change notification settings - Fork 4
Pagination
Link navigation for paginated routes
Note: This component is server side route base pagination, not client side pagination
---
import { Pagination } from 'astro-headless-ui';
---
<nav style="display:flex;gap:4px;">
<Pagination url="/posts" total="22" current="11">
<active slot="active">{page => <span>{page.number}</span>}</active>
<span slot="disabled">...</span>
{page => <a href={page.href}>{page.number}</a>}
</Pagination>
</nav>
<nav style="display:flex;gap:4px;">
<a href="/1">1</a>
<a href="/2">2</a>
<span>...</span>
<a href="/9">9</a>
<a href="/10">10</a>
<span>11</span>
<a href="/12">12</a>
<a href="/13">13</a>
<span>...</span>
<a href="/21">21</a>
<a href="/22">22</a>
</nav>
Default: /
Root path used to interpolate a href
for links '${url}/${page.number}'
Total number of pages
Current page number
Default: 2
Number of pages to display at the start
Default: 2
Number of pages to display at the end
Default: 2
Number of links to display on either side of the current active page/link
This prop is just an easier way of setting before
and after
to the same value
Default: middle
Number of links to display on the right side of the current active page/link
Default: middle
Number of links to display on the left side of the current active page/link
Default: false
true
: The first page link will link to the index of url
({url}/
)
false
: The first page link will link to the first page ({url}/1
)
Default: true
true
: Returns page number as a string number formatted with commas
false
: Return the page number as a number
Default: true
true
: Collapse the navigation, only the page links defined using the start
, current
, middle
, before
, after
and end
props will be shown
false
: Stops navigation from collapsing, a page link will be created for every single page number
All slots have access to a page
argument to use when defining templates for your link elements
<Pagination>
{page => <a href={page.href}>{page.number}</a>}
</Pagination>
interface page {
number: string | number; // Number of page
href: string; // href to page
// What type of link the page is
slot: 'link'|'first'|'disabled'|'before'|'active'|'after'|'last';
}
Example:
<Pagination url="/posts" total="22" current="11">
<active slot="active">{page => <span>{page.number}</span>}</active>
<span slot="disabled">...</span>
{page => <a href={page.href}>{page.number}</a>}
</Pagination>
<!-- or -->
<Pagination url="/posts" total="22" current="11">
{page => {
if (page.slot === "active") return <span>{page.number}</span>
if (page.slot === "disabled") return <span>...</span>
return <a href={page.href}>{page.number}</a>
}}
</Pagination>
The default slot, acts as a fallback if a slot is not defined
Targets any link that is not in the slots disabled
, first
, before
, active
, after
, or last
Targets the first link
Targets the space between the first
and before
links, and the after
and last
links, usually looks like three dots (...
). Represents the collapsed pages not included in pagination
Targets the links before the active
link
Target the current page's link
Targets the links after the active
link
Targets the last link
Target any page number
<Pagination>
<span slot="2">This is the second page's link</span>
</Pagination>
Demo that uses the <Paginate>
, <Pagination>
and <Breadcrumb>
components to paginate an array of 100 posts from jsonplaceholder