Extract useClickOutside hook, dedupe popover close-on-outside-click logic
RegexHelpIcon and LogPreview's ColumnPicker each reimplemented the same mousedown-outside-ref-closes-popover effect; share it via a small hook. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TeyhRfMYjMditek6geDRoQ
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
a47f771087
commit
c0174963d8
@@ -0,0 +1,14 @@
|
||||
import { useEffect, type RefObject } from 'react'
|
||||
|
||||
// Shared by popover-style UI (RegexHelpIcon, LogPreview's ColumnPicker) that closes itself when a
|
||||
// mousedown lands outside its container ref, while `active`.
|
||||
export function useClickOutside(ref: RefObject<HTMLElement | null>, onOutside: () => void, active: boolean) {
|
||||
useEffect(() => {
|
||||
if (!active) return
|
||||
function onClickOutside(e: MouseEvent) {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) onOutside()
|
||||
}
|
||||
document.addEventListener('mousedown', onClickOutside)
|
||||
return () => document.removeEventListener('mousedown', onClickOutside)
|
||||
}, [active, ref, onOutside])
|
||||
}
|
||||
Reference in New Issue
Block a user