Skip to content

Commit 4c97c49

Browse files
authored
fix(component): dropdown items click not firing inside modal (#1779)
1 parent beeb907 commit 4c97c49

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

.changeset/heavy-llamas-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bigcommerce/big-design": patch
3+
---
4+
5+
fix(component): dropdown items click not firing inside modal

packages/big-design/src/components/Dropdown/spec.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React from 'react';
66
import 'jest-styled-components';
77

88
import { Button } from '../Button';
9+
import { Modal } from '../Modal';
910

1011
import { Dropdown } from './';
1112

@@ -684,3 +685,20 @@ test("dropdown toggle doesn't trigger onItemClick", async () => {
684685

685686
expect(onItemClick).not.toHaveBeenCalled();
686687
});
688+
689+
test('dropdown onItemClick triggers inside a modal', async () => {
690+
onItemClick.mockClear();
691+
692+
render(<Modal isOpen={true}>{DropdownMock}</Modal>);
693+
694+
const toggle = await screen.findByRole('button', { name: 'Button' });
695+
696+
await userEvent.click(toggle);
697+
698+
const options = await screen.findAllByRole('option');
699+
700+
await userEvent.click(options[1]);
701+
702+
expect(onItemClick).toHaveBeenCalledTimes(1);
703+
expect(onItemClick).toHaveBeenCalledWith({ content: 'Option 2', onItemClick });
704+
});

packages/big-design/src/components/Modal/Modal.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,23 @@ const InternalModal: React.FC<ModalProps> = ({
9696
// Setup focus-trap
9797
useEffect(() => {
9898
if (modalRef && internalTrap.current === null) {
99-
internalTrap.current = createFocusTrap(modalRef, { fallbackFocus: modalRef });
99+
internalTrap.current = createFocusTrap(modalRef, {
100+
allowOutsideClick: (event) => {
101+
const target = event.target;
102+
103+
// Allow clicks on portaled menu/listbox elements (e.g., Dropdown, Select)
104+
/* istanbul ignore next */
105+
if (target instanceof Element) {
106+
return (
107+
target.closest('[role="menu"]') !== null ||
108+
target.closest('[role="listbox"]') !== null
109+
);
110+
}
111+
112+
return false;
113+
},
114+
fallbackFocus: modalRef,
115+
});
100116
internalTrap.current.activate();
101117
}
102118

0 commit comments

Comments
 (0)