96 lines · 4.9 KBRaw
| 1 | import { Tabs as BaseTabs } from "@base-ui/react/tabs"; |
| 2 | import { CaretLeftIcon, CaretRightIcon } from "../icons"; |
| 3 | import type { ComponentProps, ReactNode } from "react"; |
| 4 | import { Link } from "../provider"; |
| 5 | import { Button } from "./button"; |
| 6 | import { cn } from "./cn"; |
| 7 | |
| 8 | export interface TabItem { |
| 9 | value: string; |
| 10 | label: ReactNode; |
| 11 | /** Renders the tab as another element, such as a link, so a view tab can be opened in a new tab. */ |
| 12 | render?: ComponentProps<typeof BaseTabs.Tab>["render"]; |
| 13 | nativeButton?: boolean; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * A row of views. `underline` marks the active view with an ink line; `segmented` is a compact switch |
| 18 | * between two or three filters, with segments of equal width. |
| 19 | */ |
| 20 | export function Tabs({ tabs, value, onValueChange, variant = "underline", className, listClassName }: { tabs: readonly TabItem[]; value: string; onValueChange?: (value: string) => void; variant?: "underline" | "segmented"; size?: string; className?: string; listClassName?: string }) { |
| 21 | const underline = variant === "underline"; |
| 22 | return ( |
| 23 | <BaseTabs.Root value={value} onValueChange={(next) => onValueChange?.(next as string)} className={className}> |
| 24 | <BaseTabs.List className={cn(underline ? "scroll-fade flex gap-5 overflow-x-auto pr-6 shadow-[inset_0_-1px_0_var(--hairline)] [scrollbar-width:none]" : "inline-grid auto-cols-fr grid-flow-col rounded-control bg-sunken p-0.5 shadow-[inset_0_0_0_1px_var(--hairline)]", listClassName)}> |
| 25 | {tabs.map((tab) => ( |
| 26 | <BaseTabs.Tab |
| 27 | key={tab.value} |
| 28 | value={tab.value} |
| 29 | render={tab.render} |
| 30 | nativeButton={tab.nativeButton} |
| 31 | className={cn( |
| 32 | "group/tab inline-flex shrink-0 cursor-pointer items-center gap-1.5 whitespace-nowrap text-muted outline-none transition-colors duration-150 hover:text-ink focus-visible:text-ink data-[active]:text-ink", |
| 33 | underline |
| 34 | ? "relative pt-1.5 pb-2.5 text-base font-medium after:absolute after:inset-x-0 after:bottom-0 after:h-0.5 after:rounded-t-full after:bg-transparent data-[active]:after:bg-ink" |
| 35 | : "h-7 justify-center rounded-chip px-3 text-compact font-medium data-[active]:bg-control data-[active]:shadow-control pointer-coarse:h-9", |
| 36 | )} |
| 37 | > |
| 38 | {tab.label} |
| 39 | </BaseTabs.Tab> |
| 40 | ))} |
| 41 | </BaseTabs.List> |
| 42 | </BaseTabs.Root> |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** A count beside a tab label. */ |
| 47 | export function TabCount({ children }: { children: ReactNode }) { |
| 48 | return <span className="text-xs font-normal text-faint tabular-nums group-data-[active]/tab:text-muted">{children}</span>; |
| 49 | } |
| 50 | |
| 51 | function BreadcrumbsRoot({ className, children }: { className?: string; children: ReactNode }) { |
| 52 | return ( |
| 53 | <nav aria-label="Location" className={cn("flex min-w-0 items-center gap-1.5 text-compact", className)}> |
| 54 | {children} |
| 55 | </nav> |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** Where a page sits: each part links up, the last part is the current page. */ |
| 60 | export const Breadcrumbs = Object.assign(BreadcrumbsRoot, { |
| 61 | Link: ({ href, className, ...props }: ComponentProps<"a"> & { href: string }) => <Link href={href} className={cn("truncate text-muted hover:text-ink", className)} {...props} />, |
| 62 | Current: ({ className, ...props }: ComponentProps<"span">) => <span aria-current="page" className={cn("truncate font-medium text-ink", className)} {...props} />, |
| 63 | Separator: () => ( |
| 64 | <span aria-hidden className="text-faint"> |
| 65 | / |
| 66 | </span> |
| 67 | ), |
| 68 | }); |
| 69 | |
| 70 | /** Newer and older pages of a list. */ |
| 71 | export function Pagination({ page, hasNextPage, setPage }: { page: number; perPage?: number; hasNextPage: boolean; controls?: string; setPage: (page: number) => void }) { |
| 72 | return ( |
| 73 | <nav aria-label="Pages" className="flex items-center justify-center gap-2"> |
| 74 | <Button size="sm" icon={<CaretLeftIcon />} disabled={page <= 1} onClick={() => setPage(page - 1)}> |
| 75 | Newer |
| 76 | </Button> |
| 77 | <span className="px-2 text-xs text-muted tabular-nums">Page {page}</span> |
| 78 | <Button size="sm" disabled={!hasNextPage} onClick={() => setPage(page + 1)}> |
| 79 | Older <CaretRightIcon /> |
| 80 | </Button> |
| 81 | </nav> |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | function TableRoot({ className, ...props }: ComponentProps<"table">) { |
| 86 | return <table className={cn("w-full border-collapse text-left text-compact", className)} {...props} />; |
| 87 | } |
| 88 | |
| 89 | /** A dense data table: hairline rows, quiet headers, tabular numerals. */ |
| 90 | export const Table = Object.assign(TableRoot, { |
| 91 | Header: (props: ComponentProps<"thead">) => <thead {...props} />, |
| 92 | Body: (props: ComponentProps<"tbody">) => <tbody {...props} />, |
| 93 | Row: ({ className, ...props }: ComponentProps<"tr">) => <tr className={cn("shadow-[inset_0_-1px_0_var(--hairline)] last:shadow-none", className)} {...props} />, |
| 94 | Head: ({ className, ...props }: ComponentProps<"th">) => <th className={cn("caption px-4 py-2.5 text-faint", className)} {...props} />, |
| 95 | Cell: ({ className, ...props }: ComponentProps<"td">) => <td className={cn("px-4 py-3 align-middle", className)} {...props} />, |
| 96 | }); |