Having trouble with the Tailwind transition in my Next.js component

I'm facing an issue with the ease out transition not working as expected. I need the side to slide in from left to right with a duration of 500ms. Despite trying various solutions, I can't seem to figure out why it's not functioning properly. Upon inspecting in the dev tool, the transition class is being applied correctly but the effect doesn't display. Here is my code:

    "use client"
import Image from "next/image"
import React, { useState } from 'react'
import profile from '../../assets/images/profile.png'
import { buttonprops } from "@/data/Data";
import NavLinks from "./NavLinks/NavLinks";
import { ButtonType } from "@/types/button";
import { faBars } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

const Navbar = () => {
    const [isVisible, setIsVisible] = useState(false);
    const toggleVisibility = () => {
        console.log("item is being clicked and vlaue of visibie is " + isVisible)
        setIsVisible(!isVisible);
    };

    const handleMouseClick = () => {
        if (isVisible) {
            setIsVisible(!isVisible);
        }
    }

    return (
        <div className="flex flex-row">
            <div className={`flex border border-black `}>
                <div className="flex row border border-black md:hidden">
                    <div className="flex w-44 h-14 border items-center border-black pl-4">
                        <FontAwesomeIcon className="text-3xl" onClick={toggleVisibility} icon={faBars} />
                    </div>
                </div>

                <div className={`md:block w-44 h-screen border border-black text-white ${isVisible ? 'absolute transition w-80 duration-300 ease-in-out' : 'hidden'} `}>
                    <div className="flex flex-col items-center pt-5 pb-10 border bg-slate-200">
                        <Image src={profile} width={500} height={500} className="rounded-full w-32 h-32 border-4  border-blue-500" alt="Picture of the author" />
                    </div>

                    <div style={{ backgroundColor: '#222e50' }} className="flex flex-col h-full ">
                        {buttonprops.map((data, i) => {
                            return <NavLinks key={i} name={data.name} icon={data.icon} />;
                        })}
                    </div>
                </div>

            </div>

            <div onClick={handleMouseClick} className="border border-black w-full h-screen">
                <div className="flex border h-14 w-full border-black items-center justify-center md:hidden">
                    <div className="hi"> Data </div>
                </div>
                <div className="w-full">
                    Content
                </div>
            </div>
        </div>
    )
}

export default Navbar

Answer №1

Avoid conditionally adding the transition class, as it will not activate at the same time as the style changes.

<div className={`md:block w-44 h-screen border border-black text-white transition ${isVisible ? 'absolute w-80 duration-300 ease-in-out' : 'hidden'} `}>
...
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I overlay text on top of an image?

I want to create a design where half of the text overflows and overlays the image on the right side. How can I achieve this using a Grid item? I've tried using "absolute", "relative", and "z-index" positions, but they don't seem to work in the MU ...

Creating a robust setup for integrating nextjs and nestjs within an nx monorepo using pnpm package manager

I am considering building my application with Next.js for the frontend and NestJS for the backend, all within an NX monorepo using the Pnpm workspace. I'm uncertain about how to structure the folders for this project. Should I opt for a package-based ...

What is the best way to overlay text onto a background image using Material UI's Card and CardMedia components?

Using the card component, I have successfully added a background image. However, I am facing difficulty in figuring out how to overlay text on top of this image. If anyone has suggestions or alternative methods, please feel free to share! <Card> ...

What is the method for verifying authentication status on a Next.js page?

I'm struggling to understand why the call to auth.currentUser in the code snippet below always returns null. Interestingly, I have another component that can detect when a user is logged in correctly. import { auth } from "../lib/firebase"; ...

What is the best way to style the currently selected option element in a select dropdown?

I am trying to change the font color of specific option elements within a select dropdown by adding style="color: #D4D4D4". When I apply this style directly to an option element like <option value="TEST" style="color: #D4D4D4">TEST</option>, it ...

