From 6d89db1307ce2735c30268bd8128b9044f0198ab Mon Sep 17 00:00:00 2001 From: Andrew Musgrave Date: Mon, 7 Oct 2019 17:27:05 -0400 Subject: [PATCH] Fixed Popover focusing incorrect element when closed --- src/components/Popover/Popover.tsx | 5 +- src/components/Popover/tests/Popover.test.tsx | 41 +++++++ src/utilities/focus.ts | 34 ++++++ src/utilities/tests/focus.test.ts | 106 ++++++++++++++++-- 4 files changed, 175 insertions(+), 11 deletions(-) diff --git a/src/components/Popover/Popover.tsx b/src/components/Popover/Popover.tsx index 2d06a55dcb4..1d11951bc28 100644 --- a/src/components/Popover/Popover.tsx +++ b/src/components/Popover/Popover.tsx @@ -4,6 +4,7 @@ import { focusFirstFocusableNode, findFirstFocusableNode, } from '@shopify/javascript-utilities/focus'; +import {focusNextFocusableNode} from '../../utilities/focus'; import {PreferredPosition, PreferredAlignment} from '../PositionedOverlay'; import {Portal} from '../Portal'; @@ -135,7 +136,9 @@ export class Popover extends React.PureComponent { source === CloseSource.FocusOut || source === CloseSource.EscapeKeypress ) { - focusFirstFocusableNode(this.activatorContainer, false); + if (!focusNextFocusableNode(this.activatorContainer)) { + focusFirstFocusableNode(this.activatorContainer, false); + } } }; diff --git a/src/components/Popover/tests/Popover.test.tsx b/src/components/Popover/tests/Popover.test.tsx index 980036fdf60..93c5ef46208 100644 --- a/src/components/Popover/tests/Popover.test.tsx +++ b/src/components/Popover/tests/Popover.test.tsx @@ -1,6 +1,8 @@ import React from 'react'; import {mountWithAppProvider, findByTestID} from 'test-utilities/legacy'; +import {mountWithApp} from 'test-utilities'; import {Popover} from '../Popover'; +import {PopoverOverlay} from '../components'; describe('', () => { const spy = jest.fn(); @@ -182,4 +184,43 @@ describe('', () => { expect(onCloseSpy).not.toHaveBeenCalled(); }); + + it('focuses the next available element when the popover is closed', () => { + const id = 'focus-target'; + function PopoverTest() { + return ( + + } onClose={noop} /> +