Absolutely! To make this header component responsive, we'll incorporate some Tailwind CSS classes for responsive design. The approach is to use utility classes that apply different styles based on the screen size.
Here's the adjusted code:
import { useEffect, useState } from "react";
import { FaShoppingCart } from "react-icons/fa";
import PopUp from "../ShopCartPopUp/PopUP";
export default function Header() {
const [isScrolled, setIsScrolled] = useState<boolean>(false);
const [isHide, setIsHide] = useState<boolean>(false);
const [clickAnime, setClickAnim] = useState<string>("left");
const [isClicked, setIsClicked] = useState<boolean>(false);
const handleHide = () => {
setIsClicked(true);
setTimeout(() => setIsHide(!isHide), 500);
setTimeout(() => setIsClicked(false), 1000);
clickAnime === "left" ? setClickAnim("right") : setClickAnim("left");
};
const headerButtons = [
"Главная",
"Продукты",
"О нас",
"FAQ",
"Новости",
"Контакты",
"Видео",
];
useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 50) {
setIsScrolled(true);
} else {
setIsScrolled(false);
}
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return (
<div
className={`fixed top-0 w-full min-h-10 bg-[#222222] text-white z-40 ${
isScrolled ? `duration-500 ease-out bg-opacity-70` : `bg-opacity-100`
}`}
>
<div
className={`w-11/12 ${
isScrolled
? "duration-500 min-h-10 ease-out"
: "duration-500 ease-out min-h-24"
} flex items-center m-auto`}
>
<div className="text-2xl font-bold">Image</div>
<div className="m-auto hidden lg:flex">
<ul className="flex gap-7">
{headerButtons.map((item, index) => {
return (
<li
key={index}
className="relative group inline-block cursor-pointer"
>
<a
href="#"
className="text-white transition-colors duration-300"
>
{item}
</a>
<span className="absolute left-1/2 bottom-[2px] w-full h-[2px] bg-white scale-x-0 group-hover:scale-x-100 transform -translate-x-1/2 origin-center transition-transform duration-300 ease-in-out"></span>
</li>
);
})}
</ul>
</div>
<div
className={`flex gap-5 ${
isClicked ? `pointer-events-none` : "pointer-events-auto"
}`}
onClick={handleHide}
>
<FaShoppingCart size={"2rem"} />
</div>
</div>
{isHide && (
<PopUp isScrolled={isScrolled} clickAnime={clickAnime}></PopUp>
)}
<div className="block lg:hidden">
<div className="m-auto flex flex-wrap">
<ul className="flex flex-wrap gap-3">
{headerButtons.map((item, index) => {
return (
<li
key={index}
className="relative group inline-block cursor-pointer"
>
<a
href="#"
className="text-white transition-colors duration-300 text-sm"
>
{item}
</a>
<span className="absolute left-1/2 bottom-[2px] w-full h-[2px] bg-white scale-x-0 group-hover:scale-x-100 transform -translate-x-1/2 origin-center transition-transform duration-300 ease-in-out"></span>
</li>
);
})}
</ul>
</div>
</div>
</div>
);
}
Key Changes:
-
Responsive Visibility: Added hidden lg:flex for the main navigation bar to only show on large screens and above, and used block lg:hidden for a responsive alternative on smaller screens.
-
Icon and Image Scaling: Ensure that the logo or image is appropriately scalable and readable on both small and large screen sizes.
This approach allows the main navigation list to only be visible on larger screens, while an alternative (more compact or differently styled) navigation will be used on smaller screens. You may want to refine the design further by adding specific styles for the alternative navigation on smaller screens, like a burger menu or dropdown functionality.
import { useEffect, useState } from "react";
import { FaShoppingCart } from "react-icons/fa";
import PopUp from "../ShopCartPopUp/PopUP";
export default function Header() {
const [isScrolled, setIsScrolled] = useState(false);
const [isHide, setIsHide] = useState(false);
const [clickAnime, setClickAnim] = useState("left");
const [isClicked, setIsClicked] = useState(false);
const handleHide = () => {
setIsClicked(true);
setTimeout(() => setIsHide(!isHide), 500);
setTimeout(() => setIsClicked(false), 1000);
clickAnime === "left" ? setClickAnim("right") : setClickAnim("left");
};
const headerButtons = [
"Главная",
"Продукты",
"О нас",
"FAQ",
"Новости",
"Контакты",
"Видео",
];
useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 50) {
setIsScrolled(true);
} else {
setIsScrolled(false);
}
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return (
<div
className={fixed top-0 w-full min-h-10 bg-[#222222] text-white z-40 ${ isScrolled ? duration-500 ease-out bg-opacity-70:bg-opacity-100 }}
>
<div
className={w-11/12 ${ isScrolled ? "duration-500 min-h-10 ease-out" : "duration-500 ease-out min-h-24" } flex items-center m-auto}
>
Image
<div className="m-auto hidden lg:flex">
<ul className="flex gap-7">
{headerButtons.map((item, index) => {
return (
<li
key={index}
className="relative group inline-block cursor-pointer"
>
<a
href="#"
className="text-white transition-colors duration-300"
>
{item}
</a>
<span className="absolute left-1/2 bottom-[2px] w-full h-[2px] bg-white scale-x-0 group-hover:scale-x-100 transform -translate-x-1/2 origin-center transition-transform duration-300 ease-in-out"></span>
</li>
);
})}
</ul>
</div>
<div
className={`flex gap-5 ${
isClicked ? `pointer-events-none` : "pointer-events-auto"
}`}
onClick={handleHide}
>
<FaShoppingCart size={"2rem"} />
</div>
</div>
{isHide && (
<PopUp isScrolled={isScrolled} clickAnime={clickAnime}></PopUp>
)}
</div>
);
}
Сделай адаптивную вертку, так чтобы на телефоне можно было по нажатию на кнопку открыть спускающиеся меню со всеми кнопками
To achieve a responsive header with a collapsible menu for mobile devices, you can add a button (commonly referred to as a "hamburger" button) that toggles the visibility of the menu items. Here’s a complete example:
- Add a state to handle menu visibility on mobile.
- Implement a button that toggles this state.
- Adjust the navigation items to show/hide based on this state.
Here's the updated code:
import { useEffect, useState } from "react";
import { FaShoppingCart, FaBars, FaTimes } from "react-icons/fa";
import PopUp from "../ShopCartPopUp/PopUP";
export default function Header() {
const [isScrolled, setIsScrolled] = useState<boolean>(false);
const [isHide, setIsHide] = useState<boolean>(false);
const [clickAnime, setClickAnim] = useState<string>("left");
const [isClicked, setIsClicked] = useState<boolean>(false);
const [isMenuOpen, setIsMenuOpen] = useState<boolean>(false);
const handleHide = () => {
setIsClicked(true);
setTimeout(() => setIsHide(!isHide), 500);
setTimeout(() => setIsClicked(false), 1000);
clickAnime === "left" ? setClickAnim("right") : setClickAnim("left");
};
const toggleMenu = () => {
setIsMenuOpen(!isMenuOpen);
};
const headerButtons = [
"Главная",
"Продукты",
"О нас",
"FAQ",
"Новости",
"Контакты",
"Видео",
];
useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 50) {
setIsScrolled(true);
} else {
setIsScrolled(false);
}
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, []);
return (
<div
className={`fixed top-0 w-full min-h-10 bg-[#222222] text-white z-40 ${
isScrolled ? `duration-500 ease-out bg-opacity-70` : `bg-opacity-100`
}`}
>
<div
className={`w-11/12 ${
isScrolled
? "duration-500 min-h-10 ease-out"
: "duration-500 ease-out min-h-24"
} flex items-center m-auto`}
>
<div className="">Image</div>
<div className="m-auto hidden lg:flex">
<ul className="flex gap-7">
{headerButtons.map((item, index) => {
return (
<li
key={index}
className="relative group inline-block cursor-pointer"
>
<a
href="#"
className="text-white transition-colors duration-300"
>
{item}
</a>
<span className="absolute left-1/2 bottom-[2px] w-full h-[2px] bg-white scale-x-0 group-hover:scale-x-100 transform -translate-x-1/2 origin-center transition-transform duration-300 ease-in-out"></span>
</li>
);
})}
</ul>
</div>
<div className="block lg:hidden">
<button onClick={toggleMenu} className="text-white focus:outline-none">
{isMenuOpen ? <FaTimes size={24} /> : <FaBars size={24} />}
</button>
</div>
<div
className={`flex gap-5 ${
isClicked ? `pointer-events-none` : "pointer-events-auto"
}`}
onClick={handleHide}
>
<FaShoppingCart size={"2rem"} />
</div>
</div>
{isHide && (
<PopUp isScrolled={isScrolled} clickAnime={clickAnime}></PopUp>
)}
{isMenuOpen && (
<div className="lg:hidden bg-[#222222] text-white w-full py-5">
<ul className="flex flex-col items-center gap-3">
{headerButtons.map((item, index) => (
<li
key={index}
className="relative group inline-block cursor-pointer"
>
<a
href="#"
className="text-white text-lg transition-colors duration-300"
onClick={() => setIsMenuOpen(false)} // Close menu on click
>
{item}
</a>
<span className="absolute left-1/2 bottom-[2px] w-full h-[2px] bg-white scale-x-0 group-hover:scale-x-100 transform -translate-x-1/2 origin-center transition-transform duration-300 ease-in-out"></span>
</li>
))}
</ul>
</div>
)}
</div>
);
}
Key Adjustments:
- State for Menu Visibility: Introduced
isMenuOpen state to handle the toggling of the menu on mobile devices.
- Hamburger Button: Added a button that toggles the
isMenuOpen state. This button shows a hamburger icon (FaBars) when the menu is closed and a close icon (FaTimes) when the menu is open.
- Responsive Menu: Wrapped the navigation items inside a container that toggles visibility based on
isMenuOpen. This container is only visible on screens smaller than lg.
Now, when viewed on a mobile device, the header will include a button that can toggle the visibility of the navigation menu.