Components › Components
Demos show your theme.
Pagination
Links to the pages of a long list, shortened with ellipses.
Usage #
tscl
@import uiPagination from './pagination.tscl'
<div class="demo">
<uiPagination [page]="1" [pages]="5" href="/components/pagination?page={page}" />
<uiPagination [page]="50" [pages]="100" href="/components/pagination?page={page}" label="Archive pages" />
</div>
<style scoped>
:scope {
display: grid;
gap: 12px;
padding: 24px;
border: 1px solid var(--ui-border);
border-radius: var(--ui-radius);
background: var(--ui-bg);
color: var(--ui-fg);
}
</style>Source #
Copy it next to your components as pagination.tscl.
tscl
{{-- Pagination: links to pages of a list. href is a pattern with {page}; long ranges keep the first, last and
neighbours of the current page, with ellipses between. Plain links: no JavaScript. Two on one page need
different labels. --}}
@input page: number
@input pages: number
@input href: string
@input label: string = 'Pagination'
<script server>
const link = (n) => href.replaceAll('{page}', String(n))
const shown = new Set([1, pages, page - 1, page, page + 1].filter((n) => n >= 1 && n <= pages))
if (pages <= 7) for (let n = 1; n <= pages; n++) shown.add(n)
// Typed by its first shape, then emptied: a plain [] would type as never[].
const items = [{ kind: 'page', n: 0, key: '' }].slice(0, 0)
let previous = 0
for (const n of [...shown].sort((a, b) => a - b)) {
// An ellipsis stands for two or more pages; one missing page is shown as itself.
if (n - previous === 2) items.push({ kind: n - 1 === page ? 'current' : 'page', n: n - 1, key: 'p-' + (n - 1) })
else if (n - previous > 2) items.push({ kind: 'gap', n: 0, key: 'gap-' + n })
items.push({ kind: n === page ? 'current' : 'page', n, key: 'p-' + n })
previous = n
}
</script>
<nav [aria-label]="label" data-part="pagination">
<ul data-part="pagination-list">
@if (page > 1) {
<li><a [href]="link(page - 1)" rel="prev" data-part="pagination-prev">Previous</a></li>
}
@for (item of items; track item.key) {
<li>
@if (item.kind === 'gap') {
<span aria-hidden="true" data-part="pagination-ellipsis">…</span>
} @else if (item.kind === 'current') {
<span aria-current="page" data-part="pagination-current">{{ item.n }}</span>
} @else {
<a [href]="link(item.n)" data-part="pagination-link">{{ item.n }}</a>
}
</li>
}
@if (page < pages) {
<li><a [href]="link(page + 1)" rel="next" data-part="pagination-next">Next</a></li>
}
</ul>
</nav>
<style scoped>
@layer theme, base, components, utilities;
@layer components {
:where([data-part='pagination-list']) {
display: flex;
flex-wrap: wrap;
gap: 4px;
margin: 0;
padding: 0;
list-style: none;
font: var(--ui-font-size) var(--ui-font);
}
:where([data-part='pagination-list'] a),
:where([data-part='pagination-current']),
:where([data-part='pagination-ellipsis']) {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 36px;
min-height: 36px;
padding: 0 10px;
border-radius: var(--ui-radius);
color: var(--ui-fg);
text-decoration: none;
}
:where([data-part='pagination-list'] a:hover) {
background: var(--ui-accent-soft);
}
:where([data-part='pagination-list'] a:focus-visible) {
outline: 2px solid var(--ui-ring);
outline-offset: 2px;
}
:where([data-part='pagination-current']) {
border: 1px solid var(--ui-border);
font-weight: 600;
}
:where([data-part='pagination-ellipsis']) {
color: var(--ui-muted);
}
}
</style>Styling #
- On the root:
class,style,id,aria-*anddata-*written on the tag land on its root element;classandstylemerge with its own. - Selectors:
data-partnamespagination,pagination-list,pagination-prev,pagination-ellipsis,pagination-current,pagination-link,pagination-next. - Your rules win: every rule here has zero specificity (
:where()) and sits in@layer components, so your own CSS and Tailwind utilities override it.
Needs #
- theme.css, once per project