diff --git a/docs/architecture/routing-pages-and-patterns.md b/docs/architecture/routing-pages-and-patterns.md index 492db3b8..bb5cfe0c 100644 --- a/docs/architecture/routing-pages-and-patterns.md +++ b/docs/architecture/routing-pages-and-patterns.md @@ -306,6 +306,10 @@ custom fallback을 넘겨 retry/reset 기능을 잃지 않게 한다. boundary `RouteLifecycle`은 POP이 아닐 때만 top으로 보낸다(`app-router.tsx`). `PageHeader`의 heading focus는 `preventScroll`이라 복원된 위치를 옮기지 않는다 - 새 primary route는 top으로 이동 +- 라우트 이동은 route ID나 pathname이 바뀐 것만이다. 검색값만 바뀐 경우(URL에 묶은 + 필터)와 지금 주소를 가리키는 링크는 같은 화면의 입력이라 focus·scroll을 건드리지 + 않고 `route.changed`도 남기지 않는다(`RouteLifecycle`). 검색값 변경에 top이 + 필요한 화면(페이지 번호)은 그 화면이 직접 scroll한다 - hash target은 fixed header offset과 focus 가능 여부를 처리. 페이지가 그려진 뒤 `PageHeader`가 대상으로 스크롤하고, 대상이 focus 가능하면 대상에, 아니면 heading에 focus한다(`page-header.tsx`). header 높이는 `html`의 diff --git a/src/presentation/routes/app-router.tsx b/src/presentation/routes/app-router.tsx index 87691d33..1fbea994 100644 --- a/src/presentation/routes/app-router.tsx +++ b/src/presentation/routes/app-router.tsx @@ -3,6 +3,7 @@ import { type ReactNode, Suspense, useEffect, + useEffectEvent, useRef, useState, } from "react"; @@ -169,7 +170,7 @@ function RouteLifecycle({ definition: RouteDefinition; buildId: string; }) { - const { pathname, search } = useLocation(); + const { pathname } = useLocation(); const navigationType = useNavigationType(); const { message, resolve } = useLocale(); const diagnostics = useApplicationDiagnostics(); @@ -180,7 +181,7 @@ function RouteLifecycle({ appName: message("common.appName"), }); }, [definition, message, resolve]); - useEffect(() => { + const announceRoute = useEffectEvent(() => { const main = document.getElementById("main-content"); main?.focus({ preventScroll: true }); try { @@ -199,7 +200,15 @@ function RouteLifecycle({ routeId: definition.routeId, buildId, }); - }, [buildId, definition, diagnostics, navigationType, pathname, search]); + }); + // 08-02. Only a new route or path is a navigation. A query-only change + // (a filter bound to the URL) or a link to the current URL is input on the + // same screen: focus stays on the control, the scroll stays, and no + // route.changed is reported. A screen that wants the top on a query change + // (a page number) scrolls itself. + useEffect(() => { + announceRoute(); + }, [definition.routeId, pathname]); return null; } diff --git a/tests/component/router.test.tsx b/tests/component/router.test.tsx index 18026297..03cb000d 100644 --- a/tests/component/router.test.tsx +++ b/tests/component/router.test.tsx @@ -1,10 +1,13 @@ // @vitest-environment jsdom -import { act, render, screen, waitFor } from "@testing-library/react"; +import { act, render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it, vi } from "vitest"; -import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-session-adapter.ts"; +import { + createAnonymousSessionAdapter, + createDemoSessionAdapter, +} from "../../src/adapters/auth/external-session-adapter.ts"; import type { ApplicationOutputPorts } from "../../src/application/create-application.ts"; import { INSTALLED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.ts"; import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx"; @@ -26,11 +29,12 @@ vi.mock("../../src/.generated/product-context-index.ts", async () => ({ function renderRouter( diagnostics: ApplicationOutputPorts["diagnostics"] = { record: () => {} }, productFeatures?: ApplicationOutputPorts["productFeatures"], + session: ApplicationOutputPorts["session"] = createAnonymousSessionAdapter(), ) { return render( { }, ); + it("keeps focus and scroll on a control that changes only the URL query", async () => { + // 08-02. A list filter bound to the query is input on the same screen + // (WCAG 3.2.2), not a route change: focus stays on the control and no + // second route.changed is reported. + const user = userEvent.setup(); + const record = vi.fn(); + window.history.pushState({}, "", "/fixture/items"); + renderRouter({ record }, undefined, createDemoSessionAdapter("authenticated")); + + const filter = await screen.findByLabelText("Fixture limit"); + await waitFor(() => expect(routeChangeReports(record)).toBe(1)); + await user.selectOptions(filter, "50"); + + await waitFor(() => expect(window.location.search).toBe("?limit=50")); + expect(filter).toHaveValue("50"); + expect(filter).toHaveFocus(); + expect(routeChangeReports(record)).toBe(1); + }); + + it("leaves focus on a link that names the page already shown", async () => { + // 08-02. React Router turns a link to the current URL into a REPLACE; the + // navigation type changes but the screen does not. + const user = userEvent.setup(); + const record = vi.fn(); + window.history.pushState({}, "", "/examples/ui"); + renderRouter({ record }); + + await waitFor(() => + expect( + screen.getByRole("heading", { name: "UI 구성요소", level: 1 }), + ).toHaveFocus(), + ); + const reportsBefore = routeChangeReports(record); + const link = within( + screen.getByRole("navigation", { name: "주요 탐색" }), + ).getByRole("link", { name: "UI 구성요소" }); + await user.click(link); + + expect(link).toHaveFocus(); + expect(routeChangeReports(record)).toBe(reportsBefore); + }); + it("ignores unknown query parameters on a platform route and keeps the URL", async () => { // D-16: `/?utm_source=x` or an OAuth callback parameter is not an invalid // address. The `none` codec drops it from the parsed input only. diff --git a/tests/helpers/fixture-feature.tsx b/tests/helpers/fixture-feature.tsx index 026bda41..8e5158ee 100644 --- a/tests/helpers/fixture-feature.tsx +++ b/tests/helpers/fixture-feature.tsx @@ -1,4 +1,4 @@ -import type { RouteObject } from "react-router-dom"; +import { type RouteObject, useSearchParams } from "react-router-dom"; import { z } from "zod"; import { defineBoundedContextModule } from "../../src/contexts/bounded-context-module.ts"; @@ -107,8 +107,24 @@ const FIXTURE_ROUTE_CODECS = Object.freeze({ }), }); +/** A filter bound to the URL query, the way a product list screen keeps it. */ function FixturePage() { - return

Fixture page

; + const [search, setSearch] = useSearchParams(); + return ( + <> +

Fixture page

+ + + ); } export const FIXTURE_CONTEXT_ROUTES: readonly RouteObject[] = Object.freeze(