-
Notifications
You must be signed in to change notification settings - Fork 4
Pagination
Navigation for a paginated routes
Note: This is component is generated server side for paginated routes like /posts/1
, /posts/2
, /posts/3
, its does NOT work for client side pagination
Example:
---
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>
Output:
<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 pages to display on either side of the current active page
Default: false
The first link will link to the index of url
(${url}/
) instead of first page (${url}/1
)
Default: true
Uses commas in page numbers if true, use false for a number instead of a string
Default: true
Collapses navigation, only the pages defined using the start
, current
, middle
, and end
props will be shown
If false a link element will appear for every page number in the total
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 other slots are 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 first
and before
links/slots, and the after
and last
links/slots. 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
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';
}