~stop placeholder animation on input
Browse files- components/searchBar.jsx +13 -5
components/searchBar.jsx
CHANGED
@@ -38,7 +38,7 @@ const SearchBar = ({ onSearch }) => {
|
|
38 |
}, []);
|
39 |
|
40 |
useEffect(() => {
|
41 |
-
if (!showInitialPlaceholder) {
|
42 |
let typingInterval;
|
43 |
if (placeholder.length < placeholders[placeholderIndex].length) {
|
44 |
const typingSpeed = Math.floor(Math.random() * 50) + 100;
|
@@ -48,10 +48,10 @@ const SearchBar = ({ onSearch }) => {
|
|
48 |
}
|
49 |
return () => clearInterval(typingInterval);
|
50 |
}
|
51 |
-
}, [placeholder, placeholderIndex, showInitialPlaceholder]);
|
52 |
|
53 |
useEffect(() => {
|
54 |
-
if (!showInitialPlaceholder) {
|
55 |
const indexInterval = setInterval(() => {
|
56 |
if (placeholder === placeholders[placeholderIndex]) {
|
57 |
setPlaceholderIndex(Math.floor(Math.random() * placeholders.length));
|
@@ -61,7 +61,7 @@ const SearchBar = ({ onSearch }) => {
|
|
61 |
|
62 |
return () => clearInterval(indexInterval);
|
63 |
}
|
64 |
-
}, [placeholder, placeholderIndex, showInitialPlaceholder]);
|
65 |
|
66 |
const handleKeyDown = (event) => {
|
67 |
if (event.key === "Enter") {
|
@@ -69,6 +69,14 @@ const SearchBar = ({ onSearch }) => {
|
|
69 |
}
|
70 |
};
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
return (
|
73 |
<div className="flex items-center justify-center bg-gray-900 rounded-xl shadow-sm w-full lg:w-1/2 h-12 my-8">
|
74 |
<input
|
@@ -76,7 +84,7 @@ const SearchBar = ({ onSearch }) => {
|
|
76 |
placeholder={showInitialPlaceholder ? initialPlaceholder : placeholder}
|
77 |
className="search-bar w-full h-full px-4 py-2 text-gray-200 bg-gray-800 border border-gray-700 rounded-xl shadow-sm appearance-none focus:outline-none focus:ring-2"
|
78 |
value={query}
|
79 |
-
onChange={
|
80 |
onKeyDown={handleKeyDown}
|
81 |
/>
|
82 |
</div>
|
|
|
38 |
}, []);
|
39 |
|
40 |
useEffect(() => {
|
41 |
+
if (!showInitialPlaceholder && query === "") {
|
42 |
let typingInterval;
|
43 |
if (placeholder.length < placeholders[placeholderIndex].length) {
|
44 |
const typingSpeed = Math.floor(Math.random() * 50) + 100;
|
|
|
48 |
}
|
49 |
return () => clearInterval(typingInterval);
|
50 |
}
|
51 |
+
}, [placeholder, placeholderIndex, showInitialPlaceholder, query]);
|
52 |
|
53 |
useEffect(() => {
|
54 |
+
if (!showInitialPlaceholder && query === "") {
|
55 |
const indexInterval = setInterval(() => {
|
56 |
if (placeholder === placeholders[placeholderIndex]) {
|
57 |
setPlaceholderIndex(Math.floor(Math.random() * placeholders.length));
|
|
|
61 |
|
62 |
return () => clearInterval(indexInterval);
|
63 |
}
|
64 |
+
}, [placeholder, placeholderIndex, showInitialPlaceholder, query]);
|
65 |
|
66 |
const handleKeyDown = (event) => {
|
67 |
if (event.key === "Enter") {
|
|
|
69 |
}
|
70 |
};
|
71 |
|
72 |
+
const handleChange = (e) => {
|
73 |
+
setQuery(e.target.value);
|
74 |
+
if (e.target.value === "") {
|
75 |
+
setPlaceholder("");
|
76 |
+
setPlaceholderIndex(Math.floor(Math.random() * placeholders.length));
|
77 |
+
}
|
78 |
+
};
|
79 |
+
|
80 |
return (
|
81 |
<div className="flex items-center justify-center bg-gray-900 rounded-xl shadow-sm w-full lg:w-1/2 h-12 my-8">
|
82 |
<input
|
|
|
84 |
placeholder={showInitialPlaceholder ? initialPlaceholder : placeholder}
|
85 |
className="search-bar w-full h-full px-4 py-2 text-gray-200 bg-gray-800 border border-gray-700 rounded-xl shadow-sm appearance-none focus:outline-none focus:ring-2"
|
86 |
value={query}
|
87 |
+
onChange={handleChange}
|
88 |
onKeyDown={handleKeyDown}
|
89 |
/>
|
90 |
</div>
|