Unverified Commit e11e87a5 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[backport] fix: error overlay not closing when backdrop clicked (#83981) (#83992)

Backports #83981
parent 0a298885
...@@ -37,8 +37,7 @@ const Dialog: React.FC<DialogProps> = function Dialog({ ...@@ -37,8 +37,7 @@ const Dialog: React.FC<DialogProps> = function Dialog({
) )
useOnClickOutside( useOnClickOutside(
// eslint-disable-next-line react-hooks/react-compiler -- TODO dialogRef,
dialogRef.current,
CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE, CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE,
(e) => { (e) => {
e.preventDefault() e.preventDefault()
......
import * as React from 'react' import * as React from 'react'
export function useOnClickOutside( export function useOnClickOutside(
el: Node | null, el: Node | React.RefObject<Node | null> | null,
cssSelectorsToExclude: string[], cssSelectorsToExclude: string[],
handler: ((e: MouseEvent | TouchEvent) => void) | undefined handler: ((e: MouseEvent | TouchEvent) => void) | undefined
) { ) {
React.useEffect(() => { React.useEffect(() => {
if (el == null || handler == null) { // Support both direct nodes and ref objects
const element = el && 'current' in el ? el.current : el
if (element == null || handler == null) {
return return
} }
const listener = (e: MouseEvent | TouchEvent) => { const listener = (e: MouseEvent | TouchEvent) => {
// Do nothing if clicking ref's element or descendent elements // Do nothing if clicking ref's element or descendent elements
if (!el || el.contains(e.target as Element)) { if (!element || element.contains(e.target as Element)) {
return return
} }
...@@ -28,7 +30,7 @@ export function useOnClickOutside( ...@@ -28,7 +30,7 @@ export function useOnClickOutside(
handler(e) handler(e)
} }
const root = el.getRootNode() const root = element.getRootNode()
root.addEventListener('mouseup', listener as EventListener) root.addEventListener('mouseup', listener as EventListener)
root.addEventListener('touchend', listener as EventListener, { root.addEventListener('touchend', listener as EventListener, {
passive: false, passive: false,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment