Browse Source

navbar: ajout d'un style conditionné à la présence du header dans le viewport

Lou 3 years ago
parent
commit
d49d55f9b0

+ 1
- 0
package.json View File

@@ -24,6 +24,7 @@
24 24
 		"react": "^17.0.1",
25 25
 		"react-dom": "^17.0.1",
26 26
 		"react-helmet": "^6.1.0",
27
+		"react-intersection-observer": "^8.32.0",
27 28
 		"react-toastify": "^7.0.4",
28 29
 		"sass": "^1.37.5"
29 30
 	},

+ 1
- 1
src/components/contactForm/index.jsx View File

@@ -103,7 +103,7 @@ const ContactForm = () => {
103 103
 						onChange={handleEmailChange}
104 104
 					/>
105 105
 				</div>
106
-				<div className="form-group" style={{ display: "none;" }}>
106
+				<div className="form-group" style={{ display: "none" }}>
107 107
 					<input
108 108
 						style={{ display: "none;" }}
109 109
 						type="email-confirm"

+ 17
- 2
src/components/header/index.jsx View File

@@ -1,11 +1,26 @@
1
-import React from "react";
1
+import React, { useContext, useEffect } from "react";
2
+import { useInView } from "react-intersection-observer";
2 3
 
3 4
 import "./index.scss";
4 5
 import BtnContact from "../btnContact";
6
+import HeaderContext from "../../context/headerContext";
5 7
 
6 8
 const Header = () => {
9
+	const navbarContext = useContext(HeaderContext);
10
+	const { ref, inView } = useInView({
11
+		threshold: 0
12
+	});
13
+
14
+	const handleVisibility = (x) => {
15
+		navbarContext.isHeaderHiddenFct(x);
16
+	};
17
+
18
+	useEffect(() => {
19
+		handleVisibility(inView);
20
+	}, [inView]);
21
+
7 22
 	return (
8
-		<header>
23
+		<header ref={ref}>
9 24
 			<div className="hero">
10 25
 				<h1>Du numérique, en Alsace, au plus proche de vos besoins.</h1>
11 26
 				<div className="hero-box">

+ 2
- 1
src/components/header/index.scss View File

@@ -2,11 +2,12 @@
2 2
 @use "../../scss/abstracts/fonts" as *;
3 3
 
4 4
 header {
5
-	width: 100vw;
5
+	width: calc(100vw - 12px);
6 6
 	height: 100vh;
7 7
 	display: flex;
8 8
 	background-image: url("../../images/pen-blue.jpg");
9 9
 	background-position: center;
10
+	background-size: cover;
10 11
 	padding-top: 5rem;
11 12
 	color: $light;
12 13
 

+ 15
- 3
src/components/layout/index.jsx View File

@@ -1,17 +1,29 @@
1
-import React from "react";
1
+import React, { useState } from "react";
2 2
 import PropTypes from "prop-types";
3 3
 import { ToastContainer, toast } from "react-toastify";
4 4
 
5
+import HeaderContext from "../../context/headerContext";
6
+
5 7
 import Navbar from "../navbar";
6 8
 import Footer from "../footer";
7 9
 
8 10
 toast.configure();
9 11
 
10 12
 const Layout = ({ children }) => {
13
+	const [headerState, setHeaderState] = useState(false);
14
+
11 15
 	return (
12 16
 		<>
13
-			<Navbar />
14
-			<main>{children}</main>
17
+			<HeaderContext.Provider
18
+				value={{
19
+					isHeaderHiddenFct: (x) => {
20
+						setHeaderState(x);
21
+					},
22
+					isHeaderHidden: headerState
23
+				}}>
24
+				<Navbar />
25
+				<main>{children}</main>
26
+			</HeaderContext.Provider>
15 27
 			<ToastContainer
16 28
 				position="bottom-center"
17 29
 				autoClose={3000}

+ 17
- 2
src/components/navbar/index.jsx View File

@@ -1,12 +1,27 @@
1
-import React from "react";
1
+import React, { useContext } from "react";
2 2
 import { Link } from "gatsby";
3
+import HeaderContext from "../../context/headerContext";
3 4
 
4 5
 import "./index.scss";
5 6
 
6 7
 const Navbar = () => {
8
+	const mustShowNavbar = useContext(HeaderContext);
9
+
10
+	const handleShowNavbar = (context) => {
11
+		if (context.isHeaderHidden === true) {
12
+			console.log(context.isHeaderHidden);
13
+			console.log("transparent");
14
+			return { background: "transparent" };
15
+		} else {
16
+			console.log(context.isHeaderHidden);
17
+			console.log("white");
18
+			return { background: "white" };
19
+		}
20
+	};
21
+
7 22
 	return (
8 23
 		<div id="navbar-container">
9
-			<nav>
24
+			<nav style={handleShowNavbar(mustShowNavbar)}>
10 25
 				<div className="details">
11 26
 					<Link to="/services" className="menu-btn">
12 27
 						<span>Services</span>

+ 1
- 2
src/components/navbar/index.scss View File

@@ -9,12 +9,11 @@
9 9
 nav {
10 10
 	padding: 0.5rem 2rem;
11 11
 	position: fixed;
12
-	overflow-y: scroll;
13 12
 	z-index: 1;
14 13
 	width: 100vw;
15 14
 	height: 3.5rem;
16 15
 	padding: 0;
17
-	background: transparent;
16
+	// background: transparent;
18 17
 	display: flex;
19 18
 	align-items: center;
20 19
 	justify-content: space-between;

+ 8
- 0
src/context/headerContext.jsx View File

@@ -0,0 +1,8 @@
1
+import { createContext } from "react";
2
+
3
+const HeaderContext = createContext({
4
+	isHeaderHiddenFct: null,
5
+	isHeaderHidden: null
6
+});
7
+
8
+export default HeaderContext;

+ 5
- 0
yarn.lock View File

@@ -9152,6 +9152,11 @@ react-helmet@^6.1.0:
9152 9152
     react-fast-compare "^3.1.1"
9153 9153
     react-side-effect "^2.1.0"
9154 9154
 
9155
+react-intersection-observer@^8.32.0:
9156
+  version "8.32.0"
9157
+  resolved "https://registry.yarnpkg.com/react-intersection-observer/-/react-intersection-observer-8.32.0.tgz#47249332e12e8bb99ed35a10bb7dd10446445a7b"
9158
+  integrity sha512-RlC6FvS3MFShxTn4FHAy904bVjX5Nn4/eTjUkurW0fHK+M/fyQdXuyCy9+L7yjA+YMGogzzSJNc7M4UtfSKvtw==
9159
+
9155 9160
 react-is@^16.12.0, react-is@^16.8.1:
9156 9161
   version "16.13.1"
9157 9162
   resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"

Loading…
Cancel
Save