• Stars
    star
    120
  • Rank 286,301 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Animated hamburger menu icons for modern web apps (1.8 KB) πŸ”

Preview

vanilla-hamburger is a port of hamburger-react to vanilla Custom Elements.

Features

  • 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 <button> with a click listener internally.
  • Accessible: Follows WAI-ARIA guidelines for toggle buttons.
  • No dependencies

Live demo

Installation

npm install vanilla-hamburger --save

Or use one of the following content delivery networks:

unpkg.com CDN:

<script type="module" src="https://unpkg.com/vanilla-hamburger?module"></script>

Skypack CDN:

<script type="module" src="https://cdn.skypack.dev/vanilla-hamburger"></script>

Usage

<tilt-burger size="lg" label="Menu"></tilt-burger>
<script type="module">
  import 'vanilla-hamburger';

  const burger = document.querySelector('tilt-burger');
  burger.addEventListener('pressed-changed', (event) => {
    const pressed = event.detail.value;
  });
</script>

ES modules

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:

None of these tools are needed when importing the component from CDN.

Available variants

vanilla-hamburger provides 13 separate elements for different hamburger types.

File to import HTML element
"cross-burger.js" <cross-burger>
"fade-burger.js" <fade-burger>
"pivot-burger.js" <pivot-burger>
"rotate-burger.js" <rotate-burger>
"slant-burger.js" <slant-burger>
"sling-burger.js" <sling-burger>
"spin-burger.js" <spin-burger>
"spiral-burger.js" <spiral-burger>
"squash-burger.js" <squash-burger>
"squeeze-burger.js" <squeeze-burger>
"tilt-burger.js" <tilt-burger>
"turn-burger.js" <turn-burger>
"twirl-burger.js" <twirl-burger>

When using one hamburger, ~1.8 KB will be added to your bundle (min + gzip).

Properties

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 <button> id disabled.
distance md The distance between the lines: sm, md or lg.
duration 0.4 The duration of the animation. Can be set to zero.
easing cubic-bezier(0, 0, 0, 1) A valid transition-timing-function CSS value.
label hamburger Accessible label set on the internal <button>.
pressed false Set to true when element is pressed.
size 32 Size of the icon. Should be a number between 12 and 48.

Note: direction property is not supported by <squash-burger> and <squeeze-burger>.

Overriding styles

vanilla-hamburger exposes CSS Shadow Parts allowing to override the default styles:

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;
}

Base classes

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.

import { Cross } from 'vanilla-hamburger/lib/entrypoints/cross.js';

customElements.define('custom-burger', class extends Cross {});

Accessibility

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 <button>, which dispatches the click event on Enter and Space keys. The underlying native button has aria-pressed attribute set based on the pressed property.

Remember to use label property to provide an accessible label for the native button.

TypeScript support

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.

Browser support

vanilla-hamburger uses Custom Elements and Shadow DOM, and does not support IE11 or legacy Edge.

Why vanilla-hamburger?

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.

More Repositories

1

awesome-web-components

A curated list of awesome Web Components resources.
2,636
star
2

awesome-lit

A curated list of awesome Lit resources.
1,299
star
3

vanilla-colorful

A tiny color picker custom element for modern web apps (2.7 KB) 🎨
TypeScript
731
star
4

polymer3-webpack-starter

Polymer 3 starter built with Vaadin components and Vaadin.Router library, using webpack and modern tools
JavaScript
101
star
5

aybolit

Lightweight web components library built with LitElement.
JavaScript
100
star
6

webcomponents.today

A curated collection of Web Components resources.
JavaScript
80
star
7

lit-components

Moved to https://github.com/vaadin/component-mixins
JavaScript
60
star
8

lit-vaadin-helpers

A set of helpers for using Vaadin web components with Lit 2.
TypeScript
10
star
9

ts-vaadin-examples

Examples for using Vaadin components with TypeScript
TypeScript
8
star
10

pack

Polymer App Creation Kit
HTML
8
star
11

wc-jest-puppeteer-demo

JavaScript
5
star
12

polylit

An experiment to use Polymer based mixins with Lit.
JavaScript
4
star
13

a11y-vaadin-playground

Demo app showcasing accessibility fixes to Vaadin web components.
HTML
4
star
14

lit-overlays

Prototyping a set of overlay components with LitElement & TS
TypeScript
3
star
15

webpack-wc-demo

JavaScript
3
star
16

stylelint-config-polymer

shareable stylelint config for Polymer elements
JavaScript
2
star
17

elements-viewer

Vaadin core components demos & API docs https://web-padawan.github.io/elements-viewer/?vaadin-button#docs
HTML
2
star
18

fusion-wtr

TypeScript
1
star
19

a11y-vaadin-proto

JavaScript
1
star
20

npm-stats-training

HTML
1
star
21

colorizr

a WIP demo project built with Polymer 1.x
HTML
1
star
22

web-padawan.github.io

Homepage
Nunjucks
1
star
23

component-mixins

Moved to vaadin/component-mixins
TypeScript
1
star
24

hermione-esm

hermione plugin that starts @web/dev-server before running tests
JavaScript
1
star
25

pnpm-resolutions

An example how to make yarn resolutions work with pnpm
JavaScript
1
star
26

npm-downloads

JavaScript
1
star
27

skystart

frontend project template
CSS
1
star
28

components-flowchart

Visualise hierarchical relationships within Vaadin components.
1
star
29

ngplugins

plugins for NG CMS
PHP
1
star
30

shadow-examples

Examples of using Shadow DOM
HTML
1
star
31

polymer-sort-and-search

React Challenge #1 task built with Polymer
HTML
1
star