Skip to content

Commit

Permalink
fix(drawer): handle clickoutside on slower active boolean (#1621)
Browse files Browse the repository at this point in the history
* fix(drawer): handle clickoutside on slower active boolean

* test: drawer opens on click
  • Loading branch information
jinlee93 authored May 22, 2023
1 parent 5bad84f commit c61508b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
12 changes: 0 additions & 12 deletions src/components/Drawer/Drawer.test.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/components/Drawer/Drawer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { generateSnapshots } from '@chanzuckerberg/story-utils';
import { composeStories } from '@storybook/testing-react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import * as stories from './Drawer.stories';
const { DefaultInteractive } = composeStories(stories);

describe('<Drawer />', () => {
generateSnapshots(stories, {
getElement: async () => {
const filters = await screen.findByRole('dialog');
return filters.parentElement; // eslint-disable-line testing-library/no-node-access
},
});

it('shows the dialog when the open dialog button is clicked', async () => {
const user = userEvent.setup();
render(<DefaultInteractive />);
const openDrawerButton = await screen.findByRole('button', {
name: 'Open Drawer',
});
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
await user.click(openDrawerButton);
expect(screen.getByRole('dialog')).toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const Drawer = ({
useEffect(() => {
function handleOnClickOutside(e: MouseEvent) {
if (
isActive &&
activeFocus &&
dismissible &&
windowRef.current &&
!windowRef.current.contains(e.target as HTMLElement)
Expand All @@ -157,7 +157,7 @@ export const Drawer = ({
return () => {
document.removeEventListener('click', handleOnClickOutside);
};
}, [isActive, dismissible, handleOnClose, windowRef]);
}, [activeFocus, dismissible, handleOnClose, windowRef]);

/**
* If escape button is struck, close the drawer
Expand Down

0 comments on commit c61508b

Please sign in to comment.