11import { SvgSearch , SvgTaillessArrowDownSmall , SvgX } from "@chainlink/blocks"
22import styles from "./styles.module.css"
3- import { useState } from "react"
3+ import { useState , useEffect , useRef } from "react"
44import { clsx } from "~/lib/clsx/clsx.ts"
55import type { ChangelogItem } from "~/components/ChangelogSnippet/types.ts"
66import { useChangelogFilters } from "./useChangelogFilters.ts"
@@ -121,6 +121,7 @@ interface DesktopFiltersProps {
121121
122122export 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