Skip to content

Commit 044eef2

Browse files
committed
Close dropdown on scroll and attach outside click listener only when open
- should close dropdown on scroll └── packages └── ui └── src └── components └── editor └── common ├── Dropdown │ ├── Dropdown.module.scss │ ├── Dropdown.stories.tsx │ ├── Dropdown.tsx │ └── index.ts └── DropdownMenu ├── DropdownMenu.module.scss ├── DropdownMenu.stories.tsx ├── DropdownMenu.tsx └── index.ts
1 parent 1008c8e commit 044eef2

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

  • packages/ui/src/components/editor/common/Dropdown

‎packages/ui/src/components/editor/common/Dropdown/Dropdown.tsx‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,26 @@ export const Dropdown = <T extends string>(props: Dropdown.Props<T>) => {
4747

4848
const selected_option = props.options.find((opt) => opt.value == props.value)
4949

50-
const handle_click_outside = (event: MouseEvent) => {
51-
if (
52-
dropdown_ref.current &&
53-
!dropdown_ref.current.contains(event.target as Node)
54-
) {
55-
set_is_open(false)
50+
useEffect(() => {
51+
if (!is_open) return
52+
53+
const handle_click_outside = (event: MouseEvent | Event) => {
54+
if (
55+
dropdown_ref.current &&
56+
!dropdown_ref.current.contains(event.target as Node)
57+
) {
58+
set_is_open(false)
59+
}
5660
}
57-
}
5861

59-
useEffect(() => {
6062
document.addEventListener('mousedown', handle_click_outside)
63+
document.addEventListener('scroll', handle_click_outside, true)
64+
6165
return () => {
6266
document.removeEventListener('mousedown', handle_click_outside)
67+
document.removeEventListener('scroll', handle_click_outside, true)
6368
}
64-
}, [])
69+
}, [is_open])
6570

6671
const menu_items: DropdownMenu.Item[] = props.options.map((option) => ({
6772
label: option.label,

0 commit comments

Comments
 (0)