Zach’s ugly mug (his face) Zach Leat­herman

The Infinity Hamburger Menu, now in Web Component form

November 18, 2023

A whimsical hamburger menu* that goes on forever.

*Not actually a functioning menu.

First open sourced in 2015 as a vanilla not-web-component and inspired by a hamburger menu on 24ways.org. I’ve been absolutely delighted when folks have sent me various messages over the years saying that they loved the menu. Add some whimsy to your personal site, it’s fun!

Notably, the commit updating the code from a CSS/JavaScript component to a Shadow DOM encapsulated standalone JavaScript component might be educational, if you’re interested in that!

This code update resulted in a simplification to the markup (one empty custom element, this is a JavaScript Web Component) and simplified the assets necessary to use it too (a single JavaScript file instead of CSS and JavaScript files).

Using a custom element in this way also unlocks the use of <is-land>, which meant that I got a bunch of features for-free:

  1. Only runs the JS when the page is idle (requestIdleCallback).
  2. Only runs the JS when the component is visible (if you’re scrolled down, the component JavaScript doesn’t load!).
  3. Only runs the JS when the user does not have a reduced motion preference set.
  4. Only runs the JS at desktop viewport sizes.

All of the above must be true before the script is loaded. Here’s the markup:

<is-land on:idle on:visible on:media="(prefers-reduced-motion: no-preference) and (min-width: 61.1875em)">
	<infinity-burger></infinity-burger>
	<template data-island>
		<script type="module" src="/static/infinity-burger.js"></script>
	</template>
</is-land>

Demo

Click or tap the hamburger menu icon on the top of this page (at desktop viewport sizes). Standalone demo also on GitHub pages.

Get it

Zach Leatherman is a builder for the web at Font Awesome and the creator of Build Awesome (née Eleventy/11ty), an award-winning open source website generator. He measures website performance with speedlify and at one point became too fixated on web fonts. He has given 86 talks in nine different countries at events like Beyond Tellerrand, State of the Browser, Smashing Conference, Jamstack Conf, CSSConf, and The White House. Learn more about Zach »