Issue with dropdown menu display in Internet Explorer

Having trouble with a CSS dropdown menu displaying incorrectly in IE, while working fine on other browsers. <body> <div id="container"> <header> <div id="hlogo"> <a href="index.html">title ...

Updating the state of a Next.JS router component with React.JS: A step-by-step guide

I've implemented a toggleswitch in my app that changes its state based on the dynamic URL parameters. For instance, if the URL includes a parameter named followType set to following (e.g. https://www.example.com/home?followType=following), I want the ...

Next.js Error: The text displayed on the page differs from the HTML rendered by the server

Currently, I am facing an issue with the error "Error: Text content does not match server-rendered HTML." Here is my scenario - I have received a dateTime from an API, for example '2022-12-25T11:22:37.000Z', and I want to display it on a compone ...

`Nextjs customizes the position of locales`

Currently implementing i18n translation in my project, the default root appears as follows: https://example.com/en/business/transaction Is it possible to customize the root to appear as: https://example.com/business/en/transacation Thank you. ...

How can recursive data be displayed in a template?

I am working with a model in Django that has a ForeignKey pointing to itself, and I need to display all the data from the database using lists and sublists: Below is my model definition: class Place(models.Model) name = models.CharField(max_length=1 ...

Lack of animation on the button

Having trouble with this issue for 48 hours straight. Every time I attempt to click a button in the top bar, there is no animation. The intended behavior is for the width to increase and the left border color to change to green, but that's not what&ap ...

Steps on modifying the background of an element based on its location within the browser:

In the past, I've come across some impressive one-page websites with elements that would appear as you scroll. It seemed like they were created using just CSS. I think it might be achievable with the z-index and position properties? However, I can&ap ...

Encountering a SyntaxError while implementing lightweight-charts in NextJS

I'm encountering an issue while trying to integrate the lightweight-charts package into my nextjs project. When attempting to utilize the createChart function, I am receiving a syntax error in my Node.js console. ...\lightweight-charts\dist& ...

What steps can be taken to automatically set a button in a navigation bar to active when its corresponding route is clicked on?

I'm trying to implement a feature in my react app where an SVG button in the navbar changes color when its route is active or switches colors based on which button's route was selected. For example, clicking on the settings button would change it ...

Seamless movements to the exact midpoint

I am trying to find a method to create a smooth transition when removing the center class from p.title.class, allowing it to smoothly move to its new position. JSfiddle Here is an excerpt of my HTML: <body> <div class="wrapper-top"> ...

What is the best way to call a JavaScript function within a PHP echo statement that outputs a <div> element?

Having trouble echoing this PHP code due to issues with single quotes, causing the HTML to end prematurely. Any suggestions on how to fix this? function button($conn){ $sql = "SELECT * FROM table"; $result= mysqli_query($conn, $sql); while($r ...

Managing image alignment in flexbox containers with nested rows and columns

In the following design demonstration, there are three blocks to explore. Block 1: Consists of three columns. The middle column contains two rows, both set to flex:1. Block 2: Also has three columns. The middle column includes two rows, but with a unique ...

What is the reason that setting the margin of 0 on the body does not eliminate the margin on an h1 element?

I believe the body element should apply styles to all elements contained within it. In this scenario, I am attempting to give my h1 element a margin of 0. body { font: 15px/1.5 Arial, Helvetica, sans-serif; padding: 0; margin: 0; background ...

What is the reason for the excessive width of the final column in this table?

I am currently working with a dataset that I am displaying using the handsontable library in the form of a table. One issue I am facing is that the last column of my table appears too wide even though I did not specify any width for it. Below you can see t ...

Encountering an endless loop while attempting to retrieve data from Firebase in Next.js through the use of useEffect

Currently, I am in the process of setting up a video section for a project using NextJS. The videos are stored in firebase storage. I have implemented a dynamic route that retrieves all videos from a specific reference within the bucket. For instance, if ...