Skip to content

Commit 8a7ad20

Browse files
authored
Hide dropdown content when clicking away (nasa-gcn#1989)
1 parent 799f515 commit 8a7ad20

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

app/routes/circulars._archive._index/DateSelectorMenu.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from '@trussworks/react-uswds'
1919
import classNames from 'classnames'
2020
import { type ChangeEvent, useRef, useState } from 'react'
21+
import { useOnClickOutside } from 'usehooks-ts'
2122

2223
import DetailsDropdownContent from '~/components/DetailsDropdownContent'
2324

@@ -77,7 +78,11 @@ export function DateSelector({
7778
defaultStartDate?: string
7879
defaultEndDate?: string
7980
}) {
81+
const ref = useRef<HTMLDivElement>(null)
8082
const [showContent, setShowContent] = useState(false)
83+
useOnClickOutside(ref, () => {
84+
setShowContent(false)
85+
})
8186
const defaultShowDateRange = Boolean(
8287
(defaultStartDate && !dateSelectorLabels[defaultStartDate]) ||
8388
defaultEndDate
@@ -105,7 +110,7 @@ export function DateSelector({
105110
}
106111

107112
return (
108-
<>
113+
<div ref={ref}>
109114
<input
110115
type="hidden"
111116
name="startDate"
@@ -218,6 +223,6 @@ export function DateSelector({
218223
</CardFooter>
219224
)}
220225
</DetailsDropdownContent>
221-
</>
226+
</div>
222227
)
223228
}

app/routes/circulars._archive._index/SortSelectorButton.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
} from '@trussworks/react-uswds'
1717
import classNames from 'classnames'
1818
import type { ChangeEvent } from 'react'
19-
import { useState } from 'react'
19+
import { useRef, useState } from 'react'
20+
import { useOnClickOutside } from 'usehooks-ts'
2021

2122
import DetailsDropdownContent from '~/components/DetailsDropdownContent'
2223

@@ -58,9 +59,12 @@ export function SortSelector({
5859
form?: string
5960
defaultValue?: string
6061
}) {
61-
const [showContent, setShowContent] = useState(false)
62-
6362
const submit = useSubmit()
63+
const ref = useRef<HTMLDivElement>(null)
64+
const [showContent, setShowContent] = useState(false)
65+
useOnClickOutside(ref, () => {
66+
setShowContent(false)
67+
})
6468

6569
function radioOnChange({ target }: ChangeEvent<HTMLInputElement>) {
6670
setShowContent(false)
@@ -88,7 +92,7 @@ export function SortSelector({
8892
)
8993

9094
return (
91-
<>
95+
<div ref={ref}>
9296
<SortButton
9397
sort={sanitizedValue}
9498
expanded={showContent}
@@ -108,6 +112,6 @@ export function SortSelector({
108112
</Grid>
109113
</CardBody>
110114
</DetailsDropdownContent>
111-
</>
115+
</div>
112116
)
113117
}

0 commit comments

Comments
 (0)