diff --git a/src/presentation/design-system/index.ts b/src/presentation/design-system/index.ts index 11f42a6c..3467658e 100644 --- a/src/presentation/design-system/index.ts +++ b/src/presentation/design-system/index.ts @@ -23,6 +23,7 @@ export type { DialogProps, IconButtonProps, LinkButtonProps, + LinkTarget, SpinnerProps, TextFieldProps, } from "./primitives/core.tsx"; diff --git a/src/presentation/design-system/primitives/core.tsx b/src/presentation/design-system/primitives/core.tsx index 7b7be11c..681af63a 100644 --- a/src/presentation/design-system/primitives/core.tsx +++ b/src/presentation/design-system/primitives/core.tsx @@ -6,6 +6,7 @@ import { useRef, } from "react"; import { createPortal } from "react-dom"; +import { Link, type To } from "react-router-dom"; import { CloseIcon } from "../icons/semantic-icons.tsx"; import { useLocale } from "../../i18n/index.ts"; @@ -68,9 +69,21 @@ export const Button = forwardRef( }, ); -export type LinkButtonProps = React.AnchorHTMLAttributes & +/** + * Where a link goes. `to` is an in-app destination rendered as a router link, + * so it keeps the router basename and navigates without a document reload. + * `href` stays a plain anchor for external or document-level targets. + */ +export type LinkTarget = + | Readonly<{ href: string; to?: undefined }> + | Readonly<{ to: To; href?: undefined }>; + +export type LinkButtonProps = Omit< + React.AnchorHTMLAttributes, + "href" +> & + LinkTarget & Readonly<{ - href: string; variant?: ButtonVariant; size?: ButtonSize; }>; @@ -79,6 +92,7 @@ export const LinkButton = forwardRef( function LinkButton( { href, + to, variant = "primary", size = "default", className = "", @@ -86,20 +100,18 @@ export const LinkButton = forwardRef( }, ref, ) { - return ( - + const classes = [ + "ui-button", + `ui-button--${variant}`, + size === "compact" ? "ui-button--compact" : "", + className, + ] + .filter(Boolean) + .join(" "); + return to === undefined ? ( + + ) : ( + ); }, ); diff --git a/src/presentation/design-system/primitives/navigation.tsx b/src/presentation/design-system/primitives/navigation.tsx index bc69c711..5fde71f9 100644 --- a/src/presentation/design-system/primitives/navigation.tsx +++ b/src/presentation/design-system/primitives/navigation.tsx @@ -1,12 +1,20 @@ import { useId, useRef, useState } from "react"; +import { + Link, + useInRouterContext, + useLocation, + type To, +} from "react-router-dom"; import { useLocale } from "../../i18n/index.ts"; import { NextIcon, PreviousIcon } from "../icons/semantic-icons.tsx"; -import { IconButton, LinkButton } from "./core.tsx"; +import { IconButton, LinkButton, type LinkTarget } from "./core.tsx"; export type BreadcrumbItem = Readonly<{ label: string; href?: string; + /** In-app destination, rendered as a router link. Wins over `href`. */ + to?: To; }>; export function Breadcrumbs({ @@ -20,7 +28,9 @@ export function Breadcrumbs({ const current = index === items.length - 1; return (
  • - {item.href && !current ? ( + {item.to !== undefined && !current ? ( + {item.label} + ) : item.href && !current ? ( {item.label} ) : ( @@ -140,15 +150,7 @@ export function Tabs({ ); } -export function Pagination({ - label, - page, - pageCount, - previousLabel, - nextLabel, - pageLabel, - onChange, -}: Readonly<{ +type PaginationProps = Readonly<{ label: string; page: number; pageCount: number; @@ -156,8 +158,11 @@ export function Pagination({ nextLabel: string; pageLabel(page: number): string; onChange(page: number): void; -}>) { - const pages = Array.from({ length: pageCount }, (_, index) => index + 1); +}>; + +export function Pagination(props: PaginationProps) { + const { label, page, pageCount, previousLabel, nextLabel, onChange } = props; + const inRouter = useInRouterContext(); const { direction } = useLocale(); const PreviousDirectionalIcon = direction === "rtl" ? NextIcon : PreviousIcon; @@ -172,20 +177,16 @@ export function Pagination({ > - {pages.map((candidate) => ( - { - event.preventDefault(); - onChange(candidate); - }} - variant={candidate === page ? "primary" : "ghost"} - > - {pageLabel(candidate)} - - ))} + {inRouter ? ( + + ) : ( + ({ + href: withPage(globalThis.location?.search ?? "", candidate), + })} + /> + )} = pageCount} @@ -197,3 +198,46 @@ export function Pagination({ ); } + +/** Keeps every other search parameter and only replaces `page`. */ +function withPage(search: string, page: number): string { + const params = new URLSearchParams(search); + params.set("page", String(page)); + return `?${params}`; +} + +function RouterPageLinks(props: PaginationProps) { + const { search } = useLocation(); + return ( + ({ to: { search: withPage(search, candidate) } })} + /> + ); +} + +function PageLinks({ + page, + pageCount, + pageLabel, + onChange, + target, +}: PaginationProps & Readonly<{ target(page: number): LinkTarget }>) { + const pages = Array.from({ length: pageCount }, (_, index) => index + 1); + return pages.map((candidate) => ( + { + // The owner decides how a page change is applied; the href is for + // open-in-new-tab and copy-link. + event.preventDefault(); + onChange(candidate); + }} + variant={candidate === page ? "primary" : "ghost"} + > + {pageLabel(candidate)} + + )); +} diff --git a/src/presentation/templates/page-templates.tsx b/src/presentation/templates/page-templates.tsx index d6e423c1..56f459d1 100644 --- a/src/presentation/templates/page-templates.tsx +++ b/src/presentation/templates/page-templates.tsx @@ -2,6 +2,10 @@ import type { ReactNode } from "react"; import { PageHeader } from "../components/page-header.tsx"; import { Button } from "../components/ui/button.ts"; +import { + LinkButton, + type LinkTarget, +} from "../design-system/primitives/core.tsx"; import { useLocale } from "../i18n/index.ts"; export type PageHeading = Readonly<{ @@ -18,12 +22,12 @@ export type PageActionDefinition = disabled?: boolean; variant?: "primary" | "secondary" | "danger" | "ghost"; }> - | Readonly<{ + | (Readonly<{ kind: "link"; label: string; - href: string; variant?: "primary" | "secondary" | "danger" | "ghost"; - }>; + }> & + LinkTarget); export type PageTemplateSlots = Readonly<{ heading: PageHeading; @@ -245,13 +249,15 @@ function PageActionBar(props: Readonly<{
    {props.actions.map((action) => action.kind === "link" ? ( - {action.label} - + ) : (