Pagination
<cw-pagination> | CwPagination
Pagination lets users navigate between pages of content.
<cw-pagination total-items="120" page-size="10" page="1"></cw-pagination>
import CwPagination from '@cordwainer/cw-elements/dist/react/pagination'; const App = () => <CwPagination totalItems={120} pageSize={10} page={1} />;
Pagination is a controlled component — it doesn’t own or render your data. Listen for
cw-change and read the page property to know when to fetch or display a different
page.
Examples
Total Items and Page Size
Use the total-items and page-size attributes to determine how many pages are
available. The page attribute sets the current page, starting at 1.
<cw-pagination total-items="237" page-size="25" page="1"></cw-pagination>
import CwPagination from '@cordwainer/cw-elements/dist/react/pagination'; const App = () => <CwPagination totalItems={237} pageSize={25} page={1} />;
Listening for Page Changes
When the user clicks a page number or the previous/next buttons, the page property updates and
a cw-change event is emitted. Setting page programmatically does not emit
cw-change.
Current page: 1
<cw-pagination class="pagination-change" total-items="120" page-size="10" page="1"></cw-pagination> <p>Current page: <span class="pagination-change-output">1</span></p> <script> const pagination = document.querySelector('.pagination-change'); const output = document.querySelector('.pagination-change-output'); pagination.addEventListener('cw-change', () => { output.textContent = pagination.page; }); </script>
Sibling and Boundary Counts
When there are many pages, the page list collapses with an ellipsis. Use sibling-count to
control how many pages are shown on either side of the current page, and boundary-count to
control how many pages are always shown at the start and end.
<cw-pagination total-items="1000" page-size="10" page="50" sibling-count="2" boundary-count="2"></cw-pagination>
import CwPagination from '@cordwainer/cw-elements/dist/react/pagination'; const App = () => <CwPagination totalItems={1000} pageSize={10} page={50} siblingCount={2} boundaryCount={2} />;
Sizes
Use the size attribute to change the pagination’s size.
<cw-pagination total-items="120" page-size="10" page="2" size="small"></cw-pagination> <br /> <cw-pagination total-items="120" page-size="10" page="2" size="medium"></cw-pagination> <br /> <cw-pagination total-items="120" page-size="10" page="2" size="large"></cw-pagination>
import CwPagination from '@cordwainer/cw-elements/dist/react/pagination'; const App = () => ( <> <CwPagination totalItems={120} pageSize={10} page={2} size="small" /> <br /> <CwPagination totalItems={120} pageSize={10} page={2} size="medium" /> <br /> <CwPagination totalItems={120} pageSize={10} page={2} size="large" /> </> );
Disabled
Use the disabled attribute to disable the pagination control.
<cw-pagination total-items="120" page-size="10" page="2" disabled></cw-pagination>
import CwPagination from '@cordwainer/cw-elements/dist/react/pagination'; const App = () => <CwPagination totalItems={120} pageSize={10} page={2} disabled />;
Roadmap
The pagination control currently supports page-number navigation with collapsible ellipses. The following enhancements are planned for future releases:
-
Page size selector — an optional control for letting users change
page-sizethemselves. - Jump to page — an input for typing a page number directly.
- First/last page buttons — optional buttons for jumping straight to the first or last page.
[component-metadata:cw-pagination]
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@cordwainer/cw-elements@1.1.1/cdn/components/pagination/pagination.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@cordwainer/cw-elements@1.1.1/cdn/components/pagination/pagination.js';
To import this component using a bundler:
import '@cordwainer/cw-elements/dist/components/pagination/pagination.js';
To import this component as a React component:
import CwPagination from '@cordwainer/cw-elements/dist/react/pagination';
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
totalItems
total-items
|
The total number of items being paginated. |
number
|
0
|
|
pageSize
page-size
|
The number of items shown per page. Used with total-items to determine the total number
of pages.
|
number
|
10
|
|
page
|
The current page, 1-based. |
|
number
|
1
|
siblingCount
sibling-count
|
The number of page buttons to show on either side of the current page. |
number
|
1
|
|
boundaryCount
boundary-count
|
The number of page buttons to always show at the start and end of the page list. |
number
|
1
|
|
size
|
The pagination’s size. |
|
'small' | 'medium' | 'large'
|
'medium'
|
disabled
|
Disables the pagination control. |
|
boolean
|
false
|
totalPages
|
The total number of pages, derived from total-items and page-size. |
- | - | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
| Name | React Event | Description | Event Detail |
|---|---|---|---|
cw-change |
onCwChange |
Emitted when the current page changes as a result of user interaction. | - |
Learn more about events.
Parts
| Name | Description |
|---|---|
base |
The component’s base wrapper, a <nav> element. |
pages |
The container that wraps the page buttons and ellipses. |
page |
An individual page button. |
page--active |
Applied to the page button for the current page. |
ellipsis |
An ellipsis shown in place of collapsed page buttons. |
previous-button |
The button that navigates to the previous page. |
next-button |
The button that navigates to the next page. |
Learn more about customizing CSS parts.
Dependencies
This component automatically imports the following dependencies.
<cw-icon><cw-icon-button>