Code Monkey home page Code Monkey logo

hamburger-demo's Introduction

Hamburger Menu Demo (React + Styled-components)

Hamburger menu

Full article (in Russian): Бургер-меню на React, используя хуки и styled-components

There are several components in this app - HamburgerButton, MainMenu and SideMenu. When user clicks on hamburger button, context state is being updated. This way other components will know menu is open. If user clicks again, context state changes back to false.

Hamburger button has two kinds of visual states: on hover and active. Both states were done manually, using CSS styling only. No SVGs or fonts.

:hover {
  span:nth-of-type(1) {
    width: 33px;
  }

  span:nth-of-type(2) {
    width: 40px;
  }

  span:nth-of-type(3) {
    width: 30px;
  }
}

&.active {
  span:nth-of-type(1) {
    transform: rotate(45deg) translate(10px, 10px);
    width: 40px;
  }

  span:nth-of-type(2) {
    opacity: 0;
    pointer-events: none;
  }

  span:nth-of-type(3) {
    transform: rotate(-45deg) translate(7px, -7px);
    width: 40px;
  }
}  

On hover the top and bottom bars are getting shorter. When button is active, two things are happening: rotation of top and bottom bars (45 degrees) + moving. At the same time the middle bar is fading.

Also, menu tracks outside clicks. If menu is open and user clicked outside the ref, it will be closed.

import { useEffect } from 'react';

const useOnClickOutside = (ref, handler) => {
  useEffect(() => {
    const listener = event => {
      if (!ref.current || ref.current.contains(event.target)) {
        return;
      }
      handler(event);
    };
    document.addEventListener('mousedown', listener);
    return () => {
      document.removeEventListener('mousedown', listener);
    };
  }, [ref, handler]);
};

export default useOnClickOutside;

hamburger-demo's People

Contributors

r007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.