Animated Hamburger Menu Icons for React
hamburger-react provides a collection of animated hamburger menu icons specifically designed for React applications. It focuses on performance and elegance, utilizing CSS-driven transitions on 'cheap' properties to avoid jerky animations, differentiating itself from libraries that use JavaScript for animations or transition expensive CSS properties. The current stable version is 2.5.2, with releases occurring periodically to support new React versions and add minor features or accessibility improvements. Key differentiators include its small bundle size (~1.5 KB min+gzip per hamburger), hooks-based implementation (requiring React 16.8.0+), robust accessibility features (ARIA labels, keyboard interaction, recommended tap area), and a focused API that avoids over-customization while providing sensible defaults. It supports React 16.8 through 19 via peer dependencies.
Common errors
-
TypeError: (0, react__WEBPACK_IMPORTED_MODULE_0__.useState) is not a function
cause Using `hamburger-react` with an older version of React that does not support hooks (i.e., less than React 16.8.0).fixUpdate your `react` and `react-dom` packages to version 16.8.0 or higher. For example, `npm install react@latest react-dom@latest`. -
Module not found: Error: Can't resolve 'hamburger-react' in '...' or Attempted import error: 'Hamburger' is not exported from 'hamburger-react'.
cause Incorrect import statement for the `Hamburger` component. It is a default export, not a named export.fixChange `import { Hamburger } from 'hamburger-react'` to `import Hamburger from 'hamburger-react'`.
Warnings
- breaking The default color of the hamburger icon was changed to utilize `currentColor`, which inherits the text color of its parent element. This might alter the appearance of existing icons if a specific `color` prop was not previously set.
- breaking Support for the deprecated `KeyboardEvent.keyCode` property was removed. Only `KeyboardEvent.key` is now supported for keyboard interaction.
- gotcha The library is hooks-based and requires React 16.8.0 or higher. Using it with older React versions will result in runtime errors due to missing hooks functionality.
- gotcha For optimal accessibility, the library internally adds padding to create a tap/click area of at least 48x48 pixels. Be mindful of this when designing layouts around the hamburger icon, as it might occupy more space than just the visual icon itself.
Install
-
npm install hamburger-react -
yarn add hamburger-react -
pnpm add hamburger-react
Imports
- Hamburger
import { Hamburger } from 'hamburger-react'import Hamburger from 'hamburger-react'
- Hamburger component with state
const Hamburger = require('hamburger-react');import Hamburger from 'hamburger-react'; import { useState } from 'react'; const MyComponent = () => { const [isOpen, setOpen] = useState(false); return <Hamburger toggled={isOpen} toggle={setOpen} />; }; - Type definitions
import type { BurgerProps } from 'hamburger-react'
Quickstart
import Hamburger from 'hamburger-react';
import { useState } from 'react';
const MyMenuToggle = () => {
// Manage the open/closed state of the menu
const [isOpen, setOpen] = useState(false);
// Render the Hamburger component, binding its state to the component's state
// This allows external control over the hamburger's toggled state.
return (
<div>
<button
aria-label="Toggle menu"
onClick={() => setOpen(!isOpen)}
style={{ border: 'none', background: 'transparent', cursor: 'pointer' }}
>
<Hamburger
toggled={isOpen}
toggle={setOpen}
size={24}
duration={0.8}
label="Show navigation"
hideOutline={false}
direction="left"
distance="lg"
easing="ease-in"
rounded
/>
</button>
{isOpen && (
<nav style={{ padding: '20px', backgroundColor: '#f0f0f0', border: '1px solid #ccc' }}>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
)}
</div>
);
};
export default MyMenuToggle;