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

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

+8 -8
View File
@@ -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;
+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 { 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 (
<>
<header className="header" id="mainpageheader" ref={containerRef}>
<header id="mainpageheader" ref={element => setRef(element)}>
<div className="container">
<h1>Думай иначе, будь креативным!</h1>
<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 { 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 (
<RootContext.Provider
value={{ containerRef }}
value={{ setRef }}
>
<Navbar />
<div id="page">