Ausnahme gefangen: SSL certificate problem: certificate is not yet valid 📌 Say Goodbye to Boring Dropdowns: Create Custom Dropdown Menus with Headless UI

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Say Goodbye to Boring Dropdowns: Create Custom Dropdown Menus with Headless UI


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Creating a website is no small feat. It requires careful planning, strategic design, and thoughtful implementation. And one of the most crucial elements of any website is its user interface (UI).

A website’s interactive UI components serve as the gateway through which users interact with the site, and they play a critical role in shaping the user experience.

From dropdown menus to modals to tabs, UI components are essential in making websites more user-friendly and intuitive. But integrating these components is no easy task since they also have to be fully accessible as well as have reusability and customization options.

And that’s where Headless UI comes to the rescue.

Headless UI is an npm package that offers a practical solution to this problem. It enables you to build completely unstyled UI components that are fully accessible and easy to integrate into your website. It is created by Tailwind Labs who are also the creators of TailwindCSS.

By using Headless UI, you can focus more on styling your components with TailwindCSS and less on the functionality and accessibility side of things. This separates Headless UI from other, much more opinionated UI libraries such as Chakra and Bootstrap as the entire onus for styling is on the developer.

And based on that, we will be building a dropdown menu using Headless UI and TailwindCSS in this blog.

Let’s start.

Setting Up the Dev Environment

When you want to use TailwindCSS inside a React app, you must first set it up and configure it.

  • First, create a React app by running the command:
npx create-react-app my-app
  • Next, install TailwindCSS and other dependencies like postcss and autoprefixer using the command:
npm install -D tailwindcss postcss autoprefixer
  • Create config files by running the command:
npx tailwindcss init -p

Now open the tailwind.config.css file and replace the content with the provided code snippet.

/** @type {import('tailwindcss').Config} */

module.exports = {
content: [
   "./src/**/*.{js,jsx,ts,tsx}",
 ],
 theme: {
   extend: {},
 },
 plugins: [],
}
  • Paste the following code snippet inside the src/index.css file:
@tailwind base;
@tailwind components;
@tailwind utilities;

You can now use TailwindCSS in your React app.

  • Installing Headless UI is relatively simple; just run this command and then you can use it anywhere inside your React app.
npm install @headlessui/react

Creating a Traditional Dropdown Menu

While creating a basic dropdown menu is a relatively straightforward task, it can become cumbersome and keep you from implementing much more important features.

import { useState } from 'react'

function App() {
 const [isOpen, setIsOpen] = useState(false)


 return (
   <div className="relative inline-block text-left">
     <button
       type="button"
       className="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm text-gray-700 hover:bg-gray-50"
       onClick={() => setIsOpen(!isOpen)}
     >
       More
     </button>
     {isOpen && (
       <div className="absolute z-10 mt-2 w-56 rounded-md shadow-lg bg-white ">
         <div className="py-1" role="menu" aria-orientation="vertical" aria-labelledby="options-menu">
           <a href="/account-settings" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">Account settings</a>
           <a href="/documentation" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" role="menuitem">Documentation</a>
           <span className="block px-4 py-2 text-sm text-gray-700 opacity-50" role="menuitem">Invite a friend (coming soon!)</span>
         </div>
       </div>
     )}
   </div>
 )
}

export default App

To create a functional dropdown menu, you have to write about 20 lines of code as shown above.

And even with that, it often lacks critical functionalities in terms of accessibility and user experience. For example, you may not be able to click outside the panel to close the dropdown or navigate using keyboard keys.

For that, you can add multiple lines of code to address these issues but it can be time-consuming and tedious. That’s where UI libraries like Headless UI can be incredibly helpful as they provide pre-built components that offer accessibility and navigation support.

Creating a Customisable Dropdown Menu with Headless UI

When using Headless UI for a dropdown menu, it’s important to understand the basic concepts.

There are four primary components: “Menu”, “Menu.Button”, “Menu.Items”, and “Menu.Item”. As the names suggest, the “Menu” component contains everything, the “Menu.Button” is essentially a button, and the “Menu.Items” component contains multiple “Menu.Item” components.

This concept of components composition is the cornerstone of Headless UI and you will see this pattern repeat for other UI components such as popovers and dialogs.

With that said, here is the basic code based on the official documentation.

import { Menu } from '@headlessui/react'

