Skip to content

Commit b7d6a1c

Browse files
committed
click out side handler
1 parent 54b65ff commit b7d6a1c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/components/ChangelogFilters/DesktopFilters.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SvgSearch, SvgTaillessArrowDownSmall, SvgX } from "@chainlink/blocks"
22
import styles from "./styles.module.css"
3-
import { useState } from "react"
3+
import { useState, useEffect, useRef } from "react"
44
import { clsx } from "~/lib/clsx/clsx.ts"
55
import type { ChangelogItem } from "~/components/ChangelogSnippet/types.ts"
66
import { useChangelogFilters } from "./useChangelogFilters.ts"
@@ -121,6 +121,7 @@ interface DesktopFiltersProps {
121121

122122
export const DesktopFilters = ({ products, networks, types, items }: DesktopFiltersProps) => {
123123
const [activeFilter, setActiveFilter] = useState<FilterType>(null)
124+
const wrapperRef = useRef<HTMLDivElement>(null)
124125

125126
const {
126127
searchExpanded,
@@ -144,6 +145,22 @@ export const DesktopFilters = ({ products, networks, types, items }: DesktopFilt
144145
setActiveFilter(null)
145146
}
146147

148+
// Close filter when clicking outside
149+
useEffect(() => {
150+
if (!activeFilter) return
151+
152+
const handleClickOutside = (event: MouseEvent) => {
153+
if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) {
154+
closeFilter()
155+
}
156+
}
157+
158+
document.addEventListener("mousedown", handleClickOutside)
159+
return () => {
160+
document.removeEventListener("mousedown", handleClickOutside)
161+
}
162+
}, [activeFilter])
163+
147164
const getFilterOptions = () => {
148165
switch (activeFilter) {
149166
case "product":
@@ -171,7 +188,7 @@ export const DesktopFilters = ({ products, networks, types, items }: DesktopFilt
171188
}
172189

173190
return (
174-
<>
191+
<div ref={wrapperRef}>
175192
{activeFilter && (
176193
<div className={styles.expandedContent}>
177194
{getFilterOptions().map((option) => (
@@ -223,6 +240,6 @@ export const DesktopFilters = ({ products, networks, types, items }: DesktopFilt
223240
onChange={handleSearchChange}
224241
/>
225242
</div>
226-
</>
243+
</div>
227244
)
228245
}

0 commit comments

Comments
 (0)