Check-Menus.com

svelte menu

by Sunny Gislason Published 2 years ago Updated 2 years ago
image

An interactive menu that opens to the bottom of a button using Svelte and Tailwind CSS. For this component to properly work, you will need to install popper.js into your project. Please run the following: npm i -E @popperjs/core@2.4.4

Full Answer

Creating a sidebar menu in Svelte

A common requirement for web apps is a sidebar menu, at least on mobile, sometimes on desktop too.

Component Tree

First, let's think 🤔 about what kind of components we need in our tree:

Navigation bar

In our navigation, we'll use Tailwind's justify-between to keep the hamburger and logo on the left, with the menu on the right.

Floating Sidebar

The sidebar component is an <aside> that is offscreen by default left: -100%, but when open == true, the class .open is added, which transitions the sidebar to left: 0. That makes it slide across the screen.

The navbar Svelte script logic

This section of the Svelte component is what handles our state, menu items, and even handlers.

The markup

Some key parts to this section is the class conditionals, attaching the handleMobileIconClick function to a click event, and going through a an each loop to render the menu items.

The styles

Here are the styles that make up the navgiation bar, mobile menu icon, and it’s transitions.

Full code

I like to tweet about Svelte and post helpful code snippets. Follow me there if you would like some too!

image
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9