Pagination
Pagination is an interface that allows navigating between pages that contain split information, instead of being shown on a single page.
Features
Install
Install the component from your command line.
Anatomy
Import all parts and piece them together.
<script setup lang="ts">import * as pagination from '@destyler/pagination'import { normalizeProps, useMachine } from '@destyler/vue'import { computed, useId } from 'vue'
const [state, send] = useMachine(pagination.machine({ id: useId(), count: 1000}))
const api = computed(() => pagination.connect(state.value, send, normalizeProps))</script>
<template> <nav v-bind="api.getRootProps()"> <ul> <li> <a v-bind="api.getPrevTriggerProps()"></a> </li> <li v-for="(page, i) in api.pages" :key="page.type === 'page' ? page.value : `ellipsis-${i}`"> <span v-if="page.type === 'page'"> <a v-bind="api.getItemProps(page)" ></a> </span> <span v-else> <span v-bind="api.getEllipsisProps({ index: i })" ></span> </span> </li> <li> <a v-bind="api.getNextTriggerProps()"></a> </li> </ul> </nav></template>import * as pagination from '@destyler/pagination'import { normalizeProps, useMachine } from '@destyler/react'import { useId } from 'react'
export default function Pagination() { const [state, send] = useMachine(pagination.machine({ id: useId(), count: 1000, }))
const api = pagination.connect(state, send, normalizeProps)
return ( <nav {...api.getRootProps()}> <ul> <li> <a {...api.getPrevTriggerProps()}></a> </li> {api.pages.map((page, i) => ( <li key={page.type === 'page' ? page.value : `ellipsis-${i}`}> {page.type === 'page' ? ( <a {...api.getItemProps(page)}></a> ) : ( <span {...api.getEllipsisProps({ index: i })}></span> )} </li> ))} <li> <a {...api.getNextTriggerProps()}></a> </li> </ul> </nav> )}<script lang="ts"> import * as pagination from '@destyler/pagination' import { normalizeProps, useMachine } from '@destyler/svelte'
const id = $props.id()
const [state, send] = useMachine(pagination.machine({ id, count: 1000 }))
const api = $derived(pagination.connect(state, send, normalizeProps))</script>
<nav {...api.getRootProps()}> <ul> <li> <a {...api.getPrevTriggerProps()}></a> </li> {#each api.pages as page, i} <li> {#if page.type === 'page'} <a {...api.getItemProps(page)}></a> {:else} <span {...api.getEllipsisProps({ index: i })}></span> {/if} </li> {/each} <li> <a {...api.getNextTriggerProps()}></a> </li> </ul></nav>import * as pagination from '@destyler/pagination'import { normalizeProps, useMachine } from '@destyler/solid'import { createMemo, createUniqueId } from 'solid-js'
export default function Pagination() { const [state, send] = useMachine(pagination.machine({ id: createUniqueId(), count: 1000, }))
const api = createMemo(() => pagination.connect(state, send, normalizeProps))
return ( <nav {...api().getRootProps()}> <ul> <li> <a {...api().getPrevTriggerProps()}></a> </li> {api().pages.map((page, i) => ( <li> {page.type === 'page' ? ( <a {...api().getItemProps(page)}></a> ) : ( <span {...api().getEllipsisProps({ index: i })}></span> )} </li> ))} <li> <a {...api().getNextTriggerProps()}></a> </li> </ul> </nav> )}Styling Guide
Earlier, we mentioned that each collapse part has a
data-partattribute added to them to select and style them in the DOM.
[data-part="root"] { /* styles for the pagination's root */}
[data-part="item"] { /* styles for the pagination's items */}
[data-part="ellipsis"] { /* styles for the pagination's ellipsis */}
[data-part="prev-trigger"] { /* styles for the pagination's previous page trigger */}
[data-part="next-trigger"] { /* styles for the pagination's next page trigger */}
/* We add a data-disabled attribute to the prev/next items when on the first/last page */
[data-part="prev-trigger"][data-disabled] { /* styles for the pagination's previous page trigger when on first page */}
[data-part="next-trigger"][data-disabled] { /* styles for the pagination's next page trigger when on first page */}Methods and Properties
Machine Context
The pagination machine exposes the following context properties:
ids
Partial<{ root: string; ellipsis: (index: number) => string; prevTrigger: string; nextTrigger: string; item: (page: number) => string; }>The ids of the elements in the accordion. Useful for composition.
translations
IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states
count
numberTotal number of data items
pageSize(default: 10)
numberNumber of data items per page
siblingCount(default: 1)
numberNumber of pages to show beside active page
page(default: 1)
numberThe active page
onPageChange
(details: PageChangeDetails) => voidCalled when the page number is changed
onPageSizeChange
(details: PageSizeChangeDetails) => voidCalled when the page size is changed
type(default: "button")
"button" | "link"The type of the trigger element
dir(default: "ltr")
"ltr" | "rtl"The document's text/writing direction.
id
stringThe unique identifier of the machine.
getRootNode
() => ShadowRoot | Node | DocumentA root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.
Machine API
The pagination api exposes the following methods:
page
numberThe current page.
count
numberThe total number of data items.
pageSize
numberThe number of data items per page.
totalPages
numberThe total number of pages.
pages
PagesThe page range. Represented as an array of page numbers (including ellipsis)
previousPage
numberThe previous page.
nextPage
numberThe next page.
pageRange
PageRangeThe page range. Represented as an object with `start` and `end` properties.
slice
<V>(data: V[]) => V[]Function to slice an array of data based on the current page.
setCount
(count: number) => voidFunction to set the total number of pages.
setPageSize
(size: number) => voidFunction to set the page size.
setPage
(page: number) => voidFunction to set the current page.
goToNextPage
() => voidFunction to go to the next page.
goToPrevPage
() => voidFunction to go to the previous page.
goToFirstPage
() => voidFunction to go to the first page.
goToLastPage
() => voidFunction to go to the last page.
Data Attributes
Item
attribute
description
data-scopepagination
data-partitem
data-indexThe index of the item
data-selectedPresent when selected
Prev Trigger
attribute
description
data-scopepagination
data-partprev-trigger
data-disabledPresent when disabled
Next Trigger
attribute
description
data-scopepagination
data-partnext-trigger
data-disabledPresent when disabled