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> )}Controlled usage
Pagination supports controllable page and pageSize. Prefer defaultPage / defaultPageSize for uncontrolled seeds. Passing page / pageSize into machine(...) is presence-controlled — sync with onPageChange / onPageSizeChange and adapter context. See Controllable State.
pagination.machine({ id: "1", count: 100, defaultPage: 1, defaultPageSize: 10,})
const page = 1 // your reactive statepagination.machine({ id: "1", count: 100, page, onPageChange(details) { // details => { page: number, pageSize: number } },})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:
Partial<{ root: string; ellipsis: (index: number) => string; prevTrigger: string; nextTrigger: string; item: (page: number) => string; }>IntlTranslationsnumbernumbernumbernumbernumbernumber(details: PageChangeDetails) => void(details: PageSizeChangeDetails) => void"button" | "link""ltr" | "rtl"string() => ShadowRoot | Node | DocumentMachine API
The pagination api exposes the following methods:
numbernumbernumbernumberPagesnumbernumberPageRange<V>(data: V[]) => V[](count: number) => void(size: number) => void(page: number) => void() => void() => void() => void() => voidData Attributes
Item
data-scopedata-partdata-indexdata-selectedPrev Trigger
data-scopedata-partdata-disabledNext Trigger
data-scopedata-partdata-disabled