Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```javascript
  2. import React, { Component } from "react";
  3. import { Link } from "react-router-dom";
  4.  
  5. import Logo from "../Logo/Logo";
  6.  
  7. import "./Header.scss";
  8.  
  9. /**
  10.  * Header component.
  11.  */
  12.  
  13. class Header extends Component {
  14.   constructor() {
  15.     super();
  16.  
  17.     this.state = {
  18.       open: false,
  19.       scrolled: false
  20.     };
  21.   }
  22.  
  23.   /**
  24.    * Lifecycle methods.
  25.    */
  26.  
  27.   componentDidMount = () => {
  28.     document.addEventListener("scroll", this.handleScroll);
  29.   };
  30.  
  31.   componentWillUnmount = () => {
  32.     document.removeEventListener("scroll", this.handleScroll);
  33.   };
  34.  
  35.   /**
  36.    * Handle scroll.
  37.    */
  38.  
  39.   handleScroll = () => {
  40.     if (window.scrollY > 0) {
  41.       this.setState({ scrolled: true });
  42.     } else {
  43.       this.setState({ scrolled: false });
  44.     }
  45.   };
  46.  
  47.   /**
  48.    * Toggle the state of navigation and toggle.
  49.    */
  50.  
  51.   toggle = () => {
  52.     this.setState({ open: !this.state.open });
  53.   };
  54.  
  55.   render = () => {
  56.     const classes = {
  57.       navigation: this.state.open
  58.         ? "navigation navigation--open"
  59.         : "navigation",
  60.       toggle: this.state.open
  61.         ? "hamburger hamburger--spin is-active"
  62.         : "hamburger hamburger--spin",
  63.       header: this.state.scrolled ? "header header--background" : "header"
  64.     };
  65.  
  66.     return (
  67.       <>
  68.         <header className={classes.header}>
  69.           <Logo />
  70.           <button
  71.             className={classes.toggle}
  72.             onClick={this.toggle}
  73.             type="button"
  74.           >
  75.             <span className="hamburger-box">
  76.               <span className="hamburger-inner" />
  77.             </span>
  78.           </button>
  79.         </header>
  80.         <section className={classes.navigation}>
  81.           <nav>
  82.             <ul>
  83.               <li>
  84.                 <Link to="/" onClick={this.toggle} className="navigation__item">
  85.                   Home
  86.                 </Link>
  87.                 <Link
  88.                   to="/contact"
  89.                   onClick={this.toggle}
  90.                   className="navigation__item"
  91.                 >
  92.                   Contact
  93.                 </Link>
  94.               </li>
  95.             </ul>
  96.           </nav>
  97.         </section>
  98.         <section className="spacer" />
  99.       </>
  100.     );
  101.   };
  102. }
  103.  
  104. export default Header;
  105. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement