@@ -3,27 +3,28 @@ import {
3
3
Button ,
4
4
Flex ,
5
5
HStack ,
6
+ IconButton ,
6
7
Link ,
7
8
Modal ,
8
9
ModalBody ,
9
10
ModalContent ,
10
11
ModalOverlay ,
11
12
useDisclosure ,
12
13
} from "@chakra-ui/react" ;
13
- import { useRef , useState , useCallback } from "react" ;
14
- import { RiSearch2Line } from "react-icons/ri" ;
14
+ import { useCallback , useRef , useState } from "react" ;
15
+ import { RiCloseLine , RiSearch2Line } from "react-icons/ri" ;
15
16
import { useIntl } from "react-intl" ;
17
+ import useIsUnmounted from "../common/use-is-unmounted" ;
16
18
import { useDeployment } from "../deployment" ;
17
19
import { topBarHeight } from "../deployment/misc" ;
18
- import SearchDialog from "../documentation/search/SearchDialog " ;
20
+ import { SearchResults } from "../documentation/search/common " ;
19
21
import { useSearch } from "../documentation/search/search-hooks" ;
22
+ import SearchDialog from "../documentation/search/SearchDialog" ;
20
23
import { flags } from "../flags" ;
21
- import { SearchResults } from "../documentation/search/common" ;
22
- import useIsUnmounted from "../common/use-is-unmounted" ;
23
24
24
25
const SideBarHeader = ( ) => {
25
26
const ref = useRef < HTMLDivElement > ( null ) ;
26
- const horizontalLogoRef = useRef < HTMLDivElement > ( null ) ;
27
+ const faceLogoRef = useRef < HTMLDivElement > ( null ) ;
27
28
const intl = useIntl ( ) ;
28
29
const brand = useDeployment ( ) ;
29
30
const searchModal = useDisclosure ( ) ;
@@ -48,15 +49,22 @@ const SideBarHeader = () => {
48
49
} ,
49
50
[ search , isUnmounted ]
50
51
) ;
52
+ const handleClear = useCallback ( ( ) => {
53
+ setQuery ( "" ) ;
54
+ setResults ( undefined ) ;
55
+ } , [ setQuery , setResults ] ) ;
51
56
// Width of the sidebar tabs. Perhaps we can restructure the DOM?
52
- const offset = horizontalLogoRef . current
53
- ? horizontalLogoRef . current . getBoundingClientRect ( ) . left
57
+ const sidebarWidth = useRef < HTMLDivElement > ( null ) ;
58
+ const offset = faceLogoRef . current
59
+ ? faceLogoRef . current . getBoundingClientRect ( ) . right + 14
54
60
: 0 ;
55
- const width = ref . current
56
- ? ref . current ! . clientWidth - offset - 14 + "px"
61
+ const width = sidebarWidth . current
62
+ ? sidebarWidth . current ! . clientWidth - offset - 14 + "px"
57
63
: undefined ;
58
64
return (
59
65
< >
66
+ { /* Empty box used to calculate width only. */ }
67
+ < Box ref = { sidebarWidth } > </ Box >
60
68
{ flags . search && (
61
69
< Modal
62
70
isOpen = { searchModal . isOpen }
@@ -80,10 +88,9 @@ const SideBarHeader = () => {
80
88
< SearchDialog
81
89
onClose = { searchModal . onClose }
82
90
results = { results }
83
- setResults = { setResults }
84
91
query = { query }
85
- setQuery = { setQuery }
86
92
onQueryChange = { handleQueryChange }
93
+ onClear = { handleClear }
87
94
/>
88
95
</ ModalBody >
89
96
</ ModalContent >
@@ -109,20 +116,17 @@ const SideBarHeader = () => {
109
116
aria-label = { intl . formatMessage ( { id : "visit-dot-org" } ) }
110
117
>
111
118
< HStack spacing = { 3.5 } pl = { 4 } pr = { 4 } >
112
- < Box width = "3.56875rem" color = "white" role = "img" >
119
+ < Box width = "3.56875rem" color = "white" role = "img" ref = { faceLogoRef } >
113
120
{ brand . squareLogo }
114
121
</ Box >
115
- < Box
116
- ref = { horizontalLogoRef }
117
- width = "9.098rem"
118
- role = "img"
119
- color = "white"
120
- >
121
- { brand . horizontalLogo }
122
- </ Box >
122
+ { ! query && (
123
+ < Box width = "9.098rem" role = "img" color = "white" >
124
+ { brand . horizontalLogo }
125
+ </ Box >
126
+ ) }
123
127
</ HStack >
124
128
</ Link >
125
- { flags . search && (
129
+ { flags . search && ! query && (
126
130
< Button
127
131
onClick = { searchModal . onOpen }
128
132
backgroundColor = "#5c40a6"
@@ -140,6 +144,47 @@ const SideBarHeader = () => {
140
144
Search
141
145
</ Button >
142
146
) }
147
+ { flags . search && query && (
148
+ < Flex
149
+ backgroundColor = "white"
150
+ borderRadius = "3xl"
151
+ width = { `calc(100% - ${ offset } px)` }
152
+ position = "relative"
153
+ >
154
+ < Button
155
+ _active = { { } }
156
+ _hover = { { } }
157
+ border = "unset"
158
+ color = "gray.800"
159
+ flex = { 1 }
160
+ fontSize = "md"
161
+ fontWeight = "normal"
162
+ justifyContent = "flex-start"
163
+ leftIcon = {
164
+ < Box as = { RiSearch2Line } fontSize = "lg" color = "#838383" />
165
+ }
166
+ onClick = { searchModal . onOpen }
167
+ overflow = "hidden"
168
+ >
169
+ { query }
170
+ </ Button >
171
+ < IconButton
172
+ aria-label = "Clear"
173
+ backgroundColor = "white"
174
+ // Also used for Zoom, move to theme.
175
+ color = "#838383"
176
+ fontSize = "2xl"
177
+ icon = { < RiCloseLine /> }
178
+ isRound = { false }
179
+ onClick = { handleClear }
180
+ position = "absolute"
181
+ right = "0"
182
+ pr = { 3 }
183
+ pl = { 3 }
184
+ variant = "ghost"
185
+ />
186
+ </ Flex >
187
+ ) }
143
188
</ Flex >
144
189
</ >
145
190
) ;
0 commit comments