Svelte Hamburgers

Usage

Get started by putting a hamburger menu on the page

<script>
    import { Hamburger } from 'svelte-hamburgers';
</script>

<Hamburger />

You can bind to the `open` prop to get it's state

<script>
    import { Hamburger } from 'svelte-hamburgers';

    let open = $state(false);
</script>

<Hamburger bind:open />

<p>Hamburger open: {open}</p>

Hamburger open: false

You can set your preferred animation type

<script>
    import { Hamburger } from 'svelte-hamburgers';
</script>

<Hamburger type="elastic" />

Set the title (and by extension aria-label)

<script>
    import { Hamburger } from 'svelte-hamburgers';
</script>

<Hamburger title="Toggles the cat picture" />

Why not change the style

<script>
    import { Hamburger } from 'svelte-hamburgers';
</script>

<Hamburger --color="red" />

We can then put this all together and create a full nav

<script>
    import Hamburger from '$lib/Hamburger.svelte';
    import { fly } from 'svelte/transition';

    let open = $state(false);
</script>

<nav>
    <Hamburger
        bind:open
        type="collapse"
        title="Toggle nav links"
        ariaControls="nav"
    />

    {#if open}
        <ul id="nav" class="menu" transition:fly={{ y: -15 }}>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    {/if}
</nav>

Props

PropertyTypeDefault ValueDescription
openstringfalseControls and represents whether the hamburger is open.
typestringspinThe type of burger you want, you can see the types here, it should be lowercase.
titlestringHamburger menuCan be used to add a tooltip, also controls the default value of the ariaLabel prop.
ariaLabelstringvalue of titleA label that describes the hamburger menu.
ariaControlsstringThis identifies the element(s) whos presence is controlled by the hamburger menu.
--colorstringblackThe color of the burger.
--active-colorstringvalue of --colorThe color of the burger when active.
--paddingstring15pxThe padding.
--layer-widthstring30pxThe width of the burger.
--layer-heightstring2pxThe height of the burger.
--layer-spacingstring6pxThe spacing between layers of the burger.
--border-radiusstring4pxThe border radius of each burger part.
--hover-opacitynumber0.7The opacity amount on hover.
--hover-opacity-activenumbervalue of --hover-opacityThe opacity amount of hover when active.
onclickMouseEventHandler<HTMLButtonElement>Fires when the hamburger is clicked. This event won't propagate.

Types

3dx

3dx-r

3dy

3dy-r

3dxy

3dxy-r

arrow

arrow-r

arrowalt

arrowalt-r

arrowturn

arrowturn-r

boring

collapse

collapse-r

elastic

elastic-r

emphatic

emphatic-r

minus

slider

slider-r

spin

spin-r

spring

spring-r

stand

stand-r

squeeze

vortex

vortex-r