From 1ac39af569aeb2d69fe0da4abf111e54e7676592 Mon Sep 17 00:00:00 2001 From: YektaY Date: Sat, 7 Nov 2026 13:49:26 -0800 Subject: [PATCH 1/2] Keep keyboard focus in the config store's delete confirmation The delete confirmation is a second ComposedModal stacked over the store. When it opened and focused its Cancel button, the store's focus wrap saw focus leave for an element after it and pulled it back to the store's Close button. Focus stayed behind the dialog with the Close tooltip open, Tab cycled through the store, and the first Escape only closed the tooltip. List the confirmation in the store's selectorsFloatingMenus so the store lets focus move into it; the confirmation's own wrap keeps Tab inside it. When the confirmation closes, return focus to the row's Delete button, or to the store body after a delete removes that row, so Tab stays in the store instead of escaping to the page behind it. --- src/components/ConfigStoreModal.test.tsx | 64 +++++++++++++++++++++++- src/components/ConfigStoreModal.tsx | 33 +++++++++++- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/components/ConfigStoreModal.test.tsx b/src/components/ConfigStoreModal.test.tsx index 5831edf..46d0c8c 100644 --- a/src/components/ConfigStoreModal.test.tsx +++ b/src/components/ConfigStoreModal.test.tsx @@ -1,6 +1,6 @@ // @vitest-environment jsdom import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; import { ConfigStoreModal } from './ConfigStoreModal'; import type { ConfigDTO } from '../api/types'; import type { Plot } from '../types'; @@ -78,6 +78,13 @@ describe('ConfigStoreModal', () => { disconnect() {} } ); + // jsdom has no scrollIntoView. Carbon's modal calls it when focus moves to + // an element inside a scrolling ModalBody. + Object.defineProperty(Element.prototype, 'scrollIntoView', { + configurable: true, + writable: true, + value: vi.fn(), + }); vi.mocked(listConfigs).mockResolvedValue({ results: [], continuationToken: null, @@ -89,6 +96,7 @@ describe('ConfigStoreModal', () => { cleanup(); vi.unstubAllGlobals(); vi.restoreAllMocks(); + delete (Element.prototype as { scrollIntoView?: unknown }).scrollIntoView; }); it('cannot be dismissed while a save is in progress', async () => { @@ -167,6 +175,60 @@ describe('ConfigStoreModal', () => { await waitFor(() => expect(deleteConfig).toHaveBeenCalledWith('test-config')); await screen.findByText('No saved configs yet.'); + // The row's button went with the row, so focus falls back to the store body. + expect(document.activeElement).toBe(screen.getByRole('region', { name: 'Config store' })); + }); + + it('moves focus into the delete confirmation and back to its trigger on Escape', async () => { + // Carbon's focus wrap only targets elements with an offsetParent, which + // jsdom never sets (no layout). Without one the store's wrap can't move + // focus at all and this test couldn't see it pull focus back. + const offsetParent = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetParent')!; + Object.defineProperty(HTMLElement.prototype, 'offsetParent', { + configurable: true, + get(this: HTMLElement) { + return this.parentElement; + }, + }); + const focusedOutside: Element[] = []; + const recordFocus = (e: FocusEvent) => { + if (!(e.target as Element).closest('.cds--modal--danger')) { + focusedOutside.push(e.target as Element); + } + }; + try { + vi.mocked(listConfigs).mockResolvedValue({ + results: [listedConfig], + continuationToken: null, + totalCount: 1, + }); + renderModal(); + await screen.findByText('test-config'); + + // A keyboard user tabs to the row's Delete button and presses Enter. + const trigger = screen.getAllByRole('button', { name: 'Delete' })[0]; + trigger.focus(); + document.addEventListener('focusin', recordFocus); + fireEvent.click(trigger); + + const confirmation = document.querySelector('.cds--modal--danger')!; + const cancel = within(confirmation).getByRole('button', { name: 'Cancel' }); + await waitFor(() => expect(document.activeElement).toBe(cancel)); + // jsdom lets the outer focus() win, so also check the store never took + // focus on the way (in a browser its pull-back wins and focus stays there). + expect(focusedOutside).toEqual([]); + document.removeEventListener('focusin', recordFocus); + + // One Escape closes the confirmation and focus goes back to the trigger. + fireEvent.keyDown(cancel, { key: 'Escape' }); + await waitFor(() => + expect(document.querySelectorAll('.cds--modal.is-visible')).toHaveLength(1) + ); + expect(document.activeElement).toBe(trigger); + } finally { + document.removeEventListener('focusin', recordFocus); + Object.defineProperty(HTMLElement.prototype, 'offsetParent', offsetParent); + } }); it('aborts superseded loads and only applies the latest response', async () => { diff --git a/src/components/ConfigStoreModal.tsx b/src/components/ConfigStoreModal.tsx index 209fa4c..09e4030 100644 --- a/src/components/ConfigStoreModal.tsx +++ b/src/components/ConfigStoreModal.tsx @@ -49,6 +49,13 @@ interface ConfigStoreModalProps { // silently hidden; real paging is slaclab/react-trace#80. const PAGE_SIZE = 200; +// Marks the delete confirmation, which opens as a second modal over the store. +// The store's Carbon focus wrap treats focus leaving for an element outside it +// as Tab-past-the-end and pulls it back to its own first button. Listing this +// class in the store's `selectorsFloatingMenus` lets focus move into the +// confirmation; the confirmation's own wrap then keeps it there. +const DELETE_CONFIRM_CLASS = 'config-store-delete-confirm'; + function shareUrl(slug: string): string { const url = new URL(window.location.href); url.search = ''; @@ -92,6 +99,9 @@ export function ConfigStoreModal({ // Slug currently undergoing a row action (delete/load), to disable its buttons. const [busySlug, setBusySlug] = useState(null); const [deleteTarget, setDeleteTarget] = useState(null); + // The row button that opened the delete confirmation, to refocus on close. + const deleteTriggerRef = useRef(null); + const storeBodyRef = useRef(null); const loadAbortRef = useRef(null); // Monotonic refresh id. A Save/Delete fires its own refresh while the open // refresh may still be in flight; without this, the slower (older) response @@ -135,6 +145,22 @@ export function ConfigStoreModal({ [] ); + // Return focus to the store when the delete confirmation closes. Otherwise it + // stays on the hidden confirmation, and the next Tab leaves the store for the + // page behind it. After a delete the row's button is gone (the list reloads), + // so fall back to the store's scrollable body, which Carbon makes focusable. + // Runs as the cleanup, i.e. after the commit that closes the confirmation, so + // its focus wrap is already off and doesn't pull focus back. + const deleteConfirmOpen = deleteTarget !== null; + useEffect(() => { + if (!deleteConfirmOpen) return; + const trigger = deleteTriggerRef.current; + const storeBody = storeBodyRef.current; + return () => { + (trigger?.isConnected ? trigger : storeBody)?.focus(); + }; + }, [deleteConfirmOpen]); + const handleSave = async () => { setSaving(true); setError(null); @@ -249,9 +275,10 @@ export function ConfigStoreModal({ onClose(); }} size="lg" + selectorsFloatingMenus={[`.${DELETE_CONFIRM_CLASS}`]} > - +
{error && !deleteTarget && ( { + onClick={(e: React.MouseEvent) => { + deleteTriggerRef.current = e.currentTarget; setError(null); setDeleteTarget(c); }} @@ -396,6 +424,7 @@ export function ConfigStoreModal({ }} size="sm" danger + className={DELETE_CONFIRM_CLASS} > From b2766c1c20d717aafefdcae08d27a2a46dc0814a Mon Sep 17 00:00:00 2001 From: YektaY Date: Thu, 12 Nov 2026 13:08:37 -0800 Subject: [PATCH 2/2] Skip a disabled delete trigger when returning focus Address review feedback on the delete-confirmation focus fix: - When the confirmation closes, fall back to the store body if the row's Delete button is still connected but disabled, not only when it's gone. focus() on a disabled button does nothing, so focus would stay on the hidden confirmation if the list reload ever lands in a later render. - Name the store's scrolling region "Config store contents" so it doesn't repeat the dialog title for screen reader users. --- src/components/ConfigStoreModal.test.tsx | 27 +++++++++++++++++++++++- src/components/ConfigStoreModal.tsx | 10 +++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/components/ConfigStoreModal.test.tsx b/src/components/ConfigStoreModal.test.tsx index 46d0c8c..bed095c 100644 --- a/src/components/ConfigStoreModal.test.tsx +++ b/src/components/ConfigStoreModal.test.tsx @@ -176,7 +176,32 @@ describe('ConfigStoreModal', () => { await waitFor(() => expect(deleteConfig).toHaveBeenCalledWith('test-config')); await screen.findByText('No saved configs yet.'); // The row's button went with the row, so focus falls back to the store body. - expect(document.activeElement).toBe(screen.getByRole('region', { name: 'Config store' })); + expect(document.activeElement).toBe( + screen.getByRole('region', { name: 'Config store contents' }) + ); + }); + + it('falls back to the store body when the delete trigger is disabled on close', async () => { + vi.mocked(listConfigs).mockResolvedValue({ + results: [listedConfig], + continuationToken: null, + totalCount: 1, + }); + renderModal(); + await screen.findByText('test-config'); + + const trigger = screen.getAllByRole('button', { name: 'Delete' })[0]; + fireEvent.click(trigger); + // Stands in for a row still mid-delete (busySlug) when the dialog closes. + (trigger as HTMLButtonElement).disabled = true; + fireEvent.click(screen.getByRole('button', { name: 'Cancel' })); + + await waitFor(() => + expect(document.querySelectorAll('.cds--modal.is-visible')).toHaveLength(1) + ); + expect(document.activeElement).toBe( + screen.getByRole('region', { name: 'Config store contents' }) + ); }); it('moves focus into the delete confirmation and back to its trigger on Escape', async () => { diff --git a/src/components/ConfigStoreModal.tsx b/src/components/ConfigStoreModal.tsx index 09e4030..ea58921 100644 --- a/src/components/ConfigStoreModal.tsx +++ b/src/components/ConfigStoreModal.tsx @@ -100,7 +100,7 @@ export function ConfigStoreModal({ const [busySlug, setBusySlug] = useState(null); const [deleteTarget, setDeleteTarget] = useState(null); // The row button that opened the delete confirmation, to refocus on close. - const deleteTriggerRef = useRef(null); + const deleteTriggerRef = useRef(null); const storeBodyRef = useRef(null); const loadAbortRef = useRef(null); // Monotonic refresh id. A Save/Delete fires its own refresh while the open @@ -148,7 +148,8 @@ export function ConfigStoreModal({ // Return focus to the store when the delete confirmation closes. Otherwise it // stays on the hidden confirmation, and the next Tab leaves the store for the // page behind it. After a delete the row's button is gone (the list reloads), - // so fall back to the store's scrollable body, which Carbon makes focusable. + // or still disabled if that reload lands in a later render, so fall back to + // the store's scrollable body, which Carbon makes focusable. // Runs as the cleanup, i.e. after the commit that closes the confirmation, so // its focus wrap is already off and doesn't pull focus back. const deleteConfirmOpen = deleteTarget !== null; @@ -157,7 +158,8 @@ export function ConfigStoreModal({ const trigger = deleteTriggerRef.current; const storeBody = storeBodyRef.current; return () => { - (trigger?.isConnected ? trigger : storeBody)?.focus(); + const triggerUsable = trigger?.isConnected && !trigger.disabled; + (triggerUsable ? trigger : storeBody)?.focus(); }; }, [deleteConfirmOpen]); @@ -278,7 +280,7 @@ export function ConfigStoreModal({ selectorsFloatingMenus={[`.${DELETE_CONFIRM_CLASS}`]} > - +
{error && !deleteTarget && (