From 8496cae1aa8fd345b31e98424587aa2fd71978f4 Mon Sep 17 00:00:00 2001 From: Ifraim Solomonov Date: Fri, 1 Mar 2024 20:58:48 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=A5=D1=83=D0=BA=20useElementOnScreen,=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=81=D1=82=D0=B8=D0=BB=D0=B5=D0=B9=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B2=D0=B1=D0=B0=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Navbar/Navbar.jsx | 1 - src/components/Navbar/Navbar.styles.scss | 9 +++++-- src/lib/useElementOnScreen.js | 30 ++++++++++++++++++++++++ src/pages/Main/Main.jsx | 22 +++++++++++++++-- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/lib/useElementOnScreen.js diff --git a/src/components/Navbar/Navbar.jsx b/src/components/Navbar/Navbar.jsx index ebbadb5..d383a80 100755 --- a/src/components/Navbar/Navbar.jsx +++ b/src/components/Navbar/Navbar.jsx @@ -1,5 +1,4 @@ import { scb as logo } from '../../assets/images'; -import { NavLink } from "react-router-dom"; import NavbarLink from './NavbarLink'; import './Navbar.styles.scss'; diff --git a/src/components/Navbar/Navbar.styles.scss b/src/components/Navbar/Navbar.styles.scss index 5c1bd01..e71c4cf 100755 --- a/src/components/Navbar/Navbar.styles.scss +++ b/src/components/Navbar/Navbar.styles.scss @@ -1,13 +1,13 @@ @import "../../assets/styles/theme.scss"; .navbar { - position: absolute; + position: fixed; top: 0; width: 80%; height: 60px; max-height: 80px; padding: 1em 10%; - background: transparent; + background: $bg-secondary; z-index: 1; display: flex; @@ -43,4 +43,9 @@ background: $text-secondary; } } +} + +.navbar.absolute { + position: absolute; + background: transparent; } \ No newline at end of file diff --git a/src/lib/useElementOnScreen.js b/src/lib/useElementOnScreen.js new file mode 100644 index 0000000..78084b5 --- /dev/null +++ b/src/lib/useElementOnScreen.js @@ -0,0 +1,30 @@ +import { useRef, useEffect, useState } from "react"; + +const useElementOnScreen = (options) => { + + const containerRef = useRef(null); + const [isVisible, setIsVisible] = useState(false); + + const observerCallback = (entries) => { + const [entry] = entries; + setIsVisible(entry.isIntersecting); + }; + + useEffect(() => { + const observer = new IntersectionObserver(observerCallback, options); + + if (containerRef.current) { + observer.observe(containerRef.current); + } + + return () => { + if (containerRef.current) { + observer.unobserve(containerRef.current); + } + } + }, [containerRef, options]) + + return [containerRef, isVisible]; +} + +export default useElementOnScreen; \ No newline at end of file diff --git a/src/pages/Main/Main.jsx b/src/pages/Main/Main.jsx index 82b017b..40d021b 100755 --- a/src/pages/Main/Main.jsx +++ b/src/pages/Main/Main.jsx @@ -1,11 +1,29 @@ +import { useEffect } from 'react'; + import { News, Projects, Labs, Partners, Gallery, ContactUs } from './components'; +import useElementOnScreen from '../../lib/useElementOnScreen'; function Main() { + const [containerRef, isVisible] = useElementOnScreen({ + root: null, + rootMargin: '0px', + threshold: 0, + }); + + useEffect(() => { + const [ navbar ] = document.getElementsByClassName('navbar'); + if (isVisible) { + navbar.classList.add('absolute'); + } else { + navbar.classList.remove('absolute'); + } + }, [isVisible]); + return ( <> -
+

Думай иначе, будь креативным!

-

Студенческое конструкторское бюро
+

Студенческое конструкторское бюро
"Компьютерное инновационное творчество"

Связаться с нами
From ce0752c7eb2f6acf6db1a5b60f42b7b33c1be867 Mon Sep 17 00:00:00 2001 From: Ifraim Solomonov Date: Mon, 4 Mar 2024 11:38:08 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B1=D1=80=D0=BE?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=20useEffect=20=D0=B8=D0=B7=20Main=20=D0=B2?= =?UTF-8?q?=20Root=20=20=D0=B8=20=D0=BE=D1=84=D0=BE=D1=80=D0=BC=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BA=D0=B0=D0=BA=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D0=BA=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Main/Main.jsx | 31 ++++++++++------------------ src/pages/Main/Main.styles.scss | 36 +++++++++++++++++++++++++++++++++ src/pages/Root/Root.jsx | 27 ++++++++++++++++++++++--- src/pages/Root/Root.styles.scss | 31 ---------------------------- src/pages/Root/index.js | 2 +- 5 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 src/pages/Main/Main.styles.scss diff --git a/src/pages/Main/Main.jsx b/src/pages/Main/Main.jsx index 40d021b..36ccfc8 100755 --- a/src/pages/Main/Main.jsx +++ b/src/pages/Main/Main.jsx @@ -1,31 +1,22 @@ -import { useEffect } from 'react'; +import { useContext } from 'react'; import { News, Projects, Labs, Partners, Gallery, ContactUs } from './components'; -import useElementOnScreen from '../../lib/useElementOnScreen'; +import { RootContext } from '../Root'; + +import './Main.styles.scss'; function Main() { - const [containerRef, isVisible] = useElementOnScreen({ - root: null, - rootMargin: '0px', - threshold: 0, - }); - - useEffect(() => { - const [ navbar ] = document.getElementsByClassName('navbar'); - if (isVisible) { - navbar.classList.add('absolute'); - } else { - navbar.classList.remove('absolute'); - } - }, [isVisible]); + const { containerRef } = useContext(RootContext); return ( <>
-

Думай иначе, будь креативным!

-

Студенческое конструкторское бюро
- "Компьютерное инновационное творчество"

- Связаться с нами +
+

Думай иначе, будь креативным!

+

Студенческое конструкторское бюро
+ "Компьютерное инновационное творчество"

+ Связаться с нами +
diff --git a/src/pages/Main/Main.styles.scss b/src/pages/Main/Main.styles.scss new file mode 100644 index 0000000..84a7d9b --- /dev/null +++ b/src/pages/Main/Main.styles.scss @@ -0,0 +1,36 @@ +@import "../../assets/styles/theme.scss"; + +#mainpageheader { + position: relative; + + height: 100vh; + padding: 0 10%; + + color: $text-secondary; + background-image: url(/assets/images/car.jpeg); + background-color: $bg-image; + background-blend-mode: multiply; + background-position: center; + background-size: cover; + + .container { + position: absolute; + top: 40%; + display: flex; + flex-direction: column; + + + h1 { + font-size: 60px; + font-weight: 600; + line-height: 63px; + margin: 0; + } + + h2 { + font-size: 24px; + font-weight: 400; + line-height: 36px; + } + } +} \ No newline at end of file diff --git a/src/pages/Root/Root.jsx b/src/pages/Root/Root.jsx index f3d0cc9..e07af02 100755 --- a/src/pages/Root/Root.jsx +++ b/src/pages/Root/Root.jsx @@ -1,25 +1,46 @@ import { Outlet, useLoaderData } from "react-router-dom"; +import { createContext, useEffect } from 'react'; import { getContacts } from "../../api"; +import useElementOnScreen from '../../lib/useElementOnScreen'; import Navbar from '../../components/Navbar'; import Footer from "../../components/Footer"; import './Root.styles.scss'; +export const RootContext = createContext(); + async function loader() { const contacts = await getContacts(); return { contacts }; } function Root() { - const { contacts } = useLoaderData(); + + const [containerRef, isVisible] = useElementOnScreen({ + root: null, + rootMargin: '0px', + threshold: 0, + }); + + useEffect(() => { + const [navbar] = document.getElementsByClassName('navbar'); + if (isVisible) { + navbar.classList.add('absolute'); + } else { + navbar.classList.remove('absolute'); + } + }, [isVisible]); + return ( - <> +