fabiobiondi

HamburgerMenu

Oct 31st, 2020
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SERVER RESPONSE (items)
  2. {
  3.   "config": [
  4.     { "icon": "fa fa-google", "url": "http://www.google.com"},
  5.     { "icon": "fa fa-windows", "url": "http://www.microsoft.com"},
  6.     { "icon": "fa fa-facebook", "url": "http://www.facebook.com"},
  7.     { "icon": "fa fa-linkedin", "url": "http://www.linkedin.com"},
  8.     { "icon": "fa fa-instagram", "url": "http://www.instagram.com"},
  9.     { "icon": "fa fa-youtube", "url": "http://www.youtube.com"}
  10.   ],
  11.   "configRoutes": [
  12.     { "icon": "fa fa-home", "url": "home"},
  13.     { "icon": "fa fa-list", "url": "catalog"},
  14.     { "icon": "fa fa-envelope", "url": "contacts"},
  15.     { "icon": "fa fa-linkedin", "url": "home"},
  16.     { "icon": "fa fa-instagram", "url": "catalog"},
  17.     { "icon": "fa fa-youtube", "url": "contacts"}
  18.   ]
  19. }
  20.  
  21.  
  22.  // App.tsx
  23. export const App: React.FC = () => {
  24.   const [items, setItems] = useState<Item[]>([]);
  25.  
  26.   // items: populated from http request
  27.  
  28.   return (
  29.     <div>
  30.       <AnimatedHamburger items={  } iconClick={goto} />
  31.     </div>
  32.   )
  33. };
  34.  
  35. export default App;
  36.  
  37.  
  38. // ===========================
  39. // Hamburger Menu
  40.  
  41. import React, { useState } from 'react';
  42. import classNames from 'classnames';
  43.  
  44. export interface Item {
  45.   icon: string;
  46.   url: string;
  47. }
  48.  
  49. export interface AnimatedHamburgerProps {
  50.   items: Item[];
  51.   iconClick: (url: string) => void;
  52. }
  53.  
  54. export const AnimatedHamburger: React.FC<AnimatedHamburgerProps> = (props) => {
  55.   const [open, setOpen] = useState<boolean>(false);
  56.  
  57.   const iconClickHandler = (url: string) => {
  58.     // PROBLEM HERE: menu is not opened!!!
  59.     setOpen(false);
  60.     props.iconClick(url);
  61.   }
  62.  
  63.   return <nav className="menu">
  64.     <span className={classNames('menu-open', {'opened': open})} />
  65.     <label className="menu-open-button"  onClick={() => setOpen(!open)}  >
  66.       <span className="hamburger hamburger-1" />
  67.       <span className="hamburger hamburger-2" />
  68.       <span className="hamburger hamburger-3" />
  69.     </label>
  70.  
  71.     {
  72.       props.items.map(item => {
  73.         return <span className="menu-item" key={item.icon} onClick={() => iconClickHandler(item.url)}>
  74.           <i className={item.icon} />
  75.         </span>
  76.       })
  77.     }
  78.   </nav>
  79. };
  80.  
  81.  
Add Comment
Please, Sign In to add comment