From d8d03d5e77c9e2aad30f4ce858b11701e25ce0a1 Mon Sep 17 00:00:00 2001 From: Ifraim Solomonov Date: Tue, 5 Mar 2024 16:30:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BC=D0=B5=D1=85=D0=B0=D0=BD=D0=B8=D0=B7=D0=BC=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=D1=8F=20=D1=81=D1=82=D0=B8=D0=BB=D0=B5=D0=B9,=20=D1=82?= =?UTF-8?q?=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BD=D0=B5=20=D0=BE=D1=82?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D0=B2=D0=B0=D0=B5=D1=82=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 4 +++- package-lock.json | 21 +++++++++++++++++++++ package.json | 1 + src/lib/useElementOnScreen.js | 16 ++++++++-------- src/pages/Main/Main.jsx | 6 +++--- src/pages/Root/Root.jsx | 16 +++++++++++++--- 6 files changed, 49 insertions(+), 15 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 446c5b9..f7d0565 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,10 +1,12 @@ { "configurations": [ + { "type": "chrome", "name": "http://localhost:3000/", "request": "launch", - "url": "http://localhost:3000/" + "url": "http://localhost:3000/", + "runtimeExecutable": "C:\\Users\\mrifr\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe" } ] } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 998b45d..26d477d 100755 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "reactpress": "^1.0.0", "sass": "^1.70.0", "scroll-carousel-react": "^1.0.0", + "use-callback-ref": "^1.3.1", "web-vitals": "^2.1.4", "wpapi": "^1.2.2" } @@ -17357,6 +17358,26 @@ "requires-port": "^1.0.0" } }, + "node_modules/use-callback-ref": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/use-callback-ref/-/use-callback-ref-1.3.1.tgz", + "integrity": "sha512-Lg4Vx1XZQauB42Hw3kK7JM6yjVjgFmFC5/Ab797s79aARomD2nEErc4mCgM8EZrARLmmbWpi5DGCadmK50DcAQ==", + "dependencies": { + "tslib": "^2.0.0" + }, + "engines": { + "node": ">=10" + }, + "peerDependencies": { + "@types/react": "^16.8.0 || ^17.0.0 || ^18.0.0", + "react": "^16.8.0 || ^17.0.0 || ^18.0.0" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index 2d5d823..2cbfeba 100755 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "reactpress": "^1.0.0", "sass": "^1.70.0", "scroll-carousel-react": "^1.0.0", + "use-callback-ref": "^1.3.1", "web-vitals": "^2.1.4", "wpapi": "^1.2.2" }, diff --git a/src/lib/useElementOnScreen.js b/src/lib/useElementOnScreen.js index 78084b5..08ba11b 100644 --- a/src/lib/useElementOnScreen.js +++ b/src/lib/useElementOnScreen.js @@ -1,8 +1,8 @@ -import { useRef, useEffect, useState } from "react"; +import { useEffect, useState } from "react"; const useElementOnScreen = (options) => { - const containerRef = useRef(null); + const [ref, setRef] = useState(null); const [isVisible, setIsVisible] = useState(false); const observerCallback = (entries) => { @@ -13,18 +13,18 @@ const useElementOnScreen = (options) => { useEffect(() => { const observer = new IntersectionObserver(observerCallback, options); - if (containerRef.current) { - observer.observe(containerRef.current); + if (ref) { + observer.observe(ref); } return () => { - if (containerRef.current) { - observer.unobserve(containerRef.current); + if (ref) { + observer.unobserve(ref); } } - }, [containerRef, options]) + }, [ref, options]); - return [containerRef, isVisible]; + return [setRef, 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 36ccfc8..bda35ce 100755 --- a/src/pages/Main/Main.jsx +++ b/src/pages/Main/Main.jsx @@ -1,4 +1,4 @@ -import { useContext } from 'react'; +import { useContext, useEffect } from 'react'; import { News, Projects, Labs, Partners, Gallery, ContactUs } from './components'; import { RootContext } from '../Root'; @@ -6,11 +6,11 @@ import { RootContext } from '../Root'; import './Main.styles.scss'; function Main() { - const { containerRef } = useContext(RootContext); + const { setRef } = useContext(RootContext); return ( <> -
+
setRef(element)}>

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

Студенческое конструкторское бюро
diff --git a/src/pages/Root/Root.jsx b/src/pages/Root/Root.jsx index e07af02..50a74df 100755 --- a/src/pages/Root/Root.jsx +++ b/src/pages/Root/Root.jsx @@ -1,4 +1,4 @@ -import { Outlet, useLoaderData } from "react-router-dom"; +import { Outlet, useLocation } from "react-router-dom"; import { createContext, useEffect } from 'react'; import { getContacts } from "../../api"; @@ -15,8 +15,18 @@ async function loader() { } function Root() { + const currentRoot = useLocation(); - const [containerRef, isVisible] = useElementOnScreen({ + useEffect(() => { + const [navbar] = document.getElementsByClassName('navbar'); + if (currentRoot.pathname !== '/') { + navbar.classList.remove('absolute'); + } else { + navbar.classList.add('absolute'); + } + }, [currentRoot]); + + const [setRef, isVisible] = useElementOnScreen({ root: null, rootMargin: '0px', threshold: 0, @@ -33,7 +43,7 @@ function Root() { return (