import type { HTMLAttributes } from "react"; import { cn } from "./cn"; export type PageFolioVariant = "current" | "total" | "slash" | "of" | "prefix"; export type PageFolioNumberFormat = "plain" | "2-digit" | "3-digit"; export type PageFolioProps = Omit, "children"> & { variant?: PageFolioVariant; currentFormat?: PageFolioNumberFormat; totalFormat?: PageFolioNumberFormat; prefix?: string; separator?: string; ofLabel?: string; ariaLabel?: string; }; export function PageFolio({ variant = "current", currentFormat = "plain", totalFormat = "plain", prefix = "", separator = "/", ofLabel = "of", ariaLabel, className, ...rest }: PageFolioProps) { const current = placeholderForFormat(currentFormat); const total = placeholderForFormat(totalFormat); const label = ariaLabel ?? defaultAriaLabel(variant); return ( {renderFolioParts({ variant, current, total, currentFormat, totalFormat, prefix, separator, ofLabel })} ); } function renderFolioParts({ variant, current, total, currentFormat, totalFormat, prefix, separator, ofLabel, }: { variant: PageFolioVariant; current: string; total: string; currentFormat: PageFolioNumberFormat; totalFormat: PageFolioNumberFormat; prefix: string; separator: string; ofLabel: string; }) { if (variant === "total") { return {total}; } if (variant === "slash") { return ( <> {current} {separator} {total} ); } if (variant === "of") { return ( <> {current} {ofLabel} {total} ); } if (variant === "prefix") { return ( <> {prefix} {current} ); } return {current}; } function placeholderForFormat(format: PageFolioNumberFormat) { if (format === "3-digit") return "000"; if (format === "2-digit") return "00"; return "0"; } function defaultAriaLabel(variant: PageFolioVariant) { if (variant === "total") return "Total pages"; if (variant === "slash" || variant === "of") return "Page number and total pages"; return "Page number"; }