Skip to content

Commit

Permalink
dropdown strictmode findDomNode deps eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Glidias committed Jun 9, 2023
1 parent f533410 commit 15d16e3
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ const Dropdown = React.forwardRef<HTMLDivElement, DropdownProps>(function Dropdo

const baseStyle = dropdown.base
const alignStyle = dropdown.align[align]

function handleEsc(e: KeyboardEvent) {
if (e.key === 'Esc' || e.key === 'Escape') {
onClose()
}
}

const dropdownRef = useRef<HTMLUListElement>(null)
function handleClickOutside(e: MouseEvent) {
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
onClose()
}
}

const closeRef = useRef(onClose);
closeRef.current = onClose;

useEffect(() => {
function handleEsc(e: KeyboardEvent) {
if (e.key === 'Esc' || e.key === 'Escape') {
closeRef.current()
}
}
function handleClickOutside(e: MouseEvent) {
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
closeRef.current()
}
}

document.addEventListener('click', handleClickOutside, { capture: true })
document.addEventListener('keydown', handleEsc, { capture: true })
return () => {
Expand Down

0 comments on commit 15d16e3

Please sign in to comment.