NavbarSearchIcon.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use client";
  2. import { useState,useEffect } from "react";
  3. import SearchPop from "./SearchPop";
  4. import { usePathname } from "next/navigation";
  5. export default function NavbarSearchIcon() {
  6. const [searchOpen, setSearchOpen] = useState(false);
  7. const pathname = usePathname();
  8. useEffect(() => {
  9. if (searchOpen) {
  10. queueMicrotask(() => {
  11. setSearchOpen(false);
  12. });
  13. }
  14. }, [pathname, searchOpen]);
  15. const handleClick = () => {
  16. setSearchOpen((prev) => !prev);
  17. };
  18. return (
  19. <>
  20. <button
  21. type="button"
  22. aria-label={searchOpen ? "Close search" : "Open search"}
  23. onClick={handleClick}
  24. className="flex h-6 w-6 cursor-pointer items-center justify-center"
  25. >
  26. {searchOpen ? (
  27. /* Close Icon */
  28. <svg
  29. xmlns="http://www.w3.org/2000/svg"
  30. width="18"
  31. height="18"
  32. viewBox="0 0 18 18"
  33. fill="none"
  34. >
  35. <path
  36. d="M2 2L16 16"
  37. stroke="black"
  38. strokeWidth="1.5"
  39. strokeLinecap="round"
  40. />
  41. <path
  42. d="M16 2L2 16"
  43. stroke="black"
  44. strokeWidth="1.5"
  45. strokeLinecap="round"
  46. />
  47. </svg>
  48. ) : (
  49. /* Search Icon */
  50. <svg
  51. xmlns="http://www.w3.org/2000/svg"
  52. width="17.7802734375"
  53. height="18.7802734375"
  54. viewBox="0 0 17.7802734375 18.7802734375"
  55. fill="none"
  56. >
  57. <circle
  58. cx="8.75"
  59. cy="8.75"
  60. r="8"
  61. stroke="black"
  62. strokeWidth="1.5"
  63. />
  64. <path
  65. d="M14.25 15.25L17.25 18.25"
  66. stroke="black"
  67. strokeWidth="1.5"
  68. />
  69. </svg>
  70. )}
  71. </button>
  72. <SearchPop
  73. open={searchOpen}
  74. onClose={() => setSearchOpen(false)}
  75. />
  76. </>
  77. );
  78. }