The rewritten title is: How can I use Tailwind CSS to create animated dropdown menu with adjustable height?
P粉667649253
P粉667649253 2024-02-17 11:54:04
0
1
425

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:

  • Added height: "height" as transitionProperty to my tailwind.config.js
  • Add the possibly missing overflow-hidden class name to my menu class
  • Switch between transition-all and transition-[height] in the class of the drop-down menu

Current behavior: GIF of current behavior

What I Expect to Happen:

  • The dropdown should transition from height 0 to a maximum height of 10rems (max-h-40) within a duration of 500 milliseconds, using the ease-in transition timing function.

P粉667649253
P粉667649253

reply all(1)
P粉216807924

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. These values ​​change relative to showMenu as follows:

showMenu true false
max-height 10rem none
height auto 0
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!