react-burger-menu

An off-canvas sidebar component with a collection of effects and styles usi...

README

react-burger-menu Build Status code style: prettier
=================

An off-canvas sidebar React component with a collection of effects and styles using CSS transitions and SVG path animations.

Using Redux? Check out redux-burger-menu for easy integration of react-burger-menu into your project.

Demo & examples



To build the examples locally, first make sure you're using Node <11.0.0. Then run:

  1. ```
  2. npm install
  3. npm start
  4. ```

Then open [localhost:8000](http://localhost:8000) in a browser.

Tests


The test suite uses Mocha, Chai and Sinon, with jsdom.

To run the tests once, run:

  1. ```
  2. npm test
  3. ```

To run them with a watcher, run:

  1. ```
  2. npm run test:watch
  3. ```

Installation


The easiest way to use react-burger-menu is to install it from npm and include it in your own React build process (using Browserify, Webpack, etc).

You can also use the standalone build by including dist/react-burger-menu.js in your page. If you use this, make sure you have already included React, and it is available as a global variable.

Version 3.x uses Hooks, so if you're using React 16.8+:
  1. ```
  2. npm install react-burger-menu --save
  3. ```

If you're using an earlier version of React:
  1. ```
  2. npm install react-burger-menu@^2.9.2 --save
  3. ```

Usage


Items for the sidebar should be passed as child elements of the component using JSX.

  1. ``` javascript
  2. import { slide as Menu } from 'react-burger-menu'

  3. class Example extends React.Component {
  4.   showSettings (event) {
  5.     event.preventDefault();
  6.     .
  7.     .
  8.     .
  9.   }

  10.   render () {
  11.     // NOTE: You also need to provide styles, see https://github.com/negomi/react-burger-menu#styling
  12.     return (
  13.       <Menu>
  14.         <a id="home" className="menu-item" href="/">Home</a>
  15.         <a id="about" className="menu-item" href="/about">About</a>
  16.         <a id="contact" className="menu-item" href="/contact">Contact</a>
  17.         <a onClick={ this.showSettings } className="menu-item--small" href="">Settings</a>
  18.       </Menu>
  19.     );
  20.   }
  21. }
  22. ```

Animations


The example above imported slide which renders a menu that slides in on the page when the burger icon is clicked. To use a different animation you can substitute slide with any of the following (check out the demo to see the animations in action):

slide
stack
elastic
bubble
push
pushRotate
scaleDown
scaleRotate
fallDown
reveal

Properties


Some animations require certain other elements to be on your page:

Page wrapper - an element wrapping the rest of the content on your page (except elements with fixed positioning - see the wiki for details), placed after the menu component

  1. ``` javascript
  2.   <Menu pageWrapId={ "page-wrap" } />
  3.   <main id="page-wrap">
  4.     .
  5.     .
  6.     .
  7.   </main>
  8. ```

Outer container - an element containing everything, including the menu component
  1. ``` javascript
  2.   <div id="outer-container">
  3.     <Menu pageWrapId={ "page-wrap" } outerContainerId={ "outer-container" } />
  4.     <main id="page-wrap">
  5.       .
  6.       .
  7.       .
  8.     </main>
  9.   </div>
  10. ```

If you are using an animation that requires either/both of these elements, you need to give the element an ID, and pass that ID to the menu component as the pageWrapId and outerContainerId props respectively.

Check this table to see which animations require these elements:

Animation | pageWrapId | outerContainerId
slide | |
stack | |
elastic | ✓ | ✓
bubble | |
push | ✓ | ✓
pushRotate |  ✓  |  ✓
scaleDown |  ✓  |  ✓
scaleRotate |  ✓  |  ✓
fallDown |  ✓  |  ✓
reveal |  ✓  |  ✓

Position


The menu opens from the left by default. To have it open from the right, use the right prop. It's just a boolean so you don't need to specify a value. Then set the position of the button using CSS.

  1. ``` javascript
  2. <Menu right />
  3. ```

Width


You can specify the width of the menu with the width prop. The default is 300.

  1. ``` javascript
  2. <Menu width={ 280 } />
  3. <Menu width={ '280px' } />
  4. <Menu width={ '20%' } />
  5. ```

Open state


You can control whether the sidebar is open or closed with the isOpen prop. This is useful if you need to close the menu after a user clicks on an item in it, for example, or if you want to open the menu from some other button in addition to the standard burger icon. The default value is false.

  1. ``` javascript
  2. // To render the menu open
  3. <Menu isOpen />
  4. <Menu isOpen={ true } />

  5. // To render the menu closed
  6. <Menu isOpen={ false } />
  7. ```

You can see a more detailed example of how to use isOpen here.

Note: If you want to render the menu open initially, you will need to set this property in your parent component's componentDidMount() function.

Open menu handler


If you keep the menu state yourself it might be convenient to pass a custom function to be used when the user triggers something that should open the menu.

Called when:
The user clicks on the burger icon

  1. ``` javascript
  2. <Menu onOpen={ handleOnOpen } />
  3. ```

Note: The menu will NOT open automatically if you pass this prop, so you must handle it yourself.

Close menu handler


If you keep the menu state yourself it might be convenient to pass a custom function to be used when the user triggers something that should close the menu.

Called when:
The user clicks on the cross icon
The user clicks on the overlay
The user hits the escape key

  1. ``` javascript
  2. <Menu onClose={ handleOnClose } />
  3. ```

Note: The menu will NOT close automatically if you pass this prop, so you must handle it yourself.

State change


You can detect whether the sidebar is open or closed by passing a callback function to onStateChange. The callback will receive an object containing the new state as its first argument.

  1. ``` javascript
  2. var isMenuOpen = function(state) {
  3.   return state.isOpen;
  4. };

  5. <Menu onStateChange={ isMenuOpen } />
  6. ```

Close on Escape


By default, the menu will close when the Escape key is pressed. To disable this behavior, you can pass the disableCloseOnEsc prop. This is useful in cases where you want the menu to be open all the time, for example if you're implementing a responsive menu that behaves differently depending on the browser width.

  1. ``` javascript
  2. <Menu disableCloseOnEsc />
  3. ```

Custom keydown handler


For more control over global keypress functionality, you can override the handler that this component sets for window.addEventListener('keydown', handler), and pass a custom function. This could be useful if you are using multiple instances of this component, for example, and want to implement functionality to ensure that a single press of the Escape key closes them all.

  1. ``` javascript
  2. const closeAllMenusOnEsc = (e) => {
  3.   e = e || window.event;

  4.   if (e.key === 'Escape' || e.keyCode === 27) {
  5.     this.setState({areMenusOpen: false});
  6.   }
  7. };

  8. <MenuOne customOnKeyDown={closeAllMenusOnEsc} isOpen={areMenusOpen} />
  9. <MenuTwo customOnKeyDown={closeAllMenusOnEsc} isOpen={areMenusOpen} />
  10. ```
Note: Using this prop will disable all the default 'close on Escape' functionality, so you will need to handle this (including determining which key was pressed) yourself.

Overlay


You can turn off the default overlay with noOverlay.

  1. ``` javascript
  2. <Menu noOverlay />
  3. ```

You can disable the overlay click event (i.e. prevent overlay clicks from closing the menu) with disableOverlayClick. This can either be a boolean, or a function that returns a boolean.

  1. ``` javascript
  2. <Menu disableOverlayClick />
  3. <Menu disableOverlayClick={() => shouldDisableOverlayClick()} />
  4. ```

Transitions


You can disable all transitions/animations by passing noTransition.

  1. ``` javascript
  2. <Menu noTransition />
  3. ```

This is useful if you want the menu to remain open across re-mounts, for example during SPA route changes.

Custom icons


You can replace the default bars that make up the burger and cross icons with custom ReactElements. Pass them as the customBurgerIcon and customCrossIcon props respectively.

  1. ``` js
  2. <Menu customBurgerIcon={ <img src="img/icon.svg" /> } />
  3. <Menu customCrossIcon={ <img src="img/cross.svg" /> } />
  4. ```

You should adjust their size using the .bm-burger-button and .bm-cross-button classes, but the element itself will have the class .bm-icon or .bm-cross if you need to access it directly.

You can also disable the icon elements so they won't be included at all, by passing false to these props.

  1. ``` js
  2. <Menu customBurgerIcon={ false } />
  3. <Menu customCrossIcon={ false } />
  4. ```

This can be useful if you want exclusive external control of the menu, using the isOpen prop.

Custom ID and/or classNames


There are optional id and className props, which will simply add an ID or custom className to the rendered menu's outermost element. This is not required for any functionality, but could be useful for things like styling with CSS modules.

  1. ``` javascript
  2. <Menu id={ "sidebar" } className={ "my-menu" } />
  3. ```

You can also pass custom classNames to the other elements:

  1. ``` javascript
  2. <Menu burgerButtonClassName={ "my-class" } />
  3. <Menu burgerBarClassName={ "my-class" } />
  4. <Menu crossButtonClassName={ "my-class" } />
  5. <Menu crossClassName={ "my-class" } />
  6. <Menu menuClassName={ "my-class" } />
  7. <Menu morphShapeClassName={ "my-class" } />
  8. <Menu itemListClassName={ "my-class" } />
  9. <Menu overlayClassName={ "my-class" } />
  10. ```

And to the html and body elements (applied when the menu is open):
  1. ``` js
  2. <Menu htmlClassName={ "my-class" } />
  3. <Menu bodyClassName={ "my-class" } />
  4. ```

Note: Passing these props will prevent the menu from applying styles to the html or body elements automatically. See here for more explanation.

Focusing the first menu item


By default, the menu will set focus on the first item when opened. This is to help with keyboard navigation. If you don't want this functionality, you can pass the disableAutoFocus prop.

  1. ``` javascript
  2. <Menu disableAutoFocus />
  3. ```

Custom item list element


The menu's children are all wrapped in a nav element by default, as navigation is likely the most common use case for this component. However, it's a general purpose sidebar, so you can change this to a div if you're not using it for navigation:

  1. ``` javascript
  2. <Menu itemListElement="div" />
  3. ```

Styling


All the animations are handled internally by the component. However, the visual styles (colors, fonts etc.) are not, and need to be supplied, either with CSS or with a JavaScript object passed as the styles prop.

CSS


The component has the following helper classes:

  1. ``` css
  2. /* Position and sizing of burger button */
  3. .bm-burger-button {
  4.   position: fixed;
  5.   width: 36px;
  6.   height: 30px;
  7.   left: 36px;
  8.   top: 36px;
  9. }

  10. /* Color/shape of burger icon bars */
  11. .bm-burger-bars {
  12.   background: #373a47;
  13. }

  14. /* Color/shape of burger icon bars on hover*/
  15. .bm-burger-bars-hover {
  16.   background: #a90000;
  17. }

  18. /* Position and sizing of clickable cross button */
  19. .bm-cross-button {
  20.   height: 24px;
  21.   width: 24px;
  22. }

  23. /* Color/shape of close button cross */
  24. .bm-cross {
  25.   background: #bdc3c7;
  26. }

  27. /*
  28. Sidebar wrapper styles
  29. Note: Beware of modifying this element as it can break the animations - you should not need to touch it in most cases
  30. */
  31. .bm-menu-wrap {
  32.   position: fixed;
  33.   height: 100%;
  34. }

  35. /* General sidebar styles */
  36. .bm-menu {
  37.   background: #373a47;
  38.   padding: 2.5em 1.5em 0;
  39.   font-size: 1.15em;
  40. }

  41. /* Morph shape necessary with bubble or elastic */
  42. .bm-morph-shape {
  43.   fill: #373a47;
  44. }

  45. /* Wrapper for item list */
  46. .bm-item-list {
  47.   color: #b8b7ad;
  48.   padding: 0.8em;
  49. }

  50. /* Individual item */
  51. .bm-item {
  52.   display: inline-block;
  53. }

  54. /* Styling of overlay */
  55. .bm-overlay {
  56.   background: rgba(0, 0, 0, 0.3);
  57. }
  58. ```

JavaScript


The same styles can be written as a JavaScript object like this:

  1. ``` js
  2. var styles = {
  3.   bmBurgerButton: {
  4.     position: 'fixed',
  5.     width: '36px',
  6.     height: '30px',
  7.     left: '36px',
  8.     top: '36px'
  9.   },
  10.   bmBurgerBars: {
  11.     background: '#373a47'
  12.   },
  13.   bmBurgerBarsHover: {
  14.     background: '#a90000'
  15.   },
  16.   bmCrossButton: {
  17.     height: '24px',
  18.     width: '24px'
  19.   },
  20.   bmCross: {
  21.     background: '#bdc3c7'
  22.   },
  23.   bmMenuWrap: {
  24.     position: 'fixed',
  25.     height: '100%'
  26.   },
  27.   bmMenu: {
  28.     background: '#373a47',
  29.     padding: '2.5em 1.5em 0',
  30.     fontSize: '1.15em'
  31.   },
  32.   bmMorphShape: {
  33.     fill: '#373a47'
  34.   },
  35.   bmItemList: {
  36.     color: '#b8b7ad',
  37.     padding: '0.8em'
  38.   },
  39.   bmItem: {
  40.     display: 'inline-block'
  41.   },
  42.   bmOverlay: {
  43.     background: 'rgba(0, 0, 0, 0.3)'
  44.   }
  45. }

  46. <Menu styles={ styles } />
  47. ```

Browser support


Because this project uses CSS3 features, it's only meant for modern browsers. Some browsers currently fail to apply some of the animations correctly.

Chrome and Firefox have full support, but Safari and IE have strange behavior for some of the menus.

Help


Check the FAQ (https://github.com/negomi/react-burger-menu/wiki/FAQ) to see if your question has been answered already, or open a new issue.

License


MIT