1"use client"
2
3import { ReactNode, useMemo, useState } from "react"
4import { AnimatePresence, motion, MotionConfig } from "motion/react"
5import useMeasure from "react-use-measure"
6
7import { cn } from "@/lib/utils"
8
9type Tab = {
10 id: number
11 label: string
12 content: ReactNode
13}
14
15interface OgImageSectionProps {
16 tabs: Tab[]
17 className?: string
18 rounded?: string
19 onChange?: () => void
20}
21
22function DirectionAwareTabs({
23 tabs,
24 className,
25 rounded,
26 onChange,
27}: OgImageSectionProps) {
28 const [activeTab, setActiveTab] = useState(0)
29 const [direction, setDirection] = useState(0)
30 const [isAnimating, setIsAnimating] = useState(false)
31 const [ref, bounds] = useMeasure()
32
33 const content = useMemo(() => {
34 const activeTabContent = tabs.find((tab) => tab.id === activeTab)?.content
35 return activeTabContent || null
36 }, [activeTab, tabs])
37
38 const handleTabClick = (newTabId: number) => {
39 if (newTabId !== activeTab && !isAnimating) {
40 const newDirection = newTabId > activeTab ? 1 : -1
41 setDirection(newDirection)
42 setActiveTab(newTabId)
43 onChange ? onChange() : null
44 }
45 }
46
47 const variants = {
48 initial: (direction: number) => ({
49 x: 300 * direction,
50 opacity: 0,
51 filter: "blur(4px)",
52 }),
53 active: {
54 x: 0,
55 opacity: 1,
56 filter: "blur(0px)",
57 },
58 exit: (direction: number) => ({
59 x: -300 * direction,
60 opacity: 0,
61 filter: "blur(4px)",
62 }),
63 }
64
65 return (
66 <div className=" flex flex-col items-center w-full">
67 <div
68 className={cn(
69 "flex space-x-1 border border-none rounded-full cursor-pointer bg-neutral-600 px-[3px] py-[3.2px] shadow-inner-shadow",
70 className,
71 rounded
72 )}
73 >
74 {tabs.map((tab) => (
75 <button
76 key={tab.id}
77 onClick={() => handleTabClick(tab.id)}
78 className={cn(
79 "relative rounded-full px-3.5 py-1.5 text-xs sm:text-sm font-medium text-neutral-200 transition focus-visible:outline-1 focus-visible:ring-1 focus-visible:outline-none flex gap-2 items-center ",
80 activeTab === tab.id
81 ? "text-white"
82 : "hover:text-neutral-300/60 text-neutral-200/80",
83 rounded
84 )}
85 style={{ WebkitTapHighlightColor: "transparent" }}
86 >
87 {activeTab === tab.id && (
88 <motion.span
89 layoutId="bubble"
90 className="absolute inset-0 z-10 bg-neutral-700 mix-blend-difference shadow-inner-shadow border border-white/10"
91 style={rounded ? { borderRadius: 9 } : { borderRadius: 9999 }}
92 transition={{ type: "spring", bounce: 0.19, duration: 0.4 }}
93 />
94 )}
95
96 {tab.label}
97 </button>
98 ))}
99 </div>
100 <MotionConfig transition={{ duration: 0.4, type: "spring", bounce: 0.2 }}>
101 <motion.div
102 className="relative mx-auto w-full h-full overflow-hidden"
103 initial={false}
104 animate={{ height: bounds.height }}
105 >
106 <div className="p-1" ref={ref}>
107 <AnimatePresence
108 custom={direction}
109 mode="popLayout"
110 onExitComplete={() => setIsAnimating(false)}
111 >
112 <motion.div
113 key={activeTab}
114 variants={variants}
115 initial="initial"
116 animate="active"
117 exit="exit"
118 custom={direction}
119 onAnimationStart={() => setIsAnimating(true)}
120 onAnimationComplete={() => setIsAnimating(false)}
121 >
122 {content}
123 </motion.div>
124 </AnimatePresence>
125 </div>
126 </motion.div>
127 </MotionConfig>
128 </div>
129 )
130}
131export { DirectionAwareTabs }
132