Skip to content

Commit 3fe2f5f

Browse files
authored
Add optional disabling of drawer close on background overlay click (#1281)
Adds a prop to the Drawer to disable the default onClose from being called when the user clicks outside the drawer itself
1 parent e362cc0 commit 3fe2f5f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Drawer/Drawer.test.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,45 @@ describe('Drawer', () => {
215215
});
216216
});
217217

218+
it('calls onRequestClose when closeOnOverlayClick is not defined', async () => {
219+
const onRequestClose = jest.fn();
220+
221+
const { container } = render(
222+
<SetupDrawerWithChildren
223+
visible
224+
onRequestClose={onRequestClose}
225+
/>,
226+
);
227+
228+
const [overlay] = Array.from(container.getElementsByClassName('DrawerBackgroundOverlay'));
229+
230+
userEvent.click(overlay);
231+
232+
await waitFor(() => {
233+
expect(onRequestClose).toHaveBeenCalled();
234+
});
235+
});
236+
237+
it('does not call onRequestClose when closeOnOverlayClick is set to false', async () => {
238+
const onRequestClose = jest.fn();
239+
240+
const { container } = render(
241+
<SetupDrawerWithChildren
242+
closeOnOverlayClick={false}
243+
visible
244+
onRequestClose={onRequestClose}
245+
/>,
246+
);
247+
248+
const [overlay] = Array.from(container.getElementsByClassName('DrawerBackgroundOverlay'));
249+
250+
userEvent.click(overlay);
251+
252+
await waitFor(() => {
253+
expect(onRequestClose).not.toHaveBeenCalled();
254+
});
255+
});
256+
218257
it('body tag has Drawer--open', () => {
219258
const { container } = render(<SetupDrawerWithChildren visible />);
220259
const body = container.closest('body');

src/Drawer/Drawer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type DrawerProps = {
2828
behindNav?: boolean;
2929
children?: React.ReactNode;
3030
className?: string;
31+
closeOnOverlayClick?: boolean;
3132
defaultExpanded?: boolean;
3233
expandable?: boolean;
3334
hasBackgroundOverlay?: boolean;
@@ -42,6 +43,7 @@ function Drawer({
4243
children,
4344
className = '',
4445
defaultExpanded = false,
46+
closeOnOverlayClick = true,
4547
expandable = false,
4648
hasBackgroundOverlay = true,
4749
visible,
@@ -122,7 +124,7 @@ function Drawer({
122124
})
123125
}
124126
role="presentation"
125-
onClick={onRequestClose}
127+
onClick={closeOnOverlayClick ? onRequestClose : undefined}
126128
onKeyDown={handleEscKeyPress}
127129
/>
128130
)

0 commit comments

Comments
 (0)