forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

table-pagination.tsx83 lines, 3.5 KiBHistoryRawDownload
1import { Show } from "solid-js";
2import { Button } from "@/components/ui/button";
3import { IconChevronLeft, IconChevronRight } from "@/components/ui/icons";
4import { cn } from "@/lib/cn";
5import { useT } from "@/stores/preferences-context";
6
7export type TablePaginationProps = {
8 /** Zero-based current page index. */
9 pageIndex: number;
10 pageCount: number;
11 /** With `total`, prints "a-b / total"; without either only the page counter shows. */
12 pageSize?: number;
13 /** Total row count (across all pages). */
14 total?: number;
15 onPageChange: (pageIndex: number) => void;
16 class?: string;
17};
18
19/**
20 * Footer: "a-b / total" + prev / "page / count" / next, on one row at every
21 * width. The buttons are full touch targets on phones and touch screens,
22 * compact otherwise. A single page keeps the row count but drops the buttons — two disabled
23 * arrows around "1 / 1" only take room.
24 */
25export function TablePagination(props: TablePaginationProps) {
26 const t = useT();
27 const hasRange = () => props.total != null && props.pageSize != null;
28 const start = () => (!props.total || !props.pageSize ? 0 : props.pageIndex * props.pageSize + 1);
29 const end = () => Math.min((props.pageIndex + 1) * (props.pageSize ?? 0), props.total ?? 0);
30 const navButton = "h-10 w-10 gap-1 px-0 text-xs sm:h-7 sm:w-auto sm:px-1.5 sm:text-[11px] touch:h-10 touch:min-w-10";
31
32 return (
33 // Nothing to count and nowhere to go: an empty frame would only take room.
34 <Show when={hasRange() || props.pageCount > 1}>
35 <div
36 class={cn(
37 "flex w-full items-center gap-2 rounded-lg border border-border-line bg-surface-overlay px-2.5 py-1.5",
38 // With no row count to show, the empty left slot pushed the buttons
39 // to the right edge; alone, they sit in the middle.
40 hasRange() ? "justify-between" : "justify-center",
41 props.class,
42 )}
43 >
44 <Show when={hasRange()}>
45 <span class="text-xs font-medium tabular-nums text-muted-foreground sm:text-[11px]">
46 {t("common.pageRange", { start: start(), end: end(), total: props.total ?? 0 })}
47 </span>
48 </Show>
49 <Show when={props.pageCount > 1}>
50 <nav class="flex items-center gap-1" aria-label={t("common.pagination")}>
51 <Button
52 type="button"
53 variant="outline"
54 size="sm"
55 class={navButton}
56 aria-label={t("common.prev")}
57 disabled={props.pageIndex <= 0}
58 onClick={() => props.onPageChange(Math.max(0, props.pageIndex - 1))}
59 >
60 <IconChevronLeft class="h-4 w-4 sm:h-3 sm:w-3" />
61 <span class="hidden sm:inline">{t("common.prev")}</span>
62 </Button>
63 <span class="min-w-14 rounded-md border border-border-line bg-surface-base px-1.5 py-1 text-center text-xs font-semibold tabular-nums text-foreground sm:min-w-12 sm:py-0.5 sm:text-[11px]">
64 {t("common.pageOf", { page: props.pageIndex + 1, total: props.pageCount })}
65 </span>
66 <Button
67 type="button"
68 variant="outline"
69 size="sm"
70 class={navButton}
71 aria-label={t("common.next")}
72 disabled={props.pageIndex >= props.pageCount - 1}
73 onClick={() => props.onPageChange(Math.min(props.pageCount - 1, props.pageIndex + 1))}
74 >
75 <span class="hidden sm:inline">{t("common.next")}</span>
76 <IconChevronRight class="h-4 w-4 sm:h-3 sm:w-3" />
77 </Button>
78 </nav>
79 </Show>
80 </div>
81 </Show>
82 );
83}