1- "use client"
21import { useState , useEffect , useRef } from "react"
32import { InputGroup , InputLeftElement , Input , VStack , Text , useColorModeValue , InputRightElement , Button , Fade , StackProps } from "@chakra-ui/react"
43import { Search } from "lucide-react"
@@ -27,6 +26,13 @@ const SearchBar = ({ enableLabel = true, sidebarMode = false, onSearch, focusOnO
2726 const tipColor = useColorModeValue ( "blue.600" , "blue.300" )
2827 const labelColor = useColorModeValue ( "gray.500" , "gray.400" )
2928
29+ const handleFocus = ( ) => {
30+ inputRef . current ?. scrollIntoView ( {
31+ behavior : "smooth" ,
32+ block : "center" ,
33+ } )
34+ }
35+
3036 const handleSearch = ( ) => {
3137 if ( query . trim ( ) ) {
3238 router . push ( `/search?q=${ query } ` )
@@ -36,15 +42,21 @@ const SearchBar = ({ enableLabel = true, sidebarMode = false, onSearch, focusOnO
3642
3743 const handleHighlight = ( ) => {
3844 setHighlight ( true )
45+ handleFocus ( )
3946 setTimeout ( ( ) => {
4047 inputRef . current ?. focus ( )
4148 } , 500 )
49+
50+ setTimeout ( ( ) => {
51+ setHighlight ( false )
52+ } , 1500 )
4253 }
4354
4455 useEffect ( ( ) => {
4556 if ( inputRef . current && focusOnOpen ) {
4657 setTimeout ( ( ) => {
4758 inputRef . current ?. focus ( )
59+ handleFocus ( )
4860 } , 300 )
4961 }
5062 } , [ focusOnOpen ] )
@@ -57,7 +69,7 @@ const SearchBar = ({ enableLabel = true, sidebarMode = false, onSearch, focusOnO
5769 return (
5870 < VStack spacing = { 3 } w = "100%" maxW = "700px" mx = "auto" py = { 8 } { ...props } id = "search-bar" >
5971 { enableLabel && (
60- < Text fontSize = " lg" fontWeight = "semibold" color = { labelColor } textAlign = "center" >
72+ < Text fontSize = { { base : "md" , md : " lg" } } fontWeight = "semibold" color = { labelColor } textAlign = "center" >
6173 { t ( "searchBar.label" ) }
6274 </ Text >
6375 ) }
@@ -73,7 +85,7 @@ const SearchBar = ({ enableLabel = true, sidebarMode = false, onSearch, focusOnO
7385 value = { query }
7486 onChange = { ( e ) => setQuery ( e . target . value ) }
7587 placeholder = { ! sidebarMode ? t ( "searchBar.placeholder.main" ) : t ( "searchBar.placeholder.sidebar" ) }
76- fontSize = { ! sidebarMode ? "lg " : "sm" }
88+ fontSize = { { base : sidebarMode ? "sm " : "md" , md : "md" , lg : "lg" } }
7789 bg = { bg }
7890 _focus = { { bg : focusBg } }
7991 _placeholder = { { color : placeholderColor } }
@@ -86,11 +98,18 @@ const SearchBar = ({ enableLabel = true, sidebarMode = false, onSearch, focusOnO
8698 onKeyDown = { ( e ) => {
8799 if ( e . key === "Enter" ) handleSearch ( )
88100 } }
101+ onClick = { ( ) => handleFocus ( ) }
89102 />
90103
91104 { /* Bottone ricerca */ }
92105 < InputRightElement w = "5.5rem" h = "100%" >
93- < Button colorScheme = "blue" size = { ! sidebarMode ? "sm" : "xs" } fontSize = { ! sidebarMode ? "sm" : "xs" } rounded = "md" onClick = { handleSearch } >
106+ < Button
107+ colorScheme = "blue"
108+ size = { sidebarMode ? "xs" : { base : "xs" , xs : "sm" } }
109+ fontSize = { ! sidebarMode ? "sm" : "xs" }
110+ rounded = "md"
111+ onClick = { handleSearch }
112+ >
94113 { t ( "searchBar.button" ) }
95114 </ Button >
96115 </ InputRightElement >
0 commit comments