Check-Menus.com

svelte dropdown menu

by Myles Morar Published 2 years ago Updated 2 years ago
image

Notus Svelte Dropdowns 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 Dropdown Example orange Dropdown

Full Answer

How do I create an interactive menu using svelte and Tailwind CSS?

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

How do I create a dropdown menu?

Actually, creating a dropdown menu is not as simple as it sounds. First, you have to handle mouse clicks outside of it and close the menu if it's currently open. Second, you should support pressing Escape key and close the menu if it's currently open. Third, you should add nice animation to the menu so it feels more alive.

Do you need help with svelte?

Still very new to svelte (but learning more each day!) and need a bit of help. I am working on a veterinary drug dose calculator that calculates the individual dose of each drug based on the concentration of each drug, the mg/kg dosage of that drug and the weight in kilograms of the patient.

What is the svelte component for?

This section of the Svelte component is what handles our state, menu items, and even handlers. The first thing to note is that we have a Svelte state variable called showMobileMenu. This state variable will get used in the markup.

image

What is svelte dropdown?

Svelte Dropdown Component for Bootstrap (Bootstrap’s dropdown plugin in svlete applications), can be used with sapper or standalone with svelte.Just like Vainilla bootstrap this plugin too is built on a third party library, Popper.js, which provides dynamic positioning and viewport detection. But Unlike Vainilla bootstrap we are using PopperJs V2 instead of v1

How does a button dropdown work?

Button dropdowns work with buttons of all sizes, including default and split dropdown buttons as the triggerElement is being handled by the developer they have freedom to add any classes, style etc

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