A tiny framework agnostic hamburger button element for modern web apps
npm install vanilla-hamburger
vanilla-hamburger is a port of hamburger-react to vanilla Custom Elements.
- Small: Just 1,8 KB (minified and gzipped). Size Limit controls the size.
- Fast: Built with standards based Custom Elements.
- Bulletproof: Written in strict TypeScript and covered by 30+ tests.
- Framework-agnostic: Can be used with any framework.
- Simple: Uses native with a click listener internally.
- Accessible: Follows WAI-ARIA guidelines for toggle buttons.
- No dependencies
- Website
- Angular example
- LitElement example
- React example
- Svelte example
- Vue example
```
npm install vanilla-hamburger --save
Or use one of the following content delivery networks:
`html`
`html`
`html`
vanilla-hamburger is authored using ES modules which are natively supported
by modern browsers. However, it also uses "bare module imports" which are not yet standardized
and require a small transform.
We recommend the following tools for the ES modules based development:
- @web/dev-server resolves bare module imports on the fly.
- snowpack performs one-time transform when installing dependencies.
- @rollup/plugin-node-resolve is needed when using Rollup.
None of these tools are needed when importing the component from CDN.
vanilla-hamburger provides 13 separate elements for different hamburger types.
| File to import | HTML element |
| --------------------- | ------------------ |
| "cross-burger.js" | |"fade-burger.js"
| | |"pivot-burger.js"
| | |"rotate-burger.js"
| | |"slant-burger.js"
| | |"sling-burger.js"
| | |"spin-burger.js"
| | |"spiral-burger.js"
| | |"squash-burger.js"
| | |"squeeze-burger.js"
| | |"tilt-burger.js"
| | |"turn-burger.js"
| | |"twirl-burger.js"
| | |
When using one hamburger, ~1.8 KB will be added to your bundle (min + gzip).
The following properties can be used to customize hamburger elements:
| Property | Default | Description |
| ----------- | -------------------------- | ------------------------------------------------------- |
| direction | left | The animation direction of the icon, left or right. |disabled
| | false | When set to true, the internal
Note: direction property is not supported by and .
vanilla-hamburger exposes CSS Shadow Parts
allowing to override the default styles:
`css
cross-burger {
color: #999;
}
cross-burger[pressed] {
color: #666;
}
cross-burger[disabled] {
opacity: 0.7;
}
cross-burger::part(bar) {
border-radius: 9em;
}
cross-burger::part(button) {
outline: none;
background: currentColor;
border-radius: 50%;
opacity: 0;
transition: opacity 0.5s;
}
cross-burger::part(button):hover {
opacity: 0.12;
}
cross-burger::part(button):focus {
opacity: 0.16;
}
`
vanilla-hamburger provides a set of base classes that can be imported without registering custom
elements. This is useful if you want to create your own hamburger icon with a different tag name.
`js
import { Cross } from 'vanilla-hamburger/lib/entrypoints/cross.js';
customElements.define('custom-burger', class extends Cross {});
`
It is recommended to have a tap/click area of at least 48x48 pixels. Therefore, padding will be
added around the icon to create a surface of exactly this size.
Keyboard interaction is provided using native , which dispatches the click event onaria-pressed
Enter and Space keys. The underlying native button has attributepressed
set based on the property.
Remember to use label property to provide an accessible label for the native button.
vanilla-hamburger supports TypeScript and ships with types in the library itself; no need for any other install.
All the included custom elements are compatible with lit-analyzer and
lit-plugin extension for Visual
Studio Code, so you can benefit from type checking in lit-html templates.
vanilla-hamburger uses Custom Elements and Shadow DOM,
and does not support IE11 or legacy Edge.
vanilla-hamburger has all the benefits of hamburger-react with one important difference.
While hamburger-react` does not have direct dependencies, it still expects you to use React. This
means that Angular, Vue, Svelte or vanilla JS users would have an extra dependency in their apps.
Now when all the evergreen browsers support standards based Custom Elements,
it's perfect time to build such tiny and lightweight UI controls as web components rather than framework components.