function App() {
  return (
    <Menu>
      <Menu.Button>More</Menu.Button>
      <Menu.Items>
        <Menu.Item>
          {({ active }) => (
            <a
              className={`${active && 'bg-blue-500'}`}
              href="/account-settings"
            >
              Account settings
            </a>
          )}
        </Menu.Item>
        <Menu.Item>
          {({ active }) => (
            <a
              className={`${active && 'bg-blue-500'}`}
              href="/account-settings"
            >
              Documentation
            </a>
          )}
        </Menu.Item>
        <Menu.Item disabled>
          <span className="opacity-75">Invite a friend (coming soon!)</span>
        </Menu.Item>
      </Menu.Items>
    </Menu>
  )
}

export default App 

In the code snippet, the “active” property is used as an argument to the function.

When “active” is true, the className property of the “a” element will be set to “bg-blue-500”, giving it a blue background color. Conversely, when “active” is false, the className property will be set to an empty string, removing the background color.

Also, this code provides improved functionality for instance allowing users to navigate the dropdown menu using their keyboard, closing the dropdown by clicking outside, and many more.

Now that we have a functional dropdown menu, it’s time to add some styling. Since we have installed TailwindCSS, we can use it to add style to our menu.

import { Menu } from '@headlessui/react'

function App() {
 return (
   <div className="shadow-md bg-white w-48">
     <Menu>
       <Menu.Button className="px-4 py-2 text-sm font-medium text-gray-700">
         More
       </Menu.Button>
       <Menu.Items className="py-2 text-sm font-medium text-gray-700">
         <Menu.Item>
           {({ active }) => (
             <a
               className={`block px-4 py-2 ${active ? 'bg-blue-500 text-white' : 'text-gray-700'
                 }`}
               href="/account-settings"
             >
               Account settings
             </a>
           )}
         </Menu.Item>
         <Menu.Item>
           {({ active }) => (
             <a
               className={`block px-4 py-2 ${active ? 'bg-blue-500 text-white' : 'text-gray-700'
                 }`}
               href="/account-settings"
             >
               Documentation
             </a>
           )}
         </Menu.Item>
         <Menu.Item disabled>
           <span className="block px-4 py-2 text-gray-400">
             Invite a friend (coming soon!)
           </span>
         </Menu.Item>
       </Menu.Items>
     </Menu>
   </div>
 )
}

export default App

Here is the output:

Output

The customizability doesn’t stop here — Headless UI also allows you to further tailor the functionality of your dropdown menu. For example, you can manually control when the menu opens and closes by passing the open andclose props to the relevant components.

For instance, you can check the code below which uses the open andclose props.

import { Menu } from '@headlessui/react'

function App() {
 return (
   <Menu>
     {({ open }) => (
       <>
         <Menu.Button>More</Menu.Button>
         {open && (
           <div>
             <Menu.Items static>
               <Menu.Item>
                 {({ active }) => (
                   <a
                     className={`${active
                       ? 'bg-gray-100 text-gray-900'
                       : 'text-gray-700'
                       } block px-4 py-2 text-sm`}
                     href="/account-settings"
                   >
                     Account settings
                   </a>
                 )}
               </Menu.Item>
               <Menu.Item>
                 {({ active }) => (
                   <a
                     className={`${active
                       ? 'bg-gray-100 text-gray-900'
                       : 'text-gray-700'
                       } block px-4 py-2 text-sm`}
                     href="/documentation"
                   >
                     Documentation
                   </a>
                 )}
               </Menu.Item>
               <Menu.Item disabled>
                 <span className="block px-4 py-2 text-sm text-gray-400">
                   Invite a friend (coming soon!)
                 </span>
               </Menu.Item>
             </Menu.Items>
           </div>
         )}
       </>
     )}
   </Menu>
 )
}

export default App

And there are more such components API that Headless UI provides.

With these powerful features, you can create a truly bespoke dropdown menu that meets all of your unique requirements.

Unleashing the Power of Headless UI

Speaking about integration, Headless UI can be easily integrated into React and Next.js applications. And you can seamlessly add styles to your components with TailwindCSS as well.

Another key benefit of using Headless UI is its excellent accessibility features like focus management, mouse interaction, and keyboard interaction.

Doc

Also, the library has taken care of all relevant ARIA attributes and provided several other props like “active”, “open”, “close”, “as”, “unmount”, and many more, to help you further customize the components as per your needs.

