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

Fix bug where overlay would close when clicking inside it #1977

Merged
merged 3 commits into from
Feb 6, 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
5 changes: 5 additions & 0 deletions .changeset/sixty-cows-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@channel.io/bezier-react": patch
---

Fixes a bug where onHide is called when clicking inside the overlay, causing the overlay to close.
25 changes: 21 additions & 4 deletions packages/bezier-react/src/components/Overlay/Overlay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getWindow } from 'ssr-window'

import { render } from '~/src/utils/test'

import { Button } from '~/src/components/Button'

import {
CONTAINER_TEST_ID,
ESCAPE_KEY,
Expand Down Expand Up @@ -276,12 +278,12 @@ describe('Overlay', () => {
})

describe('Event', () => {
describe('keydown', () => {
document.onkeydown = jest.fn()
const onHide = jest.fn()
document.onkeydown = jest.fn()
const onHide = jest.fn()

afterEach(jest.clearAllMocks)
afterEach(jest.clearAllMocks)

describe('keydown', () => {
it('is Triggered By Escape', () => {
const { getByTestId } = renderRootOverlay({ withTransition: true, onHide })
const overlay = getByTestId(OVERLAY_TEST_ID)
Expand All @@ -307,6 +309,21 @@ describe('Overlay', () => {
expect(onHide).toHaveBeenCalledTimes(0)
})
})

describe('click', () => {
it('calls onHide when element outside the overlay is clicked', async () => {
renderRootOverlay({ onHide })
fireEvent.click(document.body)
expect(onHide).toHaveBeenCalled()
})

it('does not call onHide when element inside the overlay is clicked', () => {
const { getByRole } = renderRootOverlay({ children: (<Button text="button" />), onHide })
const button = getByRole('button')
fireEvent.click(button)
expect(onHide).not.toHaveBeenCalled()
})
})
})
})
})
4 changes: 2 additions & 2 deletions packages/bezier-react/src/components/Overlay/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ export const Overlay = forwardRef<HTMLDivElement, OverlayProps>(function Overlay
event.stopPropagation()
}, [])

const handleHideOverlay = useCallback((event: any) => {
if (!event.target?.closest(styles.Overlay)) {
const handleHideOverlay = useCallback((event: MouseEvent) => {
if (!event.target || (event.target instanceof HTMLElement && !event.target.closest(`.${styles.Overlay}`))) {
Comment on lines +158 to +159
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

onHide?.()

if (!enableClickOutside) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class SmoothCorners {
.get('--smooth-corners')
.toString()

console.log("padding: ", padding)
Copy link
Contributor

Choose a reason for hiding this comment

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

아이고 🥲

const targetWidth = geom.width - (2 * this.trimPX(padding))
const targetHeight = geom.height - (2 * this.trimPX(padding))

Expand Down
Loading