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}
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import type { ReactNode } from "react";
|
||||
import { MemoryRouter, Route, Routes, useLocation } from "react-router-dom";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
Breadcrumbs,
|
||||
LinkButton,
|
||||
Pagination,
|
||||
} from "../../src/presentation/design-system/index.ts";
|
||||
import { StandardPage } from "../../src/presentation/templates/index.ts";
|
||||
|
||||
function LocationProbe() {
|
||||
const location = useLocation();
|
||||
return <output data-testid="location">{`${location.pathname}${location.search}`}</output>;
|
||||
}
|
||||
|
||||
function renderInRouter(ui: ReactNode, entry = "/app/items?q=term&sort=name") {
|
||||
return render(
|
||||
<MemoryRouter basename="/app" initialEntries={[entry]}>
|
||||
<Routes>
|
||||
<Route
|
||||
path="*"
|
||||
element={
|
||||
<>
|
||||
{ui}
|
||||
<LocationProbe />
|
||||
</>
|
||||
}
|
||||
/>
|
||||
</Routes>
|
||||
</MemoryRouter>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("design-system links inside the router", () => {
|
||||
it("renders LinkButton `to` as a router link that keeps the basename", async () => {
|
||||
const user = userEvent.setup();
|
||||
renderInRouter(<LinkButton to="/examples/states">States</LinkButton>);
|
||||
|
||||
const link = screen.getByRole("link", { name: "States" });
|
||||
expect(link).toHaveAttribute("href", "/app/examples/states");
|
||||
expect(link).toHaveClass("ui-button", "ui-button--primary");
|
||||
|
||||
await user.click(link);
|
||||
expect(screen.getByTestId("location")).toHaveTextContent(
|
||||
/^\/examples\/states$/,
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps LinkButton `href` as a plain anchor", () => {
|
||||
renderInRouter(<LinkButton href="https://example.com/docs">Docs</LinkButton>);
|
||||
expect(screen.getByRole("link", { name: "Docs" })).toHaveAttribute(
|
||||
"href",
|
||||
"https://example.com/docs",
|
||||
);
|
||||
});
|
||||
|
||||
it("renders breadcrumb `to` items as router links", async () => {
|
||||
const user = userEvent.setup();
|
||||
renderInRouter(
|
||||
<Breadcrumbs
|
||||
items={[
|
||||
{ label: "Home", to: "/" },
|
||||
{ label: "Examples", to: "/examples" },
|
||||
{ label: "States" },
|
||||
]}
|
||||
label="Location"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByRole("link", { name: "Home" })).toHaveAttribute(
|
||||
"href",
|
||||
"/app",
|
||||
);
|
||||
await user.click(screen.getByRole("link", { name: "Examples" }));
|
||||
expect(screen.getByTestId("location")).toHaveTextContent(/^\/examples$/);
|
||||
expect(screen.getByText("States")).toHaveAttribute("aria-current", "page");
|
||||
});
|
||||
|
||||
it("renders page link actions with `to` as router links", async () => {
|
||||
const user = userEvent.setup();
|
||||
renderInRouter(
|
||||
<StandardPage
|
||||
actions={[
|
||||
{ kind: "link", label: "Open states", to: "/examples/states" },
|
||||
{ kind: "link", label: "External", href: "https://example.com" },
|
||||
]}
|
||||
heading={{ title: "Page" }}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByRole("link", { name: "External" })).toHaveAttribute(
|
||||
"href",
|
||||
"https://example.com",
|
||||
);
|
||||
const link = screen.getByRole("link", { name: "Open states" });
|
||||
expect(link).toHaveAttribute("href", "/app/examples/states");
|
||||
await user.click(link);
|
||||
expect(screen.getByTestId("location")).toHaveTextContent(
|
||||
/^\/examples\/states$/,
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the other search parameters and the basename in pagination links", async () => {
|
||||
const user = userEvent.setup();
|
||||
const onChange = vi.fn();
|
||||
renderInRouter(
|
||||
<Pagination
|
||||
label="Pages"
|
||||
nextLabel="Next"
|
||||
onChange={onChange}
|
||||
page={1}
|
||||
pageCount={3}
|
||||
pageLabel={(page) => `Page ${page}`}
|
||||
previousLabel="Previous"
|
||||
/>,
|
||||
"/app/items?q=term&sort=name&page=1",
|
||||
);
|
||||
|
||||
expect(screen.getByRole("link", { name: "Page 2" })).toHaveAttribute(
|
||||
"href",
|
||||
"/app/items?q=term&sort=name&page=2",
|
||||
);
|
||||
await user.click(screen.getByRole("link", { name: "Page 3" }));
|
||||
expect(onChange).toHaveBeenCalledWith(3);
|
||||
// Page changes stay with the owner: the link itself does not navigate.
|
||||
expect(screen.getByTestId("location")).toHaveTextContent(
|
||||
"/items?q=term&sort=name&page=1",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("pagination outside a router", () => {
|
||||
it("keeps the other search parameters of the document URL", () => {
|
||||
window.history.pushState({}, "", "/list?q=term");
|
||||
render(
|
||||
<Pagination
|
||||
label="Pages"
|
||||
nextLabel="Next"
|
||||
onChange={() => {}}
|
||||
page={1}
|
||||
pageCount={2}
|
||||
pageLabel={(page) => `Page ${page}`}
|
||||
previousLabel="Previous"
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByRole("link", { name: "Page 2" })).toHaveAttribute(
|
||||
"href",
|
||||
"?q=term&page=2",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user