The div element is animated to open from bottom to top with a sliding effect, rather than the traditional top to

Within my component, there is a button and a div that contains an input element. The intended behavior is for the div to slide down when the button is clicked, revealing the input field. Clicking the button again should slide the div back up to close it.

However, the current issue is that when the button is clicked, the div opens by sliding up instead of down.

To achieve this animation effect, I am using CSS properties like max-height and will-change. How can I correct this unexpected behavior?

You can view the CodeSandbox example here.

Here's the Component code:

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [ isOpen, setIsOpen ] = useState( false );

  const handleOpen = () => {
    setIsOpen( !isOpen );
  };

  return (
    <div className="App">

      <button onClick={handleOpen}>Open</button>

      <div className={`container ${isOpen ? "containerOpened" : "containerClosed"}`}>
        <input type="text" />
      </div>

    </div>
  );
}

And here's the corresponding CSS:

.App {
  font-family: sans-serif;
  text-align: center;
  position: relative;
}

.container {
  position: absolute;
  bottom: -46px;
  right: 0;
  margin-right: 30px;
  padding: 10px 20px;
  background: #fff;
  border: 1px solid #e2e2e2;
  transition: all 0.3s ease-in-out;
}

.containerClosed {
  overflow: hidden;
  max-height: 0;
  transition: all 0.3s ease;
  will-change: transform;
}

.containerOpened {
  max-height: 100px;
  overflow: hidden;
  transition: all 0.3s ease;
  will-change: transform;
}

.container input {
  border: none;
  outline: none;
  border-bottom: 1px solid #e2e2e2;
  width: 200px;
}

Note that the use of absolute positioning with margin padding on the container is necessary in this case.

Answer №1

Give this a shot:

.App {
  font-family: sans-serif;
  text-align: center;
  position: relative;
}

.container {
  position: absolute;
  top: 46px;
  right: 0;
  margin-right: 30px;
  padding: 0;
  background: #fff;
  border: 1px solid #e2e2e2;
  transition: all 0.3s ease-in-out;
}

.containerClosed {
  overflow: hidden;
  height: 0;

  
}

.containerOpened {
  max-height: 100px;
  overflow: hidden;
  height: 100px
  
}

.container input {
  border: none;
  outline: none;
  border: 1px solid #e2e2e2;
  width: 200px;
}

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

Vanishing border around the sticky table header

While working on creating a table in an excel style using html and css with a sticky header, I noticed that the borders on the table head appeared strange. Take a look at the code snippet below: table { border-collapse: collapse; position: relative ...

Having difficulty deleting hyphens within a textbox in Chrome on an Android device

Within my React application, I have an input field with the type being "text". The specific requirements for this input are as follows: The user's input should be automatically capitalized. After the 7th and 13th character, a hyphen "-" should be ins ...

Angular2 - Incorporating a New Attribute

I am working with the following Angular2 code: <ngx-datatable-column prop="id" name="ID"> <template ngx-datatable-cell-template let-row="row" let-value="value"> <a [routerLink]="['/devicedtls',r ...

Incorporate JSON when adding a new row to the database using Ruby On Rails

Greetings, fellow developers! I am currently working on an application with a backend in Rails. My goal is to create a user from an AJAX request and have the server return a JSON object containing the newly saved user information. Below is my Rails code s ...

What is causing the issue with Vue.js :class not functioning properly when it relies on a property of a list

Here is a snippet of my HTML code: <tr v-for="product in products" :class="{'bg-red': product.toWrite }" :key="product.name"> <td @click="setObjectToWrite(product.name)" class="show-hover&qu ...

automatically collapse a submenu once a different menu option is selected

After doing some research and trying out various solutions, I still couldn't get it to work. I made adjustments to my dropdown menu and click function so that each submenu opens and closes when its parent is clicked. However, I'm now looking to f ...

Issue: The use of destructuring props is required by eslint, and I'm currently working with a combination of react and types

I typically work with React in JavaScript, and I recently encountered an error message stating "Must use destructuring props at line 14" while trying to define a button for use in a form. Here is the code snippet in question: import React from 'react& ...

The Animation on Scroll (AOS) feature is currently malfunctioning within the upcoming 13 releases

I am currently using Next.js 13 and I want to integrate the AOS library into my application. Although I have installed it properly and added it to my component, I am facing an issue where my component is not displaying and no errors are being shown. Here i ...

Utilizing the Bootstrap grid system to dynamically adjust column width based on specific conditions

When working with HTML and bootstrap (3.7) CSS, I often come across this issue: <div class="row"> <div class="col-xs-6">Stretch ME!</div> <div class="col-xs-6">Conditional</div> </div> Sometimes the "Conditional" d ...

Error: The function nodemailer.createTransport is not defined or does not exist

I'm currently working on integrating nodemailer into my nodejs application for sending emails. Check out the code below: var express = require('express'); var nodemailer = require('node-mailer'); var app = express(); app.post(&a ...

Eliminate the delicate border in header images using bootstrap

I am struggling with some code that includes bootstrap and an SVG sprite in a header/menu, but there is a thin gray background line appearing in the icons. Here is the link to the code on CodePen Can anyone please help me solve this issue? I've trie ...

The RSS feed reader is currently malfunctioning

I'm having trouble loading an RSS feed from the following URL: http://solutions.astrology.com/scripts/it.dll?cust_id=aamfha&doc=daily07short/dailyshort.xml Here is the code I am using to try to read it: url = 'http://solutions.astrology.co ...

Improving the Roman Numeral Kata with JavaScript

As a newcomer to the world of coding, I have taken on the challenge of mastering the Roman Numeral Kata using Javascript. I am pleased to report that all the specifications are passing successfully. After refactoring the spec file, I am now focusing on re ...

What sets React apart: Is there a distinction between directly passing the useState hook to the parent component compared to passing a function that calls useState?

What is the difference in performance between directly passing the useState hook to a parent component versus passing a function that calls the useState setter? const Parent = () => { const [name, setName] = useState(null); return <Child ...

Causing a click event to occur results in crashing the browser

Take a look at this link: example on jsfiddle Why does the click event keep triggering multiple times when a div is clicked, eventually causing the browser to crash? What could be causing this issue and how can it be resolved? The actual div contains a l ...

JQuery unable to append AJAX response

Seeking assistance from the community here. My expertise lies more in PHP and less in JQuery or JavaScript. I am facing an issue with my code where it appears to work fine, except for appending content to the UL element using JQuery. Despite seeing the cor ...

Arrange the items in AngularJS using ng-repeat from horizontal to vertical orientation

I am currently using ng-repeat on floated left <li> elements, resulting in a horizontal list. Is there a way to repeat the same items vertically as shown below? from: Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8 Item9 Item10 to: Item1 ...

Is it possible for Angular-charts.js colors to be dynamically created?

Is there a way to dynamically generate an array of colors for our chart? If I initialize my colors array with hex colors, it works fine. But if I use the result of my "getRandomColor" function, it doesn't work at all. Any ideas on this issue? $scop ...

Developing an animated feature that displays a dynamic count up to the current size of the browser window

I have a script that's able to determine the height and width of my browser window. However, I am facing a challenge in creating a way for these dimensions to count up from zero to their current values upon loading. The desired functionality would be ...

Issue with Puppeteer: element selection resulting in null values or timing out

I am currently working on extracting the innerHTML value of a button from a webpage using puppeteer. My current approach involves awaiting the appearance of the selector before proceeding with the operation. However, upon executing the code below, the pro ...