Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-plums-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': minor
---

Exposed a `close` function on popovers imperative handle
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('<Combobox />', () => {
it('calls Popover.forceUpdatePosition() when onOptionSelected is triggered and allowMultiple is true and there are children', () => {
const mockForceUpdatePosition = jest.fn();
mockUseImperativeHandle.mockImplementation(
(ref: {current: PopoverPublicAPI}) => {
(ref: {current: Partial<PopoverPublicAPI>}) => {
ref.current = {
forceUpdatePosition: mockForceUpdatePosition,
};
Expand Down
70 changes: 40 additions & 30 deletions polaris-react/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ export interface PopoverProps {
captureOverscroll?: boolean;
}

type CloseTarget = 'activator' | 'next-node';
export interface PopoverPublicAPI {
forceUpdatePosition(): void;
close(target?: CloseTarget): void;
}

// TypeScript can't generate types that correctly infer the typing of
Expand Down Expand Up @@ -120,36 +122,6 @@ const PopoverComponent = forwardRef<PopoverPublicAPI, PopoverProps>(
overlayRef.current?.forceUpdatePosition();
}

useImperativeHandle(ref, () => {
return {
forceUpdatePosition,
};
});

const setAccessibilityAttributes = useCallback(() => {
if (activatorContainer.current == null) {
return;
}

const firstFocusable = findFirstFocusableNodeIncludingDisabled(
activatorContainer.current,
);
const focusableActivator: HTMLElement & {
disabled?: boolean;
} = firstFocusable || activatorContainer.current;

const activatorDisabled =
'disabled' in focusableActivator &&
Boolean(focusableActivator.disabled);

setActivatorAttributes(focusableActivator, {
id,
active,
ariaHaspopup,
activatorDisabled,
});
}, [id, active, ariaHaspopup]);

const handleClose = (source: PopoverCloseSource) => {
onClose(source);
if (activatorContainer.current == null || preventFocusOnClose) {
Expand Down Expand Up @@ -181,6 +153,44 @@ const PopoverComponent = forwardRef<PopoverPublicAPI, PopoverProps>(
}
};

useImperativeHandle(ref, () => {
return {
forceUpdatePosition,
close: (target = 'activator') => {
const source =
target === 'activator'
? PopoverCloseSource.EscapeKeypress
: PopoverCloseSource.FocusOut;

handleClose(source);
},
};
});

const setAccessibilityAttributes = useCallback(() => {
if (activatorContainer.current == null) {
return;
}

const firstFocusable = findFirstFocusableNodeIncludingDisabled(
activatorContainer.current,
);
const focusableActivator: HTMLElement & {
disabled?: boolean;
} = firstFocusable || activatorContainer.current;

const activatorDisabled =
'disabled' in focusableActivator &&
Boolean(focusableActivator.disabled);

setActivatorAttributes(focusableActivator, {
id,
active,
ariaHaspopup,
activatorDisabled,
});
}, [id, active, ariaHaspopup]);

useEffect(() => {
if (!activatorNode && activatorContainer.current) {
setActivatorNode(
Expand Down
64 changes: 63 additions & 1 deletion polaris-react/src/components/Popover/tests/Popover.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useCallback, useRef, useState} from 'react';
import {mountWithApp} from 'tests/utilities';
import {act} from 'react-dom/test-utils';

import {Portal} from '../../Portal';
import {PositionedOverlay} from '../../PositionedOverlay';
Expand Down Expand Up @@ -396,14 +397,75 @@ describe('<Popover />', () => {

mountWithApp(<Test />);

expect(popoverRef).toStrictEqual({
expect(popoverRef).toMatchObject({
current: {
forceUpdatePosition: expect.anything(),
},
});
});
});

describe('close', () => {
it('exposes a function that closes the popover & focuses the activator by default', () => {
const activatorId = 'focus-target';
let popoverRef: React.RefObject<PopoverPublicAPI> | null = null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is what's used above to test the other function in the imperative handle


function Test() {
popoverRef = useRef(null);

return (
<Popover
ref={popoverRef}
active
activator={<button id={activatorId} />}
onClose={noop}
/>
);
}

const popover = mountWithApp(<Test />);

act(() => {
popoverRef?.current?.close();
});

const focusTarget = popover.find('button', {id: activatorId})!.domNode;

expect(document.activeElement).toBe(focusTarget);
});

it('exposes a function that closes the popover & focuses the next node when the next-node option is used', () => {
const nextFocusedId = 'focus-target2';
let popoverRef: React.RefObject<PopoverPublicAPI> | null = null;

function Test() {
popoverRef = useRef(null);

return (
<>
<Popover
ref={popoverRef}
active
activator={<button />}
onClose={noop}
/>
<button id={nextFocusedId} />
</>
);
}

const popover = mountWithApp(<Test />);

act(() => {
popoverRef?.current?.close('next-node');
});

const focusTarget = popover.find('button', {id: nextFocusedId})!.domNode;

expect(document.activeElement).toBe(focusTarget);
});
});

describe('captureOverscroll', () => {
const TestActivator = <button>Activator</button>;

Expand Down