diff --git a/client/src/components/Collections/AddPlaceToCollectionModal.tsx b/client/src/components/Collections/AddPlaceToCollectionModal.tsx index 39d3c45a6..bbceb68e0 100644 --- a/client/src/components/Collections/AddPlaceToCollectionModal.tsx +++ b/client/src/components/Collections/AddPlaceToCollectionModal.tsx @@ -21,7 +21,7 @@ import { useAddPlaceToCollection } from './useAddPlaceToCollection' * scrolled, so it looked like the button had vanished (#1921). The negative * margins let its background cover the body's padding while it is stuck. */ -const STICKY_SEARCH = 'sticky top-0 z-20 -mx-6 -mt-5 bg-surface-card px-6 pb-1 pt-5' +const STICKY_SEARCH = 'sticky top-0 z-20 -mx-6 -mt-5 bg-surface-card px-6 pb-4 pt-5' /** A chip of the category row; the picked one wears the category's own colour. */ const CHIP = 'inline-flex items-center gap-1.5 rounded-full border px-3 py-1 font-semibold' const CHIP_OFF = 'border-edge-faint bg-surface-card text-content-muted hover:text-content' @@ -97,29 +97,34 @@ export default function AddPlaceToCollectionModal({ isOpen, collectionId, collec {/* Search: picking a result fills the name and the location below. */}
-
-
-
- +
{(results.length > 0 || noResults) && (
{ + const realMatchMedia = window.matchMedia; + afterEach(() => { + window.matchMedia = realMatchMedia; + document.body.innerHTML = ''; + }); + + const desktop = () => { + Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: 1400 }); + window.matchMedia = vi + .fn() + .mockImplementation((q: string) => ({ + matches: q.includes('pointer: fine'), + media: q, + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + })) as unknown as typeof window.matchMedia; + }; + + it('FE-DIALOGFOCUS-001: on a desktop the first text field takes the focus', () => { + desktop(); + const panel = panelWith(''); + focusDialog(panel); + expect(document.activeElement?.id).toBe('a'); + }); + + it('FE-DIALOGFOCUS-002: a field marked as the start wins over the first one', () => { + desktop(); + const panel = panelWith(''); + focusDialog(panel); + expect(document.activeElement?.id).toBe('b'); + }); + + it('FE-DIALOGFOCUS-003: a focus already inside stays where it is', () => { + desktop(); + const panel = panelWith(''); + (panel.querySelector('#a') as HTMLElement).focus(); + focusDialog(panel); + expect(document.activeElement?.id).toBe('a'); + }); +}); diff --git a/client/src/components/shared/dialogFocus.ts b/client/src/components/shared/dialogFocus.ts index af72ba56a..43c91ebad 100644 --- a/client/src/components/shared/dialogFocus.ts +++ b/client/src/components/shared/dialogFocus.ts @@ -50,7 +50,10 @@ function typingDevice(): boolean { */ export function focusDialog(panel: HTMLElement): void { if (panel.contains(document.activeElement)) return - const field = typingDevice() ? firstTextField(panel) : null + // A dialog that starts somewhere other than its first field marks that field, + // as the add-place dialog does with its search below the name in its head. + const marked = panel.querySelector('[data-initial-focus]') + const field = typingDevice() ? (marked ?? firstTextField(panel)) : null ;(field ?? panel).focus({ preventScroll: true }) }