Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional disabling of drawer close on background overlay click #1281

Merged
merged 1 commit into from
Aug 19, 2024
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
39 changes: 39 additions & 0 deletions src/Drawer/Drawer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,45 @@ describe('Drawer', () => {
});
});

it('calls onRequestClose when closeOnOverlayClick is not defined', async () => {
const onRequestClose = jest.fn();

const { container } = render(
<SetupDrawerWithChildren
visible
onRequestClose={onRequestClose}
/>,
);

const [overlay] = Array.from(container.getElementsByClassName('DrawerBackgroundOverlay'));

userEvent.click(overlay);

await waitFor(() => {
expect(onRequestClose).toHaveBeenCalled();
});
});

it('does not call onRequestClose when closeOnOverlayClick is set to false', async () => {
const onRequestClose = jest.fn();

const { container } = render(
<SetupDrawerWithChildren
closeOnOverlayClick={false}
visible
onRequestClose={onRequestClose}
/>,
);

const [overlay] = Array.from(container.getElementsByClassName('DrawerBackgroundOverlay'));

userEvent.click(overlay);

await waitFor(() => {
expect(onRequestClose).not.toHaveBeenCalled();
});
});

it('body tag has Drawer--open', () => {
const { container } = render(<SetupDrawerWithChildren visible />);
const body = container.closest('body');
Expand Down
4 changes: 3 additions & 1 deletion src/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type DrawerProps = {
behindNav?: boolean;
children?: React.ReactNode;
className?: string;
closeOnOverlayClick?: boolean;
defaultExpanded?: boolean;
expandable?: boolean;
hasBackgroundOverlay?: boolean;
Expand All @@ -42,6 +43,7 @@ function Drawer({
children,
className = '',
defaultExpanded = false,
closeOnOverlayClick = true,
expandable = false,
hasBackgroundOverlay = true,
visible,
Expand Down Expand Up @@ -122,7 +124,7 @@ function Drawer({
})
}
role="presentation"
onClick={onRequestClose}
onClick={closeOnOverlayClick ? onRequestClose : undefined}
onKeyDown={handleEscKeyPress}
/>
)
Expand Down