diff --git a/src/presentation/design-system/primitives/pagination.tsx b/src/presentation/design-system/primitives/pagination.tsx index 82f9b46a..610a6e08 100644 --- a/src/presentation/design-system/primitives/pagination.tsx +++ b/src/presentation/design-system/primitives/pagination.tsx @@ -32,10 +32,15 @@ export function Pagination(props: PaginationProps) { className="ui-pagination" role={nested ? "group" : undefined} > + {/* At the first or last page the edge button is aria-disabled, not + disabled: pressing "previous" on page 2 must not drop keyboard focus + when it becomes the first page. */} onChange(page - 1)} + aria-disabled={page <= 1 || undefined} + onClick={() => { + if (page > 1) onChange(page - 1); + }} variant="secondary" > @@ -52,8 +57,10 @@ export function Pagination(props: PaginationProps) { )} = pageCount} - onClick={() => onChange(page + 1)} + aria-disabled={page >= pageCount || undefined} + onClick={() => { + if (page < pageCount) onChange(page + 1); + }} variant="secondary" > diff --git a/src/presentation/layouts/app-shell.tsx b/src/presentation/layouts/app-shell.tsx index bf59bebe..32d853f1 100644 --- a/src/presentation/layouts/app-shell.tsx +++ b/src/presentation/layouts/app-shell.tsx @@ -93,7 +93,6 @@ export function AppShell() { {integrationAvailable ? ( ) : null} diff --git a/src/presentation/providers/session-provider.tsx b/src/presentation/providers/session-provider.tsx index d74dd6de..e8201049 100644 --- a/src/presentation/providers/session-provider.tsx +++ b/src/presentation/providers/session-provider.tsx @@ -3,6 +3,7 @@ import { useContext, useEffect, useMemo, + useRef, useState, useSyncExternalStore, type ReactNode, @@ -23,6 +24,12 @@ export type SessionContextValue = Readonly<{ }>; const SessionContext = createContext(null); +/** + * 1112/05-03. Set while a session action runs, shared by every session + * button: the shell's and a status page's buttons stay enabled while pending, + * so a press on either is ignored until the action ends. + */ +const SessionActionGateContext = createContext<{ current: boolean } | null>(null); export function SessionProvider({ children, @@ -46,6 +53,7 @@ export function SessionProvider({ recoveryDeclined && ownerState === "recovery-pending" ? "unauthenticated" : ownerState; + const actionGate = useRef(false); const value = useMemo( () => Object.freeze({ @@ -62,7 +70,11 @@ export function SessionProvider({ ); return ( - {children} + + + {children} + + ); } @@ -77,8 +89,11 @@ export function useSession(): SessionContextValue { export type SessionAction = "sign-in" | "sign-out" | "recover"; /** - * One session action at a time, with the pending and failed state every - * session button shows. Sign-in returns to the current URL, hash included. + * One session action at a time across every session button, with the + * pending and failed state the pressed button shows. Sign-in returns to the + * current URL, hash included. A press while an action runs, on this button + * or another, is ignored: the buttons stay enabled (`pending`, not + * `disabled`) so keyboard focus survives the action. */ export function useSessionAction(): Readonly<{ pending: boolean; @@ -89,8 +104,12 @@ export function useSessionAction(): Readonly<{ const { beginSignIn, signOut, recover } = useSession(); const [pending, setPending] = useState(false); const [failed, setFailed] = useState(false); + const ownGate = useRef(false); + const running = useContext(SessionActionGateContext) ?? ownGate; async function execute(action: SessionAction): Promise { + if (running.current) return; + running.current = true; setPending(true); setFailed(false); try { @@ -100,6 +119,7 @@ export function useSessionAction(): Readonly<{ } catch { setFailed(true); } finally { + running.current = false; setPending(false); } } diff --git a/src/presentation/routes/app-router.tsx b/src/presentation/routes/app-router.tsx index d391d1a8..cb363ce3 100644 --- a/src/presentation/routes/app-router.tsx +++ b/src/presentation/routes/app-router.tsx @@ -288,12 +288,11 @@ function ProtectedRoute({ } primaryAction={{ kind: "button", - label: pending - ? message("common.processing") - : recovering - ? message("action.recoverSession") - : message("action.signIn"), - disabled: pending, + label: recovering + ? message("action.recoverSession") + : message("action.signIn"), + pending, + pendingLabel: message("common.processing"), onAction: () => run(recovering ? "recover" : "sign-in"), }} surface="authentication-required" diff --git a/src/presentation/styles/theme/primitives.css b/src/presentation/styles/theme/primitives.css index 191b9512..b95e4544 100644 --- a/src/presentation/styles/theme/primitives.css +++ b/src/presentation/styles/theme/primitives.css @@ -28,7 +28,8 @@ background: var(--button-primary-hover); } - .ui-button:disabled { + .ui-button:disabled, + .ui-button[aria-disabled="true"] { cursor: not-allowed; opacity: var(--opacity-disabled); } diff --git a/src/presentation/templates/page-templates.tsx b/src/presentation/templates/page-templates.tsx index bd697d9b..0488385d 100644 --- a/src/presentation/templates/page-templates.tsx +++ b/src/presentation/templates/page-templates.tsx @@ -21,6 +21,15 @@ export type PageActionDefinition = label: string; onAction(): void; disabled?: boolean; + /** + * Work started by this action is still running. The button stays + * focusable and is marked `aria-busy`; `disabled` would drop keyboard + * focus (`docs/manual/02-decision-tables.md` 6절). The action ignores + * repeated presses itself. + */ + pending?: boolean; + /** Label while `pending`; the button keeps `label` when omitted. */ + pendingLabel?: string; variant?: "primary" | "secondary" | "danger" | "ghost"; }> | (Readonly<{ @@ -286,6 +295,8 @@ function PageActionBar(props: Readonly<{ disabled={action.disabled} key={index} onClick={action.onAction} + pending={action.pending} + pendingLabel={action.pendingLabel} variant={action.variant} > {action.label} diff --git a/tests/component/pagination.test.tsx b/tests/component/pagination.test.tsx index e4ee9730..3e3804f4 100644 --- a/tests/component/pagination.test.tsx +++ b/tests/component/pagination.test.tsx @@ -1,6 +1,8 @@ // @vitest-environment jsdom import { fireEvent, render, screen, within } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { useState } from "react"; import { describe, expect, it, vi } from "vitest"; import { Pagination } from "../../src/presentation/design-system/index.ts"; @@ -116,3 +118,57 @@ describe("Pagination modified clicks", () => { expect(onChange).toHaveBeenCalledWith(2); }); }); + +// 1112/05-03. Reaching the first or last page must not take keyboard focus +// away from the edge button the user just pressed. +describe("Pagination edge buttons", () => { + function Controlled(props: Readonly<{ onChange(page: number): void }>) { + const [page, setPage] = useState(2); + return ( + { + props.onChange(next); + setPage(next); + }} + page={page} + pageCount={3} + pageLabel={(candidate) => `Page ${candidate}`} + previousLabel="Previous" + /> + ); + } + + it("keeps focus on previous when it reaches the first page and ignores further presses", async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + render(); + + const previous = screen.getByRole("button", { name: "Previous" }); + await user.click(previous); + expect(onChange).toHaveBeenCalledWith(1); + expect(previous).not.toBeDisabled(); + expect(previous).toHaveAttribute("aria-disabled", "true"); + expect(previous).toHaveFocus(); + + await user.keyboard("{Enter}"); + expect(onChange).toHaveBeenCalledOnce(); + }); + + it("marks next at the last page the same way", async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + render(); + + const next = screen.getByRole("button", { name: "Next" }); + await user.click(next); + expect(next).toHaveAttribute("aria-disabled", "true"); + expect(next).toHaveFocus(); + await user.click(next); + expect(onChange).toHaveBeenCalledOnce(); + expect(screen.getByRole("button", { name: "Previous" })).not.toHaveAttribute( + "aria-disabled", + ); + }); +}); diff --git a/tests/component/route-status-surfaces.test.tsx b/tests/component/route-status-surfaces.test.tsx index ef5108a1..6eed9c63 100644 --- a/tests/component/route-status-surfaces.test.tsx +++ b/tests/component/route-status-surfaces.test.tsx @@ -96,13 +96,41 @@ describe("route-level status surfaces", () => { expect(beginSignIn).toHaveBeenCalledWith( "/fixture/items#results", ); - expect(screen.getByRole("button", { name: "처리 중…" })).toBeDisabled(); + // 1112/05-03: pending, not disabled, so keyboard focus stays on it. + const busy = screen.getByRole("button", { name: "처리 중…" }); + expect(busy).not.toBeDisabled(); + expect(busy).toHaveAttribute("aria-busy", "true"); + expect(busy).toHaveFocus(); + await user.keyboard("{Enter}"); + expect(beginSignIn).toHaveBeenCalledOnce(); pending.reject(new Error("identity provider unavailable")); expect(await screen.findByRole("alert")).toHaveTextContent( "세션 작업을 완료하지 못했습니다.", ); - expect(screen.getByRole("button", { name: "로그인 시작" })).toBeEnabled(); + const retry = screen.getByRole("button", { name: "로그인 시작" }); + expect(retry).toBe(busy); + expect(retry).toBeEnabled(); + expect(retry).not.toHaveAttribute("aria-busy"); + expect(retry).toHaveFocus(); + }); + + // 1112/05-03. The shell's button and the page's button share one action. + it("ignores the other session button while one session action runs", async () => { + const user = userEvent.setup(); + const pending = deferred(); + const beginSignIn = vi.fn(() => pending.promise); + renderApp("/fixture/items", { session: sessionWithSignIn(beginSignIn) }); + + const shellButton = await within(screen.getByRole("banner")).findByRole("button", { + name: "로그인", + }); + await user.click(await screen.findByRole("button", { name: "로그인 시작" })); + await user.click(shellButton); + expect(beginSignIn).toHaveBeenCalledOnce(); + + pending.reject(new Error("identity provider unavailable")); + expect(await screen.findByRole("button", { name: "로그인 시작" })).not.toHaveAttribute("aria-busy"); }); it("returns to the current URL with its hash from the shell session action", async () => { @@ -116,12 +144,17 @@ describe("route-level status surfaces", () => { await within(banner).findByRole("button", { name: "로그인" }), ); expect(beginSignIn).toHaveBeenCalledWith("/fixture/first#buttons"); - expect(within(banner).getByRole("button", { name: "처리 중…" })).toBeDisabled(); + const busy = within(banner).getByRole("button", { name: "처리 중…" }); + expect(busy).not.toBeDisabled(); + expect(busy).toHaveAttribute("aria-busy", "true"); + await user.click(busy); + expect(beginSignIn).toHaveBeenCalledOnce(); pending.reject(new Error("identity provider unavailable")); expect(await within(banner).findByRole("alert")).toHaveTextContent( "세션 작업을 완료하지 못했습니다.", ); + expect(within(banner).getByRole("button", { name: "로그인" })).toHaveFocus(); }); it("answers an unknown address with a home link that keeps the basename", async () => {