diff --git a/apps/app/src/components/canvas/AccessLabels.tsx b/apps/app/src/components/canvas/AccessLabels.tsx index 8d019928..092fc188 100644 --- a/apps/app/src/components/canvas/AccessLabels.tsx +++ b/apps/app/src/components/canvas/AccessLabels.tsx @@ -115,8 +115,10 @@ function MoreViews({ box, areaHeight, items, onActivate }: MoreProps) { }, [open]) const onBlur = (event: FocusEvent) => { + // A null target is no exit: Safari gives a tapped button no focus. A press outside + // is closed by the document listener. const next = event.relatedTarget - if (!(next instanceof Node && root.current?.contains(next))) setOpen(false) + if (next instanceof Node && !root.current?.contains(next)) setOpen(false) } const onKeyDown = (event: KeyboardEvent) => { diff --git a/apps/app/tests/unit/components.AccessLabels.test.tsx b/apps/app/tests/unit/components.AccessLabels.test.tsx index d7ff946a..ab6aeabe 100644 --- a/apps/app/tests/unit/components.AccessLabels.test.tsx +++ b/apps/app/tests/unit/components.AccessLabels.test.tsx @@ -1,6 +1,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { useRef } from 'react' -import { render, screen, within } from '@testing-library/react' +import { fireEvent, render, screen, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import ModalDialog from '@/components/ui/ModalDialog' import AccessLabels from '@/components/canvas/AccessLabels' @@ -251,6 +251,30 @@ describe('AccessLabels, « Autres vues »', () => { expect(screen.queryAllByRole('listitem')).toHaveLength(0) }) + it('still activates an item after a blur without a related target', async () => { + overflowing() + const onActivate = vi.fn() + render() + const more = screen.getByRole('button', { name: 'Autres vues (3)' }) + await userEvent.click(more) + fireEvent.blur(more, { relatedTarget: null }) + await userEvent.click(within(screen.getAllByRole('listitem')[1]).getByRole('button')) + expect(onActivate).toHaveBeenCalledWith('a2', 'view-a2') + }) + + it('keeps the list open while the focus moves inside it', async () => { + overflowing() + render() + const more = screen.getByRole('button', { name: 'Autres vues (3)' }) + await userEvent.click(more) + await userEvent.tab() + await userEvent.tab() + await userEvent.tab({ shift: true }) + await userEvent.tab({ shift: true }) + expect(more).toHaveFocus() + expect(screen.getAllByRole('listitem')).toHaveLength(3) + }) + it('closes on a press outside', async () => { overflowing() render()