So, whether you want to build a simple dropdown menu or a more complex UI component, Headless UI provides all the necessary functionality and accessibility features to make your development process easier and more efficient.

While Headless UI is great for building accessible UI components that work out-of-the-box and integrate seamlessly with TailwindCSS, you still need a responsive React codebase with TailwindCSS to get started.

You can generate responsive code directly from your design files in Figma and Adobe XD using the Locofy.ai plugin.

Locofy automatically picks up your Figma auto layout settings and constraints to generate responsive code with flex properties. Additionally, you can choose to use TailwindCSS as the default styling option as well so your Locofy-generated code will utilize TailwindCSS for handling styles.

You can then use these Headless UI components to incorporate accessible drop downs, auto-complete, tabs, and other such components.

Hope you like it.

That’s it — thanks.

...



📌 Say Goodbye to Boring Dropdowns: Create Custom Dropdown Menus with Headless UI


📈 145.61 Punkte

📌 Issue of screenshots of dropdown menus in linux (both printscreen and using flameshot)


📈 40.8 Punkte

📌 Running a headless VM inside a headless server


📈 34.57 Punkte

📌 Chrome’s Headless mode gets an upgrade: introducing `--headless=new`


📈 34.57 Punkte

📌 Say Goodbye to Busywork, Say Hello to Watson


📈 33.76 Punkte

📌 Say Goodbye to Busywork, Say Hello to watsonx Orchestrate


📈 33.76 Punkte

📌 Say Goodbye to Busywork, Say Hello to watsonx Orchestrate


📈 33.76 Punkte

📌 How to Build an Accessible Custom Dropdown Select Element


📈 31.87 Punkte

📌 Custom Dropdown with HTML,CSS and JS


📈 31.87 Punkte

📌 Goodbye Boring PLAXIS Output with Python


📈 30.84 Punkte

📌 Bootstrap 5 Dropdowns Menu content Headers


📈 30.74 Punkte

📌 Bootstrap 5 Dropdowns Menu content Forms


📈 30.74 Punkte

📌 Excel-Dropdowns: So funktionieren sie optimal!


📈 30.74 Punkte

📌 Google Drive: Neue Suchfunktion für die Android-App kommt – Karussells werden durch Dropdowns ersetzt


📈 30.74 Punkte

📌 How to create a form within dropdown menu using Bootstrap ?


📈 30.44 Punkte

📌 How to create a dropdown menu with HTML and CSS


📈 30.44 Punkte

📌 Custom Start Menus in Roaming Profiles Reset After Windows 10 Upgrades


📈 26.66 Punkte

📌 React Native PSA - select and highlight text with custom context menus


📈 26.66 Punkte

📌 5+ Tips For Building Custom Headless CMS


📈 26.15 Punkte

📌 Open Source Developers Say Securing Their Code Is 'Insufferably Boring' and 'Soul-Withering'


📈 26.01 Punkte

📌 How to Create and Manage Menus in WordPress


📈 25.23 Punkte

📌 How you can create your own custom chatbot with your own custom data using Google Gemini API all for free


📈 25.17 Punkte

📌 Create a Headless CMS Using OceanBase and TypeScript: A Step-By-Step Tutorial


📈 24.72 Punkte

📌 Say goodbye to enhanced data privacy, US web surfers


📈 24.12 Punkte

📌 It’s time to finally say goodbye to Windows XP. And Vista. Again


📈 24.12 Punkte

📌 Windows Hello Learns To Say Goodbye In Windows 10 Creators Update


📈 24.12 Punkte

📌 Never can say goodbye: face scans for departing US visitors fast-tracked


📈 24.12 Punkte

📌 Say Goodbye to SMBv1 in Windows Fall Creators Update


📈 24.12 Punkte

📌 StalinLocker ransomware: Put unlock code or say goodbye to your data


📈 24.12 Punkte

📌 Wanted to say goodbye to Windows before switching to Linux 100%, but the bluescreen crashed


📈 24.12 Punkte

📌 Say Goodbye to CoreOS


📈 24.12 Punkte

📌 Say goodbye to browser ads and malware with this $30 tool


📈 24.12 Punkte

📌 Heinz: It's Time to Say Goodbye to These Obsolete Python Libraries


📈 24.12 Punkte











matomo