"use client" import { useState, useRef, useEffect } from "react" const tabs = ["Новинки", "Коллекции", "Популярное"] interface TabSelectorProps { onTabChange: (index: number) => void; } export default function TabSelector({ onTabChange }: TabSelectorProps) { const [hoveredIndex, setHoveredIndex] = useState(null) const [activeIndex, setActiveIndex] = useState(0) const [hoverStyle, setHoverStyle] = useState({}) const [activeStyle, setActiveStyle] = useState({ left: "0px", width: "0px" }) const tabRefs = useRef<(HTMLDivElement | null)[]>([]) useEffect(() => { if (hoveredIndex !== null) { const hoveredElement = tabRefs.current[hoveredIndex] if (hoveredElement) { const { offsetLeft, offsetWidth } = hoveredElement setHoverStyle({ left: `${offsetLeft}px`, width: `${offsetWidth}px`, }) } } }, [hoveredIndex]) useEffect(() => { const activeElement = tabRefs.current[activeIndex] if (activeElement) { const { offsetLeft, offsetWidth } = activeElement setActiveStyle({ left: `${offsetLeft}px`, width: `${offsetWidth}px`, }) } // Вызываем функцию обратного вызова при изменении активного таба onTabChange(activeIndex); }, [activeIndex, onTabChange]) useEffect(() => { requestAnimationFrame(() => { const firstElement = tabRefs.current[0] if (firstElement) { const { offsetLeft, offsetWidth } = firstElement setActiveStyle({ left: `${offsetLeft}px`, width: `${offsetWidth}px`, }) } }) }, []) return (
{/* Hover Highlight */}
{/* Active Indicator */}
{/* Tabs */}
{tabs.map((tab, index) => (
(tabRefs.current[index] = el)} className={`px-3 py-2 cursor-pointer transition-colors duration-300 h-[30px] ${ index === activeIndex ? "text-[#2B5F47] font-medium" : "text-[#2B5F47]/70" }`} onMouseEnter={() => setHoveredIndex(index)} onMouseLeave={() => setHoveredIndex(null)} onClick={() => setActiveIndex(index)} >
{tab}
))}
) }