From 1607c136d8a285ea7a323b50921148c588e41362 Mon Sep 17 00:00:00 2001 From: dertyp7 Date: Sun, 14 Jan 2024 13:00:47 +0100 Subject: [PATCH] Refactor scroll functionality in App component --- src/App.tsx | 4 +- src/components/ScrollToTop.tsx | 56 ------------------------- src/components/SectionScroll.tsx | 62 ++++++++++++++++++++++++++++ src/styles/ScrollToTop.scss | 70 -------------------------------- src/styles/SectionScroll.scss | 43 ++++++++++++++++++++ 5 files changed, 107 insertions(+), 128 deletions(-) delete mode 100644 src/components/ScrollToTop.tsx create mode 100644 src/components/SectionScroll.tsx delete mode 100644 src/styles/ScrollToTop.scss create mode 100644 src/styles/SectionScroll.scss diff --git a/src/App.tsx b/src/App.tsx index 9bbcf61..bf03175 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,7 @@ import { useEffect, useRef } from "react"; import "@styles/App.scss"; import Header from "@components/Header"; import About from "@components/About"; -import ScrollToTop from "@components/ScrollToTop"; +import SectionScroll from "@components/SectionScroll"; import Skills from "@components/Skills"; import SectionLine from "@components/SectionLine"; @@ -68,7 +68,7 @@ export default function App() {

Website under construction 🚧👷

- + ); } diff --git a/src/components/ScrollToTop.tsx b/src/components/ScrollToTop.tsx deleted file mode 100644 index 6fe54d7..0000000 --- a/src/components/ScrollToTop.tsx +++ /dev/null @@ -1,56 +0,0 @@ -import { useState, useEffect } from "react"; -import "@styles/ScrollToTop.scss"; - -export default function ScrollToTop() { - const [isScrolledUp, setIsScrolledUp] = useState(false); - - const toggleVisibilityAndDirection = () => { - if (window.scrollY < 100) { - setIsScrolledUp(false); - } else { - setIsScrolledUp(window.scrollY === 0 ? false : true); - } - }; - - const scrollTo = () => { - if (isScrolledUp) { - window.scrollTo({ - top: 0, - behavior: "smooth", - }); - } else { - const skillsElement = document.getElementById("skills"); - if (skillsElement) { - skillsElement.scrollIntoView({ behavior: "smooth" }); - } - } - }; - - useEffect(() => { - window.addEventListener("scroll", toggleVisibilityAndDirection); - return () => - window.removeEventListener("scroll", toggleVisibilityAndDirection); - }, []); - - return ( -
-
- - - - -
-
- ); -} diff --git a/src/components/SectionScroll.tsx b/src/components/SectionScroll.tsx new file mode 100644 index 0000000..f8cc7ba --- /dev/null +++ b/src/components/SectionScroll.tsx @@ -0,0 +1,62 @@ +import { useState } from "react"; +import "@styles/SectionScroll.scss"; + +export default function SectionScroll({ sections }: { sections: string[] }) { + const [currentSectionIndex, setCurrentSectionIndex] = useState(0); + + function scrollTo(direction: number) { + let newIndex = currentSectionIndex + direction; + if (newIndex < 0) newIndex = sections.length - 1; + if (newIndex >= sections.length) newIndex = 0; + + const sectionElement = document.getElementById(sections[newIndex]); + if (sectionElement) { + sectionElement.scrollIntoView({ behavior: "smooth" }); + } + + setCurrentSectionIndex(newIndex); + } + + return ( +
+ {currentSectionIndex > 0 && ( +
scrollTo(-1)}> + + + + +
+ )} + {currentSectionIndex < sections.length - 1 && ( +
scrollTo(1)}> + + + + +
+ )} +
+ ); +} diff --git a/src/styles/ScrollToTop.scss b/src/styles/ScrollToTop.scss deleted file mode 100644 index 2109884..0000000 --- a/src/styles/ScrollToTop.scss +++ /dev/null @@ -1,70 +0,0 @@ -.scrollToTop { - position: fixed; - bottom: 20px; - right: 20px; - cursor: pointer; - - .arrow { - &.up { - transform: rotate(0deg); - - &:hover { - animation: pulseUp 1s 2; - } - - @media screen and (min-width: 768px) { - &:hover { - animation: pulseUp 1s infinite; - } - } - } - - &.down { - transform: rotate(-180deg); - - &:hover { - animation: pulseDown 1s 2; - } - - @media screen and (min-width: 768px) { - width: 50px; - height: 50px; - position: fixed; - bottom: 15px; - right: 50%; - - &:hover { - animation: pulseDown 1s infinite; - } - } - } - } - - &:hover { - color: var(--color-accent); - } - - @keyframes pulseUp { - 0% { - transform: scale(1); - } - 50% { - transform: scale(1.4); - } - 100% { - transform: scale(1); - } - } - - @keyframes pulseDown { - 0% { - transform: scale(1) rotate(-180deg); - } - 50% { - transform: scale(1.4) rotate(-180deg); - } - 100% { - transform: scale(1) rotate(-180deg); - } - } -} diff --git a/src/styles/SectionScroll.scss b/src/styles/SectionScroll.scss new file mode 100644 index 0000000..6dd2756 --- /dev/null +++ b/src/styles/SectionScroll.scss @@ -0,0 +1,43 @@ +.sectionScroll { + position: fixed; + bottom: 20px; + right: 20px; + cursor: pointer; + + div { + .arrow { + &.up { + &:hover { + animation: pulse 1s 2; + } + + @media screen and (min-width: 768px) { + &:hover { + animation: pulse 1s infinite; + } + } + } + + &.down { + &:hover { + animation: pulse 1s 2; + } + } + &:hover { + color: var(--color-accent); + } + } + } + + @keyframes pulse { + 0% { + transform: scale(1); + } + 50% { + transform: scale(1.4); + } + 100% { + transform: scale(1); + } + } +}