Skip to content

Commit 9b0085a

Browse files
committed
add useIntersect hook to card component
1 parent 2f1e5bb commit 9b0085a

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

components/CardsColumns.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { useEffect, useState } from 'react';
12
import Card from './Card';
23
import Container from './Container';
34
import styles from '../styles/CardsColumns.module.scss';
5+
import { useIntersect } from '../hooks/useIntersect';
46

57
export default function CardsColumns({
68
images,
@@ -10,9 +12,23 @@ export default function CardsColumns({
1012
links,
1113
linkText,
1214
}) {
15+
const [ref, entry] = useIntersect({ threshold: 0.15 });
16+
const [firstLoad, setFirstLoad] = useState(true);
17+
const [hiddenStyle, setHiddenStyle] = useState('section__hidden');
18+
19+
useEffect(() => {
20+
if (entry.isIntersecting && firstLoad) {
21+
setHiddenStyle('');
22+
setFirstLoad(false);
23+
}
24+
}, [entry.isIntersecting]);
25+
1326
return (
1427
<Container>
15-
<div className={styles.inner__content}>
28+
<div
29+
ref={ref}
30+
className={`${styles.inner__content} ${styles[hiddenStyle]}`}
31+
>
1632
{titles.map((title, index) => {
1733
return (
1834
<Card

components/TwoColumn.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ export default function TwoColumn({
3535
if (entry.isIntersecting && firstLoad) {
3636
setHiddenStyle('');
3737
setFirstLoad(false);
38-
console.log(
39-
title,
40-
entry.isIntersecting,
41-
entry.intersectionRatio,
42-
hiddenStyle
43-
);
4438
}
4539
}, [entry.isIntersecting]);
4640

hooks/useIntersect.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ export const useIntersect = ({ root = null, rootMargin, threshold = 0 }) => {
77
const observer = useRef(null);
88

99
useEffect(() => {
10-
//if (observer.current) observer.current.disconnect();
11-
1210
observer = new window.IntersectionObserver(
1311
([entry]) => {
1412
setEntry(entry);
1513
},
1614
{ root, rootMargin, threshold }
1715
);
1816

19-
//const { current: currentObserver } = observer;
2017
if (node) {
2118
observer.observe(node);
2219
}

styles/CardsColumns.module.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
@use './variables' as *;
22
@use './mixins' as *;
33

4+
.section__hidden {
5+
opacity: 0;
6+
transform: translateY(8rem);
7+
}
8+
49
.inner__content {
510
margin: 0 auto;
611
padding-bottom: 12rem;
712
justify-content: center;
13+
transition: transform 1s, opacity 1s;
814

915
@include desktop-breakpoint-plus {
1016
display: flex;

0 commit comments

Comments
 (0)