Skip to content

Commit a47cd4f

Browse files
authored
[devtool] handle click outscope child element (#81296)
Improve the DX of close by clicking outside of the of the UI, if it's children it also shouldn't close it. Since devtool is under the shadow dom, we need to attach the listeners onto `ownerDocument` rather than the `document` itself ### Diff | After | Before | |:--|:--| | <video src="https://github.com/user-attachments/assets/617d79f8-f093-4c17-9588-633331a416bc"> | <video src="https://github.com/user-attachments/assets/94626f7e-6f2f-450a-aa73-f9cb5548820d"> | <!-- https://github.com/user-attachments/assets/617d79f8-f093-4c17-9588-633331a416bc --> <!-- https://github.com/user-attachments/assets/94626f7e-6f2f-450a-aa73-f9cb5548820d -->
1 parent 2c6b7dc commit a47cd4f

File tree

1 file changed

+11
-4
lines changed
  • packages/next/src/next-devtools/dev-overlay/components/errors/dev-tools-indicator

1 file changed

+11
-4
lines changed

packages/next/src/next-devtools/dev-overlay/components/errors/dev-tools-indicator/utils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ export function useClickOutside(
9191
return
9292
}
9393

94+
const ownerDocument = rootRef.current?.ownerDocument
95+
9496
function handleClickOutside(event: MouseEvent) {
97+
const target = event.target as HTMLElement
98+
if (rootRef.current && rootRef.current.contains(target)) {
99+
return
100+
}
101+
95102
if (
96103
!(rootRef.current?.getBoundingClientRect()
97104
? event.clientX >= rootRef.current.getBoundingClientRect()!.left &&
@@ -117,12 +124,12 @@ export function useClickOutside(
117124
}
118125
}
119126

120-
document.addEventListener('mousedown', handleClickOutside)
121-
document.addEventListener('keydown', handleKeyDown)
127+
ownerDocument?.addEventListener('mousedown', handleClickOutside)
128+
ownerDocument?.addEventListener('keydown', handleKeyDown)
122129

123130
return () => {
124-
document.removeEventListener('mousedown', handleClickOutside)
125-
document.removeEventListener('keydown', handleKeyDown)
131+
ownerDocument?.removeEventListener('mousedown', handleClickOutside)
132+
ownerDocument?.removeEventListener('keydown', handleKeyDown)
126133
}
127134
// eslint-disable-next-line react-hooks/exhaustive-deps
128135
}, [active])

0 commit comments

Comments
 (0)