-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnimatedLayout.tsx
More file actions
83 lines (75 loc) · 1.93 KB
/
Copy pathAnimatedLayout.tsx
File metadata and controls
83 lines (75 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { type FC, useEffect, useRef, useState } from 'react';
import { AnimatePresence, type Variants, motion, useInView } from 'motion/react';
import { diy, fashion, food } from './images';
import Masonry from '../../../src';
import Card from './AnimatedCard';
const variants: Variants = {
inital: {
opacity: 0,
y: 10,
},
animate: {
opacity: 1,
y: 0,
transition: {
duration: 1,
bounce: 0,
delay: 1,
},
},
exit: {
opacity: 0,
y: -10,
transition: {
duration: 0.8,
},
},
};
const collections = [food, fashion, diy];
const AnimatedLayout: FC = () => {
const [currentCollection, setCurrentCollection] = useState(0);
const ref = useRef(null);
const inView = useInView(ref);
useEffect(() => {
const timeIndex = setInterval(() => {
if (!inView) {
return;
}
setCurrentCollection((prev) => {
const nextIndex = prev + 1;
if (nextIndex === collections.length) {
return 0;
}
return nextIndex;
});
}, 5000);
return () => clearInterval(timeIndex);
}, [inView]);
return (
<div className="pt-16" ref={ref}>
{collections.map((collection, index) => (
<AnimatePresence key={`Collection__${index}`} mode="popLayout">
{currentCollection === index && (
<Masonry
columns={{ 640: 1, 768: 2, 1024: 3, 1280: 4, 1536: 5 }}
as={motion.div}
variants={variants}
initial="initial"
animate={inView ? 'animate' : false}
exit="exit"
className="gap-6"
columnProps={{
className: 'gap-y-8',
}}
>
{collection.map((imageUrl) => (
<Card url={imageUrl} key={imageUrl} />
))}
</Masonry>
)}
</AnimatePresence>
))}
</div>
);
};
export default AnimatedLayout;