search

Home  >  Q&A  >  body text

The rewritten title is: How can I use Tailwind CSS to create animated dropdown menu with adjustable height?

I'm using: Tailwindcss, React and Next.js for side projects.

I'm trying to create a responsive navigation bar that displays a hamburger menu when the screen size reaches the "sm" size defined by tailwind.

When I click on the hamburger icon, I want the menu to transition from height 0 to max-h-40.

I feel like I'm missing something trivial in the code below, hopefully others looking at this can see what I'm missing?

navbar.tsx

"use client";

import Image from "next/image";
import Link from "next/link";
import {
  useState
} from "react";
import logo from "../public/finallang_favicon.ico";

export default function Navbar() {
  const [showMenu, setShowMenu] = useState(false);
  const toggleMenu = () => {
    setShowMenu(!showMenu);
  };
  return ( <
    div >
    <
    nav className = "flex items-center justify-between flex-grow w-auto py-3 text-center border-b px-9 sm:w-auto" >
    <
    div className = "flex items-center justify-center flex-shrink-0 sm:mr-6" >
    <
    Link href = "/" >
    <
    Image src = {
      logo
    }
    alt = "Logo"
    width = {
      48
    }
    height = {
      48
    }
    /> <
    /Link> <
    /div> <
    div className = "hidden text-sm sm:block" >
    <
    Link href = "#"
    className = "block mt-4 sm:mr-4 text-slate-900 hover:text-slate-700 sm:mt-0 sm:inline-block" >
    About <
    /Link> <
    Link href = "#"
    className = "block mt-4 sm:mr-4 text-slate-900 hover:text-slate-700 sm:mt-0 sm:inline-block" >
    Blog <
    /Link> <
    Link href = "#"
    className = "block mt-4 text-slate-900 hover:text-slate-700 sm:mt-0 sm:inline-block" >
    Contact Me <
    /Link> <
    /div> <
    div >
    <
    button className = "hidden px-4 py-2 text-sm leading-none rounded text-slate-100 hover:text-white sm:inline-block bg-brand" >
    Download <
    /button> <
    button onClick = {
      toggleMenu
    }
    aria - label = "Toggle navigation menu"
    className = "text-gray-400 align-middle sm:hidden hover:text-gray-900 focus:ring-2 rounded-md" >
    <
    svg xmlns = "http://www.w3.org/2000/svg"
    fill = "none"
    viewBox = "0 0 24 24"
    strokeWidth = {
      2
    }
    stroke = "currentColor"
    className = "w-6 h-6" >
    <
    path strokeLinecap = "round"
    strokeLinejoin = "round"
    d = "M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" / >
    <
    /svg> <
    /button> <
    /div> <
    /nav> {
      showMenu &&
        <
        div className = {
          `${showMenu ? "max-h-40" : "h-0"} text-sm text-center sm:hidden transition-all duration-500 ease-in-out overflow-hidden`
        } >
        <
        Link href = "/about"
      className = "block mt-4 text-slate-900 hover:text-slate-700" >
        About <
        /Link> <
        Link href = "/blog"
      className = "block mt-4 text-slate-900 hover:text-slate-700" >
        Blog <
        /Link> <
        Link href = "/contact"
      className = "block mt-4 text-slate-900 hover:text-slate-700" >
        Contact Me <
        /Link> <
        /div>
    } <
    /div>
  );
}

Things I've Tried:

Current behavior: GIF of current behavior

What I Expect to Happen:

P粉667649253P粉667649253280 days ago511

reply all(1)I'll reply

  • P粉216807924

    P粉2168079242024-02-18 10:08:48

    Explanation of the problem

    DOM mount

    Conditional rendering via code snippet:

    {showMenu &&
      

    Indicates that the element is mounted into the DOM or mounted outside the DOM. The transition does not play on the same frame as the element is mounted/unmounted.

    CSS Property Transformation

    Also, you can change different CSS properties using the menu container's conditional class:

    ${showMenu ? "max-h-40" : "h-0"}
    

    max-h-40 corresponds to max-height: 10rem and h-0 corresponds to height: 0. This means we want to change the initial values ​​of two values: max-height and height. According to MDN, the initial value of max-height is none and the initial value of height is auto< /a>. These values ​​change relative to showMenu as follows:

    showMenu true false
    max-height 10rem none
    height auto 0

    reply
    0
  • Cancelreply