From 378c9018789cf52625996213c84aad229f4d16c8 Mon Sep 17 00:00:00 2001 From: Ryan Min <73128364+kcrmin@users.noreply.github.com> Date: Mon, 26 Oct 2026 22:21:53 +0900 Subject: [PATCH] fix(competition): keep dialog focus on re-render Room detail passes an inline onClose, so every parent render re-ran the open effect and moved focus back to the close button. --- src/CompetitionApiWorkspace.test.tsx | 31 +++++++++++++++++++++- src/components/CompetitionApiWorkspace.tsx | 9 +++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/CompetitionApiWorkspace.test.tsx b/src/CompetitionApiWorkspace.test.tsx index 2b8005c..9649d43 100644 --- a/src/CompetitionApiWorkspace.test.tsx +++ b/src/CompetitionApiWorkspace.test.tsx @@ -3,7 +3,7 @@ import userEvent from '@testing-library/user-event'; import { describe, expect, test, vi } from 'vitest'; import { CompetitionApiWorkspace, joinFailureMessage } from './components/CompetitionApiWorkspace'; import { CompetitionApiError } from './api/competitionRooms'; -import type { CompetitionRoomsClient, PublicRoom, RoomInputCatalog } from './api/competitionRooms'; +import type { CompetitionRoomsClient, LeaderboardPage, PublicRoom, RoomInputCatalog } from './api/competitionRooms'; import { LanguageProvider } from './lib/i18n'; import { RoomsView } from './views/OperationsViews'; @@ -90,6 +90,35 @@ describe('real competition room workspace', () => { expect(launcher).toHaveFocus(); }); + test('keeps focus in the join dialog while the room detail re-renders', async () => { + const user = userEvent.setup(); + let resolveMyBots!: (page: LeaderboardPage) => void; + const myBots = vi.fn(() => new Promise((resolve) => { resolveMyBots = resolve; })); + render(); + await user.click(await screen.findByRole('listitem', { name: '실전 API 대회 열기' })); + const detail = await screen.findByRole('region', { name: '실전 API 대회 상세' }); + await within(detail).findByText('Bot 3F9A'); + await user.click(within(detail).getByRole('button', { name: '이 대회 참가하기' })); + const join = screen.getByRole('dialog', { name: '대회 참가' }); + await within(join).findByRole('option', { name: /Momentum · 편집 7/ }); + + const alias = within(join).getByLabelText('익명 봇 별칭'); + await user.type(alias, 'Bot'); + expect(alias).toHaveFocus(); + + // The owned leaderboard arrives while the dialog is open, so the room + // detail re-renders and hands the dialog a fresh inline onClose. + await act(async () => { + resolveMyBots({ snapshotId: 's1', snapshotStatus: 'FINAL', cutoffAt: room.participationClosesAt, nextCursor: null, hasMore: false, items: [] }); + }); + await within(detail).findByText('표시할 봇 성과가 없습니다.'); + + expect(alias).toHaveFocus(); + await user.keyboard(' 7ABC'); + expect(alias).toHaveValue('Bot 7ABC'); + expect(within(join).getByRole('button', { name: '대회 참가 닫기' })).not.toHaveFocus(); + }); + test('shows loading, schedule, anonymous ranking, owned comparison and post-end choice', async () => { const api = client(); render(); diff --git a/src/components/CompetitionApiWorkspace.tsx b/src/components/CompetitionApiWorkspace.tsx index 2f61822..4ffac05 100644 --- a/src/components/CompetitionApiWorkspace.tsx +++ b/src/components/CompetitionApiWorkspace.tsx @@ -271,6 +271,11 @@ function PostChoice({ client, roomId, item, initial }: { client: CompetitionRoom function DialogShell({ title, onClose, children }: { title: string; onClose: () => void; children: React.ReactNode }) { const dialogRef = useRef(null); const closeButtonRef = useRef(null); + // Callers pass inline handlers, so onClose changes on every parent render. + // Reading it through a ref keeps the open-time effect (initial focus, + // scroll lock, focus restore) from re-running and stealing focus. + const onCloseRef = useRef(onClose); + useEffect(() => { onCloseRef.current = onClose; }, [onClose]); useEffect(() => { const previouslyFocused = document.activeElement as HTMLElement | null; @@ -278,7 +283,7 @@ function DialogShell({ title, onClose, children }: { title: string; onClose: () const closeOnEscape = (event: KeyboardEvent) => { if (event.key === 'Escape') { event.preventDefault(); - onClose(); + onCloseRef.current(); return; } if (event.key !== 'Tab' || !dialogRef.current) return; @@ -305,7 +310,7 @@ function DialogShell({ title, onClose, children }: { title: string; onClose: () document.removeEventListener('keydown', closeOnEscape); previouslyFocused?.focus(); }; - }, [onClose]); + }, []); return
{ if (event.target === event.currentTarget) onClose(); }}>