Skip to content

Commit

Permalink
fix: IntersectionObserver error
Browse files Browse the repository at this point in the history
  • Loading branch information
kausko committed Nov 11, 2021
1 parent 11e3799 commit 3ee816a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
16 changes: 4 additions & 12 deletions src/Experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Avatar, Card, CardActionArea, CardHeader, Fade, Grid, Hidden, makeStyle
import Image from 'next/image'
import { DateRange, LocationCity } from '@material-ui/icons';
import { experience } from '../data.json'
import { useEffect, useRef, useState } from "react";
import { useRef } from "react";
import useAnimate from "./useAnimate";

const useStyles = makeStyles(theme => ({
cont: {
Expand Down Expand Up @@ -60,17 +61,8 @@ export default function Experience() {
const align = mdDown ? "center" : "flex-end"
const textAlign = mdDown ? "center" : "right"

const [animate, setAnimate] = useState(false)
const animRef = useRef()

useEffect(() => {
const observer = new IntersectionObserver(entries => {
if (entries.some(entry => entry.isIntersecting))
setAnimate(true)
})
observer.observe(animRef.current)
return () => observer.unobserve(animRef.current)
}, [])
const animRef = useRef(null)
const animate = useAnimate(animRef)

return (
<Grid direction="row" container justify="center" alignItems="center" spacing={10} className={classes.cont}>
Expand Down
16 changes: 4 additions & 12 deletions src/Projects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Card, CardActionArea, CardActions, CardContent, CardHeader, Chip, Fade, Grid, Hidden, makeStyles, Typography } from "@material-ui/core";
import { RepoForkedIcon, RepoIcon, StarIcon } from '@primer/octicons-react';
import Image from 'next/image'
import { useEffect, useRef, useState } from "react";
import { useRef } from "react";
import useAnimate from "./useAnimate";

const useStyles = makeStyles(theme => ({
cont: {
Expand All @@ -20,17 +21,8 @@ export default function Projects({ data }) {

const classes = useStyles()

const [animate, setAnimate] = useState(false)
const animRef = useRef()

useEffect(() => {
const observer = new IntersectionObserver(entries => {
if (entries.some(entry => entry.isIntersecting))
setAnimate(true)
})
observer.observe(animRef.current)
return () => observer.unobserve(animRef.current)
}, [])
const animRef = useRef(null)
const animate = useAnimate(animRef)

return (
<Grid direction="row-reverse" container justify="center" alignItems="center" spacing={10} className={classes.cont}>
Expand Down
16 changes: 4 additions & 12 deletions src/Skills.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Avatar, Fade, Grid, Hidden, makeStyles, Tooltip, Typography, useMediaQu
import Cancel from "@material-ui/icons/Cancel";
import clsx from "clsx";
import Image from 'next/image'
import { useEffect, useRef, useState } from "react";
import { useRef} from "react";
import simpleIcons from 'simple-icons'
import { skills } from '../data.json'
import useAnimate from "./useAnimate";
import { iconify } from "./util";

const wrapper = (sk = []) => sk.map(v => {
Expand Down Expand Up @@ -58,17 +59,8 @@ export default function Skills() {
const align = mdDown ? "center" : "flex-end"
const textAlign = mdDown ? "center" : "right"

const [animate, setAnimate] = useState(false)
const animRef = useRef()

useEffect(() => {
const observer = new IntersectionObserver(entries => {
if (entries.some(entry => entry.isIntersecting))
setAnimate(true)
})
observer.observe(animRef.current)
return () => observer.unobserve(animRef.current)
}, [])
const animRef = useRef(null)
const animate = useAnimate(animRef)

return (
<Grid container justify="center" alignItems="center" spacing={10} className={classes.cont}>
Expand Down
20 changes: 20 additions & 0 deletions src/useAnimate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useState, useRef, useEffect } from 'react'

export default function useAnimate(ref) {
const [animate, setAnimate] = useState(false);
const observerRef = useRef(null)

useEffect(() => {
observerRef.current = new IntersectionObserver(entries => {
if (entries.some(entry => entry.isIntersecting))
setAnimate(true)
})
}, [])

useEffect(() => {
observerRef.current.observe(ref.current)
return () => observerRef.current.disconnect()
}, [ref])

return animate
}

0 comments on commit 3ee816a

Please sign in to comment.