1
+ import { useRef , useState } from 'react'
1
2
import { CiSearch } from 'react-icons/ci'
3
+ import { MdOutlineClear } from 'react-icons/md'
2
4
import Browser from 'webextension-polyfill'
3
5
import { BookmarkProvider } from '../../context/bookmark.context'
4
6
import { useTheme } from '../../context/theme.context'
5
7
import { BookmarksComponent } from './bookmarks/bookmarks'
6
8
7
9
export function SearchLayout ( ) {
8
10
const { theme, themeUtils } = useTheme ( )
11
+ const [ searchQuery , setSearchQuery ] = useState ( '' )
12
+ const searchRef = useRef < HTMLDivElement > ( null )
13
+ const inputRef = useRef < HTMLInputElement > ( null )
9
14
10
15
const getSearchBoxBackground = ( ) => {
11
16
switch ( theme ) {
12
17
case 'light' :
13
18
return 'bg-white hover:bg-white/95'
14
19
case 'dark' :
15
- return 'bg-neutral-800 hover:bg-neutral-700/90'
20
+ return 'bg-neutral-800 hover:bg-neutral-700/90'
16
21
default :
17
22
return 'bg-neutral-900/70 backdrop-blur-sm hover:bg-neutral-800/80'
18
23
}
@@ -29,39 +34,66 @@ export function SearchLayout() {
29
34
}
30
35
}
31
36
32
- const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
37
+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
33
38
e . preventDefault ( )
34
- const query = ( e . target as HTMLFormElement ) . search . value
35
- if ( query . trim ( ) ) {
39
+ const query = searchQuery . trim ( )
40
+ if ( query ) {
36
41
Browser . search . query ( { text : query } )
37
42
}
38
43
}
39
44
45
+ const handleSearchInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
46
+ setSearchQuery ( e . target . value )
47
+ }
48
+
49
+ const handleClearSearch = ( ) => {
50
+ setSearchQuery ( '' )
51
+ if ( inputRef . current ) {
52
+ inputRef . current . focus ( )
53
+ }
54
+ }
55
+
40
56
return (
41
57
< >
42
58
< div className = "flex flex-col items-center justify-center max-h-80" >
43
- < form className = "w-full" onSubmit = { handleSubmit } >
44
- < div
45
- className = { `relative overflow-hidden transition-all duration-300 shadow-xl rounded-2xl group ${ getSearchBoxBackground ( ) } ` }
46
- >
47
- < input
48
- type = "text"
49
- name = "search"
50
- className = { `w-full py-4 pl-16 pr-6 text-lg font-light text-right bg-transparent focus:outline-none ${ themeUtils . getTextColor ( ) } ` }
51
- placeholder = "جستجو ..."
52
- autoComplete = "off"
53
- />
54
- < button
55
- type = "submit"
56
- className = { `absolute p-2 transition-all duration-300 -translate-y-1/2 rounded-lg cursor-pointer left-3 top-1/2 ${ getSearchButtonStyles ( ) } ` }
57
- >
58
- < CiSearch size = { 20 } />
59
- </ button >
59
+ < div className = "relative w-full" ref = { searchRef } >
60
+ < form className = "w-full" onSubmit = { handleSubmit } >
60
61
< div
61
- className = { `absolute inset-0 transition-all duration-300 border pointer-events-none rounded-2xl ${ themeUtils . getBorderColor ( ) } ` }
62
- />
63
- </ div >
64
- </ form >
62
+ className = { `relative overflow-hidden transition-all duration-300 shadow-lg hover:shadow-xl rounded-xl group ${ getSearchBoxBackground ( ) } ` }
63
+ >
64
+ < input
65
+ ref = { inputRef }
66
+ type = "text"
67
+ name = "search"
68
+ value = { searchQuery }
69
+ onChange = { handleSearchInputChange }
70
+ className = { `w-full py-4 pr-12 pl-16 text-lg font-medium text-right bg-transparent focus:outline-none ${ themeUtils . getTextColor ( ) } ` }
71
+ placeholder = "جستجو ..."
72
+ autoComplete = "off"
73
+ />
74
+ < button
75
+ type = "submit"
76
+ className = { `absolute p-3 transition-all duration-300 -translate-y-1/2 rounded-lg cursor-pointer left-2 top-1/2 ${ getSearchButtonStyles ( ) } ` }
77
+ >
78
+ < CiSearch size = { 22 } />
79
+ </ button >
80
+ { searchQuery && (
81
+ < button
82
+ type = "button"
83
+ onClick = { handleClearSearch }
84
+ className = {
85
+ 'absolute p-2 transition-all duration-200 -translate-y-1/2 rounded-full cursor-pointer right-3 top-1/2 text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700'
86
+ }
87
+ >
88
+ < MdOutlineClear size = { 16 } />
89
+ </ button >
90
+ ) }
91
+ < div
92
+ className = { `absolute inset-0 transition-all duration-300 border pointer-events-none rounded-xl ${ themeUtils . getBorderColor ( ) } ` }
93
+ />
94
+ </ div >
95
+ </ form >
96
+ </ div >
65
97
< BookmarkProvider >
66
98
< BookmarksComponent />
67
99
</ BookmarkProvider >
0 commit comments