Skip to content

Commit 1196548

Browse files
committed
Dont want for location change to finish before closing search on item select
1 parent 53dbeda commit 1196548

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/components/search.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import SearchResults from './search-results'
88
import useSiteMetadata from '../hooks/use-site-metadata'
99
import {HEADER_BAR, HEADER_HEIGHT} from '../constants'
1010
import {LightTheme} from '../theme'
11-
import useLocationChange from '../hooks/use-location-change'
1211

1312
export const Desktop = props => {
1413
const siteMetadata = useSiteMetadata()
15-
const {getInputProps, getMenuProps, isOpen, results, getItemProps, highlightedIndex} = props
14+
const {getInputProps, getMenuProps, resultsOpen, results, getItemProps, highlightedIndex} = props
1615

1716
return (
1817
<Box sx={{position: 'relative'}}>
@@ -23,7 +22,7 @@ export const Desktop = props => {
2322
{...getInputProps()}
2423
/>
2524
<Box sx={{position: 'absolute', left: 0, right: 0, pt: 1}} {...getMenuProps()}>
26-
{isOpen ? (
25+
{resultsOpen ? (
2726
<LightTheme
2827
sx={{
2928
overflow: 'auto',
@@ -46,46 +45,35 @@ export const Desktop = props => {
4645
}
4746

4847
export const Mobile = ({
49-
reset,
5048
results,
51-
isOpen: resultsOpen,
49+
resultsOpen,
5250
getInputProps,
5351
getItemProps,
5452
getMenuProps,
5553
highlightedIndex,
54+
isMobileSearchOpen,
55+
setMobileSearchOpen,
56+
resetAndClose,
5657
}) => {
57-
const [open, setOpen] = React.useState(false)
5858
const siteMetadata = useSiteMetadata()
59-
const locationChange = useLocationChange()
60-
61-
React.useEffect(() => {
62-
if (locationChange.change) {
63-
setOpen(false)
64-
}
65-
}, [locationChange])
6659

6760
// Fixes focus behavior on iOS where the input gets focus styles but not the
6861
// actual focus after animating open.
6962
const ref = React.useRef()
7063
React.useEffect(() => {
71-
if (open) {
64+
if (isMobileSearchOpen) {
7265
ref.current.focus()
7366
}
74-
}, [ref, open])
75-
76-
const handleDismiss = () => {
77-
reset()
78-
setOpen(false)
79-
}
67+
}, [ref, isMobileSearchOpen])
8068

8169
return (
8270
<>
83-
<Button aria-label="Search" aria-expanded={open} onClick={() => setOpen(true)}>
71+
<Button aria-label="Search" aria-expanded={isMobileSearchOpen} onClick={() => setMobileSearchOpen(true)}>
8472
<SearchIcon />
8573
</Button>
8674
<AnimatePresence>
87-
{open ? (
88-
<FocusOn returnFocus={true} onEscapeKey={handleDismiss}>
75+
{isMobileSearchOpen ? (
76+
<FocusOn returnFocus={true} onEscapeKey={resetAndClose}>
8977
<Box
9078
sx={{
9179
position: 'fixed',
@@ -112,7 +100,7 @@ export const Mobile = ({
112100
animate={{opacity: 1}}
113101
exit={{opacity: 0}}
114102
transition={{type: 'tween'}}
115-
onClick={handleDismiss}
103+
onClick={resetAndClose}
116104
/>
117105
<Box sx={{display: 'flex', flexDirection: 'column', height: resultsOpen ? '100%' : 'auto'}}>
118106
<Box
@@ -183,7 +171,7 @@ export const Mobile = ({
183171
exit={{opacity: 0}}
184172
transition={{type: 'tween', ease: 'easeOut', duration: 0.2}}
185173
>
186-
<Button sx={{ml: 3}} aria-label="Cancel" onClick={handleDismiss}>
174+
<Button sx={{ml: 3}} aria-label="Cancel" onClick={resetAndClose}>
187175
<XIcon />
188176
</Button>
189177
</Box>

src/hooks/use-search.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ const useCliVersion = () => {
5555

5656
const useSearchCombobox = (results, setQuery) => {
5757
const isMobile = useIsMobile()
58+
const [isMobileSearchOpen, setMobileSearchOpen] = React.useState(false)
59+
5860
const combobox = useCombobox({
5961
id: 'search-box',
6062
items: results || [],
6163
onInputValueChange: ({inputValue}) => setQuery(inputValue),
6264
onSelectedItemChange: ({selectedItem}) => {
6365
if (selectedItem) {
6466
navigate(selectedItem.path)
67+
setMobileSearchOpen(false)
6568
combobox.reset()
6669
}
6770
},
@@ -90,7 +93,18 @@ const useSearchCombobox = (results, setQuery) => {
9093
return changes
9194
},
9295
})
93-
return combobox
96+
97+
const resetAndClose = React.useCallback(() => {
98+
combobox.reset()
99+
setMobileSearchOpen(false)
100+
}, [combobox, setMobileSearchOpen])
101+
102+
return {
103+
...combobox,
104+
isMobileSearchOpen,
105+
setMobileSearchOpen,
106+
resetAndClose,
107+
}
94108
}
95109

96110
function useSearch() {
@@ -138,7 +152,7 @@ function useSearch() {
138152
return {
139153
...combobox,
140154
results,
141-
isOpen: !!(combobox.isOpen && results),
155+
resultsOpen: !!(combobox.isOpen && results),
142156
}
143157
}
144158

0 commit comments

Comments
 (0)