Работает механизм переключения стилей, теперь не отваливается

This commit is contained in:
Ifraim Solomonov committed 2024-03-05 16:30:49 +03:00
1 parent ce0752c7eb
commit d8d03d5e77
6 files changed
+49 -15

No files matched your search

+3 -1
View File
@@ -1,10 +1,12 @@
{ {
"configurations": [ "configurations": [
{ {
"type": "chrome", "type": "chrome",
"name": "http://localhost:3000/", "name": "http://localhost:3000/",
"request": "launch", "request": "launch",
"url": "http://localhost:3000/" "url": "http://localhost:3000/",
"runtimeExecutable": "C:\\Users\\mrifr\\AppData\\Local\\Yandex\\YandexBrowser\\Application\\browser.exe"
} }
] ]
} }
+21
View File
@@ -18,6 +18,7 @@
"reactpress": "^1.0.0", "reactpress": "^1.0.0",
"sass": "^1.70.0", "sass": "^1.70.0",
"scroll-carousel-react": "^1.0.0", "scroll-carousel-react": "^1.0.0",
"use-callback-ref": "^1.3.1",
"web-vitals": "^2.1.4", "web-vitals": "^2.1.4",
"wpapi": "^1.2.2" "wpapi": "^1.2.2"
} }
@@ -17357,6 +17358,26 @@
"requires-port": "^1.0.0" "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": { "node_modules/util-deprecate": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
+1
View File
@@ -13,6 +13,7 @@
"reactpress": "^1.0.0", "reactpress": "^1.0.0",
"sass": "^1.70.0", "sass": "^1.70.0",
"scroll-carousel-react": "^1.0.0", "scroll-carousel-react": "^1.0.0",
"use-callback-ref": "^1.3.1",
"web-vitals": "^2.1.4", "web-vitals": "^2.1.4",
"wpapi": "^1.2.2" "wpapi": "^1.2.2"
}, },
+8 -8
View File
@@ -1,8 +1,8 @@
import { useRef, useEffect, useState } from "react"; import { useEffect, useState } from "react";
const useElementOnScreen = (options) => { const useElementOnScreen = (options) => {
const containerRef = useRef(null); const [ref, setRef] = useState(null);
const [isVisible, setIsVisible] = useState(false); const [isVisible, setIsVisible] = useState(false);
const observerCallback = (entries) => { const observerCallback = (entries) => {
@@ -13,18 +13,18 @@ const useElementOnScreen = (options) => {
useEffect(() => { useEffect(() => {
const observer = new IntersectionObserver(observerCallback, options); const observer = new IntersectionObserver(observerCallback, options);
if (containerRef.current) { if (ref) {
observer.observe(containerRef.current); observer.observe(ref);
} }
return () => { return () => {
if (containerRef.current) { if (ref) {
observer.unobserve(containerRef.current); observer.unobserve(ref);
} }
} }
}, [containerRef, options]) }, [ref, options]);
return [containerRef, isVisible]; return [setRef, isVisible];
} }
export default useElementOnScreen; export default useElementOnScreen;
+3 -3
View File
@@ -1,4 +1,4 @@
import { useContext } from 'react'; import { useContext, useEffect } from 'react';
import { News, Projects, Labs, Partners, Gallery, ContactUs } from './components'; import { News, Projects, Labs, Partners, Gallery, ContactUs } from './components';
import { RootContext } from '../Root'; import { RootContext } from '../Root';
@@ -6,11 +6,11 @@ import { RootContext } from '../Root';
import './Main.styles.scss'; import './Main.styles.scss';
function Main() { function Main() {
const { containerRef } = useContext(RootContext); const { setRef } = useContext(RootContext);
return ( return (
<> <>
<header className="header" id="mainpageheader" ref={containerRef}> <header id="mainpageheader" ref={element => setRef(element)}>
<div className="container"> <div className="container">
<h1>Думай иначе, будь креативным!</h1> <h1>Думай иначе, будь креативным!</h1>
<h2>Студенческое конструкторское бюро<br /> <h2>Студенческое конструкторское бюро<br />
+13 -3
View File
@@ -1,4 +1,4 @@
import { Outlet, useLoaderData } from "react-router-dom"; import { Outlet, useLocation } from "react-router-dom";
import { createContext, useEffect } from 'react'; import { createContext, useEffect } from 'react';
import { getContacts } from "../../api"; import { getContacts } from "../../api";
@@ -15,8 +15,18 @@ async function loader() {
} }
function Root() { 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, root: null,
rootMargin: '0px', rootMargin: '0px',
threshold: 0, threshold: 0,
@@ -33,7 +43,7 @@ function Root() {
return ( return (
<RootContext.Provider <RootContext.Provider
value={{ containerRef }} value={{ setRef }}
> >
<Navbar /> <Navbar />
<div id="page"> <div id="page">