<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: An Phan</title>
    <description>The latest articles on Forem by An Phan (@anphan).</description>
    <link>https://forem.com/anphan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F138577%2Fb31f3883-9ac8-4599-85a3-69a1ca948eef.png</url>
      <title>Forem: An Phan</title>
      <link>https://forem.com/anphan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anphan"/>
    <language>en</language>
    <item>
      <title>How to create the reused drop-down menu?</title>
      <dc:creator>An Phan</dc:creator>
      <pubDate>Tue, 04 Apr 2023 16:12:39 +0000</pubDate>
      <link>https://forem.com/anphan/how-to-create-the-reused-drop-down-menu-1opc</link>
      <guid>https://forem.com/anphan/how-to-create-the-reused-drop-down-menu-1opc</guid>
      <description>&lt;p&gt;Here's a basic example of how to create a dropdown menu using Next.js and Tailwind CSS:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, install Tailwind CSS and its dependencies by running the following command in your terminal:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install tailwindcss postcss-preset-env postcss-flexbugs-fixes autoprefixer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Next, create a &lt;code&gt;tailwind.config.js&lt;/code&gt; file in your project's root directory and add the following code to it:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  mode: 'jit',
  purge: ['./pages/**/*.{js,jsx,ts,tsx}', './components/**/*.{js,jsx,ts,tsx}'],
  theme: {},
  variants: {},
  plugins: [],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This sets up Tailwind CSS to work with Next.js, enables the &lt;code&gt;jit&lt;/code&gt; mode for faster builds, and specifies which files to purge when generating the final CSS file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now create a new component for the dropdown menu. For example, you can create a &lt;code&gt;DropdownMenu&lt;/code&gt; component in a file named &lt;code&gt;DropdownMenu.js&lt;/code&gt;. Here's a basic example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from 'react'
import Link from 'next/link'

const DropdownMenu = ({ items }) =&amp;gt; {
  const [isOpen, setIsOpen] = useState(false)

  const toggleMenu = () =&amp;gt; {
    setIsOpen(!isOpen)
  }

  return (
    &amp;lt;div className="relative"&amp;gt;
      &amp;lt;button
        type="button"
        onClick={toggleMenu}
        className="inline-flex items-center justify-center w-full px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
        aria-haspopup="true"
        aria-expanded={isOpen}
      &amp;gt;
        Menu
        &amp;lt;svg
          className="-mr-1 ml-2 h-5 w-5"
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 20 20"
          fill="currentColor"
          aria-hidden="true"
        &amp;gt;
          &amp;lt;path
            fillRule="evenodd"
            d="M10 14l-5-5h10l-5 5z"
          /&amp;gt;
        &amp;lt;/svg&amp;gt;
      &amp;lt;/button&amp;gt;

      &amp;lt;div
        className={`${
          isOpen ? 'block' : 'hidden'
        } absolute right-0 w-56 mt-2 origin-top-right bg-white border border-gray-200 divide-y divide-gray-100 rounded-md shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none`}
        role="menu"
        aria-orientation="vertical"
        aria-labelledby="options-menu"
      &amp;gt;
        {items.map((item) =&amp;gt; (
          &amp;lt;Link href={item.href} key={item.label}&amp;gt;
            &amp;lt;a
              className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900"
              role="menuitem"
              tabIndex="-1"
              onClick={() =&amp;gt; setIsOpen(false)}
            &amp;gt;
              {item.label}
            &amp;lt;/a&amp;gt;
          &amp;lt;/Link&amp;gt;
        ))}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default DropdownMenu

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;DropdownMenu&lt;/code&gt; component takes an array of &lt;code&gt;items&lt;/code&gt; as a prop, which contains objects with href and label properties for each menu item. When the user clicks on the menu button, the isOpen state is toggled to show or hide the menu items.&lt;/p&gt;

&lt;p&gt;The menu button and items are styled using Tailwind CSS classes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Finally, you can use the &lt;code&gt;DropdownMenu&lt;/code&gt; component in your Next.js pages by importing it and passing in the necessary props. For example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import DropdownMenu from '../components/DropdownMenu'

const items = [
  { label: 'Home', href: '/' },
  { label: 'About', href: '/about' },
  { label: 'Contact', href: '/contact' },
]

const IndexPage = () =&amp;gt; {
  return (
    &amp;lt;div className="container mx-auto"&amp;gt;
      &amp;lt;DropdownMenu items={items} /&amp;gt;
      &amp;lt;h1 className="text-2xl font-bold text-center mt-8"&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default IndexPage

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example imports the &lt;code&gt;DropdownMenu&lt;/code&gt; component and passes in an array of &lt;code&gt;items&lt;/code&gt; to display in the menu. The menu is then rendered above the page title.&lt;/p&gt;

&lt;p&gt;That's it! With these steps, you should now have a basic dropdown menu working in your Next.js app using Tailwind CSS for styling. Of course, you can customize the styles and behavior to fit your specific needs.&lt;/p&gt;

&lt;p&gt;Related Post: &lt;a href="https://anphan.com/embrace-constraints"&gt;Embrace constraints to more creativity and concentration&lt;/a&gt;&lt;br&gt;
Author: &lt;a href="https://anphan.com"&gt;An Phan&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>The first post</title>
      <dc:creator>An Phan</dc:creator>
      <pubDate>Fri, 29 Oct 2021 08:20:43 +0000</pubDate>
      <link>https://forem.com/anphan/the-first-post-4hl5</link>
      <guid>https://forem.com/anphan/the-first-post-4hl5</guid>
      <description>&lt;h2&gt;
  
  
  This is the first post.
&lt;/h2&gt;

&lt;p&gt;This is the testing post.&lt;/p&gt;

&lt;p&gt;Related posts:&lt;br&gt;
&lt;a href="https://dev.to/anphan/how-to-create-the-reused-drop-down-menu-1opc"&gt;How to create the reused drop-down menu?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/indie-hacker"&gt;Indie Hacker là ai và họ làm gì?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/tiep-thi-thong-qua-giang-day"&gt;Tiếp thị thông qua Giảng dạy&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/tai-tao-chinh-minh"&gt;Không bao giờ quá trễ để theo đuổi ước mơ và tái tạo chính mình&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/sang-tao-hon"&gt;Tại sao nên sáng tạo hơn trong cuộc sống&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/rush"&gt;Rust là tương lai của cơ sở hạ tầng JavaScript&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/song-duoi-muc-thu-nhap-d"&gt;Sống dưới mức thu nhập mà bạn kiếm được&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/thoi-quen"&gt;Sự quan trọng của thói quen&lt;/a&gt;&lt;br&gt;
&lt;a href="https://anphan.com/vi/ghi-chep-thong-minh"&gt;Phương pháp ghi chép thông minh Cornell&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
