improved interface tab transitions

This commit is contained in:
SethBurkart123
2023-09-25 11:21:07 +10:00
parent 3e805f91fb
commit 745f7058c1
+20 -4
View File
@@ -1,5 +1,5 @@
import React, { useState, useRef, useEffect } from 'react'; import React, { useState, useRef, useEffect } from 'react';
import { motion } from 'framer-motion'; import { motion, AnimatePresence } from 'framer-motion';
import type { TabbedContainerProps } from '../types/TabbedContainerProps'; import type { TabbedContainerProps } from '../types/TabbedContainerProps';
import { useSettingsContext } from '../SettingsContext'; import { useSettingsContext } from '../SettingsContext';
@@ -21,6 +21,13 @@ const TabbedContainer: React.FC<TabbedContainerProps> = ({ tabs }) => {
const springTransition = { type: 'spring', stiffness: 250, damping: 25 }; const springTransition = { type: 'spring', stiffness: 250, damping: 25 };
const contentVariants = {
hidden: { opacity: 0 },
visible: { opacity: 1 },
};
const fastOpacityTransition = { duration: 0.2 };
useEffect(() => { useEffect(() => {
if (containerRef.current) { if (containerRef.current) {
// @ts-expect-error for some reason its giving an error in TS but it works... // @ts-expect-error for some reason its giving an error in TS but it works...
@@ -67,14 +74,23 @@ const TabbedContainer: React.FC<TabbedContainerProps> = ({ tabs }) => {
transition={springTransition} transition={springTransition}
> >
<div className="absolute flex w-full" style={{ left: `${-position}%` }}> <div className="absolute flex w-full" style={{ left: `${-position}%` }}>
<AnimatePresence>
{tabs.map((tab, index) => ( {tabs.map((tab, index) => (
<div activeTab === index && (
<motion.div
key={index} key={index}
className={`w-full ${activeTab === index ? '' : 'hidden'}`} className="absolute w-full"
initial="hidden"
animate="visible"
exit="hidden"
transition={fastOpacityTransition}
variants={contentVariants}
> >
{tab.content} {tab.content}
</div> </motion.div>
)
))} ))}
</AnimatePresence>
</div> </div>
</motion.div> </motion.div>
</div> </div>