fix(presentation): render design-system links through the router (Q-6.8-9)
LinkButton, Breadcrumbs items and page link actions accept an additive `to` target rendered as a router Link, so in-app links keep the router basename and navigate without a document reload. `href` still renders a plain anchor. Pagination builds its page links from the current search (router location, or the document URL outside a router) and only replaces `page`, so other search parameters survive. Verified with tests/component/design-system-links.test.tsx (RED: 5 of 6 failed before the change) and the lane gates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f139b6f7e2
commit
a535db6ea1
@@ -23,6 +23,7 @@ export type {
|
||||
DialogProps,
|
||||
IconButtonProps,
|
||||
LinkButtonProps,
|
||||
LinkTarget,
|
||||
SpinnerProps,
|
||||
TextFieldProps,
|
||||
} from "./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<HTMLButtonElement, ButtonProps>(
|
||||
},
|
||||
);
|
||||
|
||||
export type LinkButtonProps = React.AnchorHTMLAttributes<HTMLAnchorElement> &
|
||||
/**
|
||||
* 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<HTMLAnchorElement>,
|
||||
"href"
|
||||
> &
|
||||
LinkTarget &
|
||||
Readonly<{
|
||||
href: string;
|
||||
variant?: ButtonVariant;
|
||||
size?: ButtonSize;
|
||||
}>;
|
||||
@@ -79,6 +92,7 @@ export const LinkButton = forwardRef<HTMLAnchorElement, LinkButtonProps>(
|
||||
function LinkButton(
|
||||
{
|
||||
href,
|
||||
to,
|
||||
variant = "primary",
|
||||
size = "default",
|
||||
className = "",
|
||||
@@ -86,20 +100,18 @@ export const LinkButton = forwardRef<HTMLAnchorElement, LinkButtonProps>(
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<a
|
||||
{...props}
|
||||
className={[
|
||||
"ui-button",
|
||||
`ui-button--${variant}`,
|
||||
size === "compact" ? "ui-button--compact" : "",
|
||||
className,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ")}
|
||||
href={href}
|
||||
ref={ref}
|
||||
/>
|
||||
const classes = [
|
||||
"ui-button",
|
||||
`ui-button--${variant}`,
|
||||
size === "compact" ? "ui-button--compact" : "",
|
||||
className,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
return to === undefined ? (
|
||||
<a {...props} className={classes} href={href} ref={ref} />
|
||||
) : (
|
||||
<Link {...props} className={classes} ref={ref} to={to} />
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -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 (
|
||||
<li key={`${item.label}-${index}`}>
|
||||
{item.href && !current ? (
|
||||
{item.to !== undefined && !current ? (
|
||||
<Link to={item.to}>{item.label}</Link>
|
||||
) : item.href && !current ? (
|
||||
<a href={item.href}>{item.label}</a>
|
||||
) : (
|
||||
<span aria-current={current ? "page" : undefined}>
|
||||
@@ -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({
|
||||
>
|
||||
<PreviousDirectionalIcon />
|
||||
</IconButton>
|
||||
{pages.map((candidate) => (
|
||||
<LinkButton
|
||||
aria-current={candidate === page ? "page" : undefined}
|
||||
href={`?page=${candidate}`}
|
||||
key={candidate}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
onChange(candidate);
|
||||
}}
|
||||
variant={candidate === page ? "primary" : "ghost"}
|
||||
>
|
||||
{pageLabel(candidate)}
|
||||
</LinkButton>
|
||||
))}
|
||||
{inRouter ? (
|
||||
<RouterPageLinks {...props} />
|
||||
) : (
|
||||
<PageLinks
|
||||
{...props}
|
||||
target={(candidate) => ({
|
||||
href: withPage(globalThis.location?.search ?? "", candidate),
|
||||
})}
|
||||
/>
|
||||
)}
|
||||
<IconButton
|
||||
accessibleName={nextLabel}
|
||||
disabled={page >= pageCount}
|
||||
@@ -197,3 +198,46 @@ export function Pagination({
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
|
||||
/** 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 (
|
||||
<PageLinks
|
||||
{...props}
|
||||
target={(candidate) => ({ 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) => (
|
||||
<LinkButton
|
||||
{...target(candidate)}
|
||||
aria-current={candidate === page ? "page" : undefined}
|
||||
key={candidate}
|
||||
onClick={(event) => {
|
||||
// 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)}
|
||||
</LinkButton>
|
||||
));
|
||||
}
|
||||
|
||||
@@ -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<{
|
||||
<div className={props.className ?? "page-template__actions"}>
|
||||
{props.actions.map((action) =>
|
||||
action.kind === "link" ? (
|
||||
<a
|
||||
className={`ui-button ui-button--${action.variant ?? "primary"}`}
|
||||
href={action.href}
|
||||
<LinkButton
|
||||
{...(action.to === undefined
|
||||
? { href: action.href }
|
||||
: { to: action.to })}
|
||||
key={`${action.kind}:${action.label}`}
|
||||
variant={action.variant}
|
||||
>
|
||||
{action.label}
|
||||
</a>
|
||||
</LinkButton>
|
||||
) : (
|
||||
<Button
|
||||
disabled={action.disabled}
|
||||
|
||||
Reference in New Issue
Block a user