The Pagination component provides a flexible navigation interface for paginated content. It's designed to handle various pagination scenarios with customizable options for page display, navigation, and information display.
import { Pagination } from '@dashflowx/core'export default function PaginationExample() {return (<PaginationcurrentPage={2}totalPages={10}/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function BasicPagination() {return (<PaginationcurrentPage={2}totalPages={10}/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function PaginationWithFirstLast() {return (<PaginationcurrentPage={5}totalPages={20}showFirstLast={true}/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function CompactPagination() {return (<PaginationcurrentPage={3}totalPages={15}maxVisiblePages={3}variant="compact"/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function MinimalPagination() {return (<PaginationcurrentPage={4}totalPages={8}variant="minimal"/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function ExtendedPagination() {return (<PaginationcurrentPage={10}totalPages={100}maxVisiblePages={7}showFirstLast={true}variant="extended"/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function SizeVariations() {return (<div className="space-y-4">{/* Small */}<PaginationcurrentPage={2}totalPages={10}size="sm"/>{/* Medium (default) */}<PaginationcurrentPage={2}totalPages={10}size="md"/>{/* Large */}<PaginationcurrentPage={2}totalPages={10}size="lg"/></div>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function PaginationWithInfo() {return (<PaginationcurrentPage={2}totalItems={150}itemsPerPage={10}showInfo={true}/>)}
Preview:
import { Pagination } from '@dashflowx/core'export default function PaginationWithPageSize() {return (<PaginationcurrentPage={1}totalItems={200}itemsPerPage={20}showPageSize={true}pageSizeOptions={[10, 20, 50, 100]}/>)}
Preview:
import { Pagination } from '@dashflowx/core'import { useState } from 'react'export default function InteractivePagination() {const [currentPage, setCurrentPage] = useState(1);const [itemsPerPage, setItemsPerPage] = useState(10);const totalItems = 150;const handlePageChange = (page: number) => {setCurrentPage(page);};const handlePageSizeChange = (pageSize: number) => {setItemsPerPage(pageSize);setCurrentPage(1); // Reset to first page when changing page size};return (<PaginationcurrentPage={currentPage}totalItems={totalItems}itemsPerPage={itemsPerPage}showFirstLast={true}showInfo={true}showPageSize={true}pageSizeOptions={[5, 10, 20, 50]}onPageChange={handlePageChange}onPageSizeChange={handlePageSizeChange}/>)}
Preview:
const DataTablePagination = () => (<PaginationcurrentPage={currentPage}totalItems={totalItems}itemsPerPage={itemsPerPage}showFirstLast={true}showInfo={true}showPageSize={true}variant="extended"/>);
const SearchResultsPagination = () => (<PaginationcurrentPage={searchPage}totalItems={searchResults.length}itemsPerPage={resultsPerPage}showFirstLast={false}maxVisiblePages={3}variant="compact"/>);
const BlogPagination = () => (<PaginationcurrentPage={currentPost}totalPages={totalPosts}showFirstLast={true}maxVisiblePages={7}variant="extended"size="lg"/>);
const ApiPagination = () => (<PaginationcurrentPage={apiPage}totalItems={apiTotalItems}itemsPerPage={apiItemsPerPage}showFirstLast={true}showInfo={true}onPageChange={handleApiPageChange}onPageSizeChange={handleApiPageSizeChange}/>);
Organize pagination logically and consistently.
// ✅ Good - Clear structure<PaginationcurrentPage={currentPage}totalItems={totalItems}itemsPerPage={itemsPerPage}showFirstLast={true}showInfo={true}/>// ❌ Avoid - Confusing structure<div>Random page numbers</div>
Maintain consistent styling across your application.
// ✅ Good - Consistent styling<PaginationclassName="standard-pagination-style"variant="default"size="md"currentPage={currentPage}totalItems={totalItems}/>// ❌ Avoid - Inconsistent styling<PaginationclassName="random-styles"variant="minimal"size="lg"currentPage={currentPage}totalItems={totalItems}/>
Ensure pagination is accessible to all users.
// ✅ Good - Accessible<PaginationcurrentPage={currentPage}totalItems={totalItems}showFirstLast={true}showInfo={true}/>// ❌ Avoid - Not accessible<div>Page numbers without proper structure</div>
Make pagination responsive for different screen sizes.
// ✅ Good - Responsive<PaginationcurrentPage={currentPage}totalItems={totalItems}maxVisiblePages={window.innerWidth < 768 ? 3 : 7}variant="compact"/>// ❌ Avoid - Fixed width<PaginationcurrentPage={currentPage}totalItems={totalItems}className="w-96"/>
Handle page changes properly.
// ✅ Good - Proper handlingconst handlePageChange = (page: number) => {setCurrentPage(page);fetchData(page);};const handlePageSizeChange = (pageSize: number) => {setItemsPerPage(pageSize);setCurrentPage(1);fetchData(1, pageSize);};// ❌ Avoid - No handling<PaginationcurrentPage={currentPage}totalItems={totalItems}/>
You can customize the pagination appearance using CSS classes:
<PaginationclassName="border border-gray-300 rounded-lg p-4"currentPage={currentPage}totalItems={totalItems}/>
Use different variants for different contexts:
// Compact variant for mobile<PaginationcurrentPage={currentPage}totalItems={totalItems}variant="compact"maxVisiblePages={3}/>// Extended variant for desktop<PaginationcurrentPage={currentPage}totalItems={totalItems}variant="extended"maxVisiblePages={7}showFirstLast={true}/>
Use different sizes for different emphasis levels:
// Small pagination for secondary content<Paginationsize="sm"currentPage={currentPage}totalItems={totalItems}/>// Large pagination for primary content<Paginationsize="lg"currentPage={currentPage}totalItems={totalItems}/>
The pagination utility functions provide powerful state management and helper functions for working with pagination.
import { Pagination } from '@dashflowx/core'import { usePagination } from '@/utils/pagination'export default function PaginationWithHook() {const { state, actions } = usePagination({currentPage: 1,totalItems: 150,itemsPerPage: 10});const handlePageChange = (page: number) => {actions.setCurrentPage(page);// Fetch new data here};const handlePageSizeChange = (pageSize: number) => {actions.setItemsPerPage(pageSize);// Fetch new data with new page size};return (<PaginationcurrentPage={state.currentPage}totalItems={state.totalItems}itemsPerPage={state.itemsPerPage}showFirstLast={true}showInfo={true}showPageSize={true}onPageChange={handlePageChange}onPageSizeChange={handlePageSizeChange}/>)}
Preview:
import { Pagination } from '@dashflowx/core'import { paginationPresets } from '@/utils/pagination'export default function PresetPagination() {return (<div className="space-y-4">{/* Small preset */}<PaginationcurrentPage={2}totalPages={10}{...paginationPresets.small}/>{/* Medium preset */}<PaginationcurrentPage={2}totalPages={10}{...paginationPresets.medium}/>{/* Large preset */}<PaginationcurrentPage={2}totalPages={10}{...paginationPresets.large}/></div>)}
Preview:
import { paginationUtils } from '@/utils/pagination'export default function UtilityExample() {const currentPage = 3;const totalPages = 15;const itemsPerPage = 10;const totalItems = 150;// Get page rangeconst pageRange = paginationUtils.getPageRange(currentPage, totalPages, 5);// Get pagination infoconst paginationInfo = paginationUtils.getPaginationInfo(currentPage, itemsPerPage, totalItems);// Calculate offset for API callsconst offset = paginationUtils.getOffset(currentPage, itemsPerPage);return (<div className="space-y-4"><div><strong>Page Range:</strong> {pageRange.join(', ')}</div><div><strong>Info:</strong> {paginationInfo.text}</div><div><strong>Offset:</strong> {offset}</div></div>)}
import { Pagination } from '@dashflowx/core'import { usePagination } from '@/utils/pagination'export default function ManualPagination() {const { state, actions } = usePagination({currentPage: 1,totalItems: 200,itemsPerPage: 20});return (<div className="space-y-4">{/* Manual controls */}<div className="flex gap-2"><buttononClick={actions.goToFirstPage}disabled={state.currentPage === 1}className="px-3 py-1 bg-blue-500 text-white rounded disabled:opacity-50">First</button><buttononClick={actions.goToPreviousPage}disabled={state.currentPage === 1}className="px-3 py-1 bg-blue-500 text-white rounded disabled:opacity-50">Previous</button><buttononClick={actions.goToNextPage}disabled={state.currentPage === state.totalPages}className="px-3 py-1 bg-blue-500 text-white rounded disabled:opacity-50">Next</button><buttononClick={actions.goToLastPage}disabled={state.currentPage === state.totalPages}className="px-3 py-1 bg-blue-500 text-white rounded disabled:opacity-50">Last</button></div>{/* Pagination component */}<PaginationcurrentPage={state.currentPage}totalItems={state.totalItems}itemsPerPage={state.itemsPerPage}showFirstLast={true}showInfo={true}onPageChange={actions.setCurrentPage}/></div>)}
Create dynamic pagination based on data:
import { usePagination } from '@/utils/pagination'const [paginationData, setPaginationData] = useState({currentPage: 1,totalItems: 0,itemsPerPage: 10});const { state, actions } = usePagination(paginationData);useEffect(() => {// Load pagination data based on API responseloadPaginationData().then(setPaginationData);}, []);
Show/hide pagination based on conditions:
const getPaginationConfig = (totalItems: number) => {const baseConfig = {currentPage: 1,totalItems,itemsPerPage: 10};if (totalItems <= itemsPerPage) {return null; // Don't show pagination}return baseConfig;};
Handle pagination state:
const [paginationState, setPaginationState] = useState({currentPage: 1,itemsPerPage: 10});const handlePaginationChange = (page: number) => {setPaginationState(prev => ({...prev,currentPage: page}));// Update URL or fetch new dataupdateUrl(page);fetchData(page);};const handlePageSizeChange = (pageSize: number) => {setPaginationState(prev => ({...prev,itemsPerPage: pageSize,currentPage: 1}));fetchData(1, pageSize);};
- NavigationMenu: For navigation menus
- MenuList: For menu lists
- Button: For pagination triggers
- Link: For pagination links
| Prop | Type | Default | Description |
|---|---|---|---|
currentPage | number | 1 | Current active page number |
totalPages | number | 10 | Total number of pages |
totalItems | number | undefined | Total number of items (used to calculate totalPages) |
itemsPerPage | number | 10 | Number of items per page |
className | string | '' | Additional CSS classes |
showFirstLast | boolean | false | Show/hide first and last page buttons |
showPrevNext | boolean | true | Show/hide previous and next buttons |
showEllipsis | boolean | true | Show/hide ellipsis (...) for hidden pages |
maxVisiblePages | number | 5 | Maximum number of visible page buttons |
variant | 'default' | 'minimal' | 'compact' | 'extended' | 'default' | Visual variant of the pagination |
size | 'sm' | 'md' | 'lg' | 'md' | Size of the pagination component |
disabled | boolean | false | Disable all pagination interactions |
showInfo | boolean | false | Show information text (e.g., "Showing 1-10 of 100 items") |
showPageSize | boolean | false | Show page size selector |
pageSizeOptions | number[] | [10, 20, 50, 100] | Available page size options |
preset | 'small' | 'medium' | 'large' | 'compact' | 'extended' | undefined | Apply predefined pagination configuration |
onPageChange | (page: number) => void | undefined | Callback function when page changes |
onPageSizeChange | (pageSize: number) => void | undefined | Callback function when page size changes |
default: Standard pagination with normal spacingminimal: Minimal spacing between elementscompact: Compact spacing for tight layoutsextended: Extended spacing for prominent display
sm: Small size for secondary contentmd: Medium size (default) for standard contentlg: Large size for primary content
small: Compact pagination with minimal visible pagesmedium: Standard pagination with balanced featureslarge: Extended pagination with maximum visible pagescompact: Minimal pagination without ellipsisextended: Full-featured pagination with all options
The @/utils/pagination module provides several utility functions:
usePagination(initialState): React hook for state managementpaginationUtils.getPageRange(): Calculate visible page rangepaginationUtils.getOffset(): Calculate data offset for API callspaginationUtils.calculateTotalPages(): Calculate total pages from itemspaginationUtils.isValidPage(): Validate page numberpaginationUtils.getPaginationInfo(): Get pagination info textpaginationPresets: Predefined configuration objects
The onPageChange callback receives the new page number:
const handlePageChange = (page: number) => {console.log('Page changed to:', page);// Update state, fetch data, update URL, etc.};
The onPageSizeChange callback receives the new page size:
const handlePageSizeChange = (pageSize: number) => {console.log('Page size changed to:', pageSize);// Update state, reset to first page, fetch data, etc.};
For the complete API reference and advanced usage patterns, see the Pagination API documentation.
