Skip to content

Commit 2f1e5bb

Browse files
committed
add intersect observer custom hook and implement on TowColumn component, add translateY effect on reveal
1 parent 5e4d73f commit 2f1e5bb

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

components/TwoColumn.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { useEffect, useState } from 'react';
12
import Image from 'next/image';
23
import ButtonLink from './ButtonLink';
34
import Container from './Container';
45
import styles from '../styles/TwoColumn.module.scss';
6+
import { useIntersect } from '../hooks/useIntersect';
57

68
export default function TwoColumn({
79
image,
@@ -25,8 +27,29 @@ export default function TwoColumn({
2527
},
2628
};
2729

30+
const [ref, entry] = useIntersect({ threshold: 0.15 });
31+
const [firstLoad, setFirstLoad] = useState(true);
32+
const [hiddenStyle, setHiddenStyle] = useState('section__hidden');
33+
34+
useEffect(() => {
35+
if (entry.isIntersecting && firstLoad) {
36+
setHiddenStyle('');
37+
setFirstLoad(false);
38+
console.log(
39+
title,
40+
entry.isIntersecting,
41+
entry.intersectionRatio,
42+
hiddenStyle
43+
);
44+
}
45+
}, [entry.isIntersecting]);
46+
2847
return (
29-
<section className={styles.wrapper} style={styleProps.wrapper}>
48+
<section
49+
ref={ref}
50+
className={`${styles.wrapper} ${styles[hiddenStyle]}`}
51+
style={styleProps.wrapper}
52+
>
3053
<Container
3154
className={`${styles.inner} ${styles[customInnerClass]}`}
3255
styles={{ flexDirection: rowOrder }}

hooks/useIntersect.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useState, useEffect, useRef } from 'react';
2+
3+
export const useIntersect = ({ root = null, rootMargin, threshold = 0 }) => {
4+
const [entry, setEntry] = useState({});
5+
const [node, setNode] = useState(null);
6+
7+
const observer = useRef(null);
8+
9+
useEffect(() => {
10+
//if (observer.current) observer.current.disconnect();
11+
12+
observer = new window.IntersectionObserver(
13+
([entry]) => {
14+
setEntry(entry);
15+
},
16+
{ root, rootMargin, threshold }
17+
);
18+
19+
//const { current: currentObserver } = observer;
20+
if (node) {
21+
observer.observe(node);
22+
}
23+
return () => {
24+
observer.disconnect();
25+
if (node) {
26+
observer.unobserve(node);
27+
}
28+
};
29+
}, [node, root, rootMargin, threshold]);
30+
31+
return [setNode, entry];
32+
};

pages/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useRef, useEffect, useState } from 'react';
12
import Bracket from '../components/Bracket';
23
import Stick from '../components/Stick';
34
import TwoColumn from '../components/TwoColumn';
@@ -6,6 +7,8 @@ import styles from '../styles/Home.module.scss';
67
import { white, primary } from '../styles/TwoColumn.module.scss';
78

89
export default function Home() {
10+
const aboutIsVisible = true; //temp
11+
912
return (
1013
<>
1114
<TwoColumn
@@ -15,7 +18,6 @@ export default function Home() {
1518
link="/about"
1619
customBtnClass="inverted-grey"
1720
/>
18-
1921
<Bracket className={styles.bracket} />
2022

2123
<TwoColumn

styles/Home.module.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.bracket {
1818
left: 0;
1919
position: absolute;
20+
z-index: 2;
2021
transform: translate(10rem, -5rem);
2122

2223
@include desktop-breakpoint-plus {
@@ -32,6 +33,7 @@
3233
.stick {
3334
right: 0;
3435
position: absolute;
36+
z-index: 2;
3537
transform: translate(-10rem, -5rem);
3638

3739
@include desktop-breakpoint-plus {

styles/TwoColumn.module.scss

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

4+
.section__hidden {
5+
opacity: 0;
6+
transform: translateY(8rem);
7+
}
8+
49
.wrapper {
510
background-color: $light-bg-color;
11+
transition: transform 1s, opacity 1s;
612

713
.inner {
814
padding: 5rem 0;

0 commit comments

Comments
 (0)