diff --git a/src/components/GroupSwitch.tsx b/src/components/GroupSwitch.tsx index 144b684..9446785 100644 --- a/src/components/GroupSwitch.tsx +++ b/src/components/GroupSwitch.tsx @@ -1,5 +1,6 @@ import { cn } from "@/lib/utils" import { m } from "framer-motion" +import { createRef, useEffect, useRef } from "react" export default function GroupSwitch({ tabs, @@ -13,16 +14,52 @@ export default function GroupSwitch({ const customBackgroundImage = // @ts-expect-error CustomBackgroundImage is a global variable (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined + + const scrollRef = useRef(null) + const tagRefs = useRef(tabs.map(() => createRef())) + + useEffect(() => { + const container = scrollRef.current + if (!container) return + + const isOverflowing = container.scrollWidth > container.clientWidth + if (!isOverflowing) return + + const onWheel = (e: WheelEvent) => { + e.preventDefault() + container.scrollLeft += e.deltaY + } + + container.addEventListener("wheel", onWheel, { passive: false }) + + return () => { + container.removeEventListener("wheel", onWheel) + } + }, []) + + useEffect(() => { + const currentTagRef = tagRefs.current[tabs.indexOf(currentTab)] + + if (currentTagRef && currentTagRef.current) { + currentTagRef.current.scrollIntoView({ + behavior: "smooth", + block: "nearest", + inline: "center", + }) + } + }, [currentTab]) + return ( -
+
- {tabs.map((tab: string) => ( + {tabs.map((tab: string, index: number) => (
setCurrentTab(tab)} className={cn( "relative cursor-pointer rounded-3xl px-2.5 py-[8px] text-[13px] font-[600] transition-all duration-500",