Having trouble implementing a transition on a dropdown menu in React

Can anyone help me troubleshoot an issue with adding a transition to a select box that appears when clicking on an input field arrow? I have tried implementing a CSS transition property, but it doesn't seem to be working. Any suggestions on what might be causing this problem?

.combobox-options {
  position: absolute;
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 15em;
  overflow-y: auto;
  overscroll-behavior: contain;
  width: 100%;
  left: 0;
  top: calc(100% + 0.25em);
  z-index: 100;
  transition: all 0.5s;
}
    const [isOpen, setIsOpen] = useState(false);

    <input role="combobox"  className="combobox-input"/>
    <button
        className={clsx("arrow", isOpen && "down")}
        onClick={() => setIsOpen(!isOpen)}>
    </button>
    {isOpen && (
       <ul
          id="combobox-listbox"
          role="listbox"
          className={clsx("combobox-options")}
        >
            {options?.map((option: any, index: any) => {
              return (
                <li>...</li>
              );
            })}
        </ul>
    )}

Answer №1

When using javascript to show and hide the ul element, it is not recommended to apply css. Instead of wrapping the ul element with isOpen, toggle between different CSS classes.

Refer to the example below for guidance on restructuring your code:

className={clsx("combobox-options", !isOpen && "hide")}
.combobox-options {
  /* position: absolute; */
  margin: 0;
  padding: 0;
  list-style: none;
  height: 15em;
  overflow: hidden;
  overscroll-behavior: contain;
  width: 100%;
  left: 0;
  top: calc(100% + 0.25em);
  z-index: 100;
  transition: all 0.5s ease-in-out;
}

.hide {
  height: 0;
  visibility: hidden;
}

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

Unable to display nested React components on GitHub Pages

Currently, I am in the process of developing a single page website and deploying it via GitHub Pages. However, for some reason, the content from the nested components (About and Skills) is not displaying on the site. I have been unable to identify what mig ...

Angular // binding innerHTML data

I'm having trouble setting up a dynamic table where one of the cells needs to contain a progress bar. I attempted using innerHTML for this, but it's not working as expected. Any suggestions on how to approach this? Here is a snippet from my dash ...

Angular: Assigning a key from one variable to a value in another

I am currently facing a challenge with rendering a form on a page using ng-repeat, where the data for this form is dynamically fetched from a request. Within this data, there is a nested array called "categories" which contains IDs. I want to display the n ...

Validating image uploads using Yup and Formik in a standalone module

I am facing an issue in my Next.js application where I am attempting to validate the upload of an image using Formik and Yup by passing a method called handleOnImageChange in the component. However, I am struggling to make it work this way. I have also tri ...

Is it possible to apply a click effect to a specific child element of a parent using jQuery?

Struggling to figure out how to modify this jQuery code so that only the parent of the clicked button displays its child. Currently, all children are displayed when any button is clicked, not just the one within the targeted parent. I attempted using $(t ...

Client-Side Isomorphic JS: A Guide to Using HTTP Requests

Seeking advice on data population for isomorphic flux apps. (Using react, alt, iso, and node but principles are transferable) In my flux 'store' (), I need to fetch data from an api: getState() { return { data : makeHttpRequest(url) ...

Enhance your Ag-Grid experience with paginated data storage

How can paginated data of Ag-Grid be stored effectively? Include features like GotoPage10, GotoPage50, etc. Retrieve only the corresponding data when navigating to Page10 or Page50 from backend Need to retain this data for random pages such as 1, 15, 20 ...

Retrieve the element that triggered the event listener within Nuxt

I am currently developing a Nuxt project where I have set up my component using the code below. The select-parent-container has a click event attached to it. Whenever this event is triggered, I need to identify and return the specific parent container that ...

Another return payload failing to retrieve the return value

I'm currently facing an issue where a function that should return a value is not being passed on to another function. Below is the code snippet in question: public _getProfileToUpdate() { return { corporateId: this.storeService.setStoreData().p ...

What is the best way to pass an email as a Laravel parameter within a function using Angular?

I'm currently working on a feature that allows users to delete their account only if the input field matches their email address. However, I encountered an error: Error: $parse:lexerr Lexer Error when attempting to set this up. Here is my HTML code: ...

Utilize context and custom hooks in React to efficiently handle the current user's management

Implementing two methods, login and logout, has been a goal of mine. I've been working on sharing the current user's data through context to access those details from any component. In my App component, I have integrated the useUser custom hook ...

The subdirectory containing the Rails assets is causing loading issues in the production environment

My event CSS is currently located in app/assets/stylesheets/pages/events.scss. Surprisingly, everything looks perfect in development mode with all the assets loading properly. However, the real issue arises when I switch to production as it seems like thes ...

There seems to be an issue with the functionality of the AJAX file upload feature in

I've developed a method for file uploading using AJAX technology (pure JavaScript) in CodeIgniter. Here's how it works: 1- I created a script.js file to manage the AJAX/JavaScript upload process. 2- I implemented a controller in CodeIgniter to ...

Preventing NextJS Link Component from Displaying 404 Error

Hey there, can anyone help me figure out why my Link Component is having trouble finding the linked page? It's weird because VSCode is auto-completing the file name as I type it in, but all I get is a 404 error. //index.js in WelcomePage folder import ...

Creating Vue3 Component Instances Dynamically with a Button Click

Working with Vue2 was a breeze: <template> <button :class="type"><slot /></button> </template> <script> export default { name: 'Button', props: [ 'type' ], } </scr ...

Incorporating HTML into a WordPress Plugin

I am currently working on my very first WordPress plugin. The main purpose of this plugin is to display a message on the plugin page whenever a user saves or edits pages. However, I'm experiencing some issues with printing out these messages. Here&ap ...

Issue encountered while attempting to utilize the useRef function on a webpage

Is it possible to use the useRef() and componentDidMount() in combination to automatically focus on an input field when a page loads? Below is the code snippet for the page: import React, { Component, useState, useEffect } from "react"; import st ...

What are some ways to customize the drop down colors of a material ui select menu?

I am struggling to change the background color of the dropdown menu but all my attempts so far have been unsuccessful, leaving me puzzled. Despite my efforts in searching for solutions online, nothing seems to be working. That's really all there is t ...

Position of the helper text in Material-UI TextField using React.js

Currently, I am developing a project using React JS and incorporating the Material UI library to manipulate table rows. However, I'm facing an issue with aligning text fields alongside other fields. Whenever the helper text appears, the text field see ...

NuxtJS - Google Auth: ExpiredAuthSessionError: The authentication session has expired because both the token and refresh token have expired. Please try your request

I've been using nuxtjs for some time now. Everything related to authentication was working smoothly until my last update. However, after updating the nuxt.config.js file and moving the axios plugin from the plugins array to the auth config object, an ...