-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use client' | ||
import React, { useState } from 'react' | ||
import { motion } from 'motion/react' | ||
import clsx from 'clsx' | ||
|
||
const AnimationTabs = ({ tabs: propTabs, containerClassName, activeTabClassName, tabClassName, contentClassName }) => { | ||
const [active, setActive] = useState(propTabs[0]) | ||
const [tabs, setTabs] = useState(propTabs) | ||
|
||
const moveSelectedTabToTop = (idx) => { | ||
const newTabs = [...propTabs] | ||
const selectedTab = newTabs.splice(idx, 1) | ||
newTabs.unshift(selectedTab[0]) | ||
setTabs(newTabs) | ||
setActive(newTabs[0]) | ||
} | ||
|
||
const [hovering, setHovering] = useState(false) | ||
|
||
return ( | ||
<> | ||
<div | ||
className={clsx( | ||
'flex flex-row items-center justify-start [perspective:1000px] relative overflow-auto sm:overflow-visible no-visible-scrollbar max-w-full w-full', | ||
containerClassName | ||
)} | ||
> | ||
{propTabs.map((tab, idx) => ( | ||
<button | ||
key={tab.title} | ||
onClick={() => { | ||
moveSelectedTabToTop(idx) | ||
}} | ||
onMouseEnter={() => setHovering(true)} | ||
onMouseLeave={() => setHovering(false)} | ||
className={clsx('relative px-4 py-2 rounded-full', tabClassName)} | ||
style={{ | ||
transformStyle: 'preserve-3d', | ||
}} | ||
> | ||
{active.value === tab.value && ( | ||
<motion.div | ||
layoutId="clickedButton" | ||
transition={{ type: 'spring', bounce: 0.3, duration: 0.6 }} | ||
className={clsx('absolute inset-0 bg-gray-200 dark:bg-zinc-800 rounded-full', activeTabClassName)} | ||
/> | ||
)} | ||
|
||
<span className="relative block text-black dark:text-white">{tab.title}</span> | ||
</button> | ||
))} | ||
</div> | ||
<FadeInDiv | ||
tabs={tabs} | ||
active={active} | ||
key={active.value} | ||
hovering={hovering} | ||
className={clsx('mt-32', contentClassName)} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export const FadeInDiv = ({ className, tabs, hovering }) => { | ||
const isActive = (tab) => { | ||
return tab.value === tabs[0].value | ||
} | ||
return ( | ||
<div className="relative w-full h-full"> | ||
{tabs.map((tab, idx) => ( | ||
<motion.div | ||
key={tab.value} | ||
layoutId={tab.value} | ||
style={{ | ||
scale: 1 - idx * 0.1, | ||
top: hovering ? idx * -50 : 0, | ||
zIndex: -idx, | ||
opacity: idx < 3 ? 1 - idx * 0.1 : 0, | ||
}} | ||
animate={{ | ||
y: isActive(tab) ? [0, 40, 0] : 0, | ||
}} | ||
className={clsx('w-full h-full absolute top-0 left-0', className)} | ||
> | ||
{tab.content} | ||
</motion.div> | ||
))} | ||
</div> | ||
) | ||
} | ||
|
||
export default AnimationTabs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters