forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadInView.js
More file actions
46 lines (40 loc) · 1.03 KB
/
LoadInView.js
File metadata and controls
46 lines (40 loc) · 1.03 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
import React, { useEffect, useState } from 'react';
import { useInView } from 'react-hook-inview';
import { motion } from 'framer-motion';
const LoadInView = ({ children }) => {
const [ref, inView] = useInView({ unobserveOnEnter: true });
const [animations, setAnimations] = useState(true);
useEffect(() => {
const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
if (mediaQuery.matches) setAnimations(false);
}, []);
if (!animations) return children;
return (
<div ref={ref}>
{inView ? (
<motion.section
initial={{ opacity: 0, y: 50 }}
animate={{ opacity: 1, y: 0 }}
transition={{
duration: 0.4,
ease: 'easeInOut',
}}
css={`
margin-bottom: 8rem;
`}
>
{children}
</motion.section>
) : (
<div
css={`
visibility: hidden;
`}
>
{children}
</div>
)}
</div>
);
};
export default LoadInView;