Animating container heights in React allows for smooth transitions and dynamic

Check out my demo here

This demo features a simple app with four buttons and a text area.

Each button click loads different text, which varies in length and causes the grey container to adjust its height accordingly.

I'm wondering if it's possible to add animations to the grey box as the text content changes in height?

const [text, setText] = useState(textArr[3])

  return (
    <div>
      <div className="nav">
        <button onClick={() => setText(textArr[0])}>
          One
        </button>
        <button onClick={() => setText(textArr[1])}>
          Two
        </button>
        <button onClick={() => setText(textArr[2])}>
          Three
        </button>
        <button onClick={() => setText(textArr[3])}>
          Four
        </button>
      </div>  
      <div className="text">
        {text}
      </div>
    </div>
  );

Answer №1

To achieve the desired effect, I implemented a parent div with an applied style of overflow: hidden. I then utilized a useEffect hook to dynamically calculate the height of the text div as it changes, and subsequently adjusted the height of the parent div accordingly.

By incorporating a transition property on the parent div, the animation smoothly unfolds.

Feel free to check out the demo for reference: https://stackblitz.com/edit/react-t7ctuf?file=src%2FApp.js

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

What advantages does the use of $(e).attr(name,value) offer compared to using e.setAttribute(name,value)?

Scenario: The variable "e" represents an element of type "HtmlElement" and not a "css selector" I am referring to any attribute, not just the standard allowed ones like "atom-type" or "data-atom-type". Regardless of the attribute name, will it function wi ...

Is it possible to change the color of my button or icon based on the component that the user is currently viewing?

I have a set of drawer buttons that I want to customize by changing the icon colors when users click on them. For example, if a user clicks on 'Dashboard' and remains in that section, the dashboard button color will change to white to indicate th ...

Submit a photo with a unique identifier to a swiftAPI endpoint using a POST request from a ReactJS application

Attempting to send an image from the client side (ReactJS) to FastAPI using a POST method This is the code on the client side: const [img, setImg] = useState(null) const onImageUpload = (e) => { console.log(e.target.files[0]) setImg(e. ...

Looping through each element to assign labels spans

After retrieving data using a request, I am now attempting to display each result on my website. The data is in the form of an array with genres, and I need suggestions on utilizing loops in JSX to place these spans within a result div that already contain ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

The CSS files seem to be malfunctioning in the HTML code within ASP.Net Core

This is my first ASP.NET Core assignment and I am experiencing issues with applying CSS. The CSS files are all located under the wwwroot folder. While I can view them properly through a browser, my project in ASP.NET appears to be messed up. like this Tha ...

Troubles encountered in retrieving data from Axios with the help of Redux

I have been attempting to make an axios request to my ecommerce API using axios, but it keeps returning empty. I have tried multiple solutions, but none seem to be working. Below are the relevant files: Store.js (I attempted to use configureStore but ran ...

Sending an array of styles to a React component

I am facing a challenge in passing an array of styles to a React Component using postcss-loader and css modules. In my webpack configuration file for compiling CSS files, I have the following setup: loaders: [ 'style-loader', 'css ...

Creating a div resizing function using only JavaScript is a fun and useful project to tackle

Seeking help to create a div resizer using only pure JavaScript as jQuery is restricted. The current functionality works, but breaks when the position of the slider-div is not set to absolute. Any solutions to fix this issue would be greatly appreciated. ...

Animating drop-right feature in a horizontal navigation menu

I want to design a horizontal menu with an animated effect that activates when hovering over first-level items. Here is the code snippet: .nav { float: right; } .nav ul { list-style: none; padding: 0; margin: 0; } .nav ul li { position: relat ...

Transfer or duplicate an SVG image from the bottom div to the top div

Is there a way to move a div from the chart_div to the movehere div? I've tried duplicating it below the current svg, but I'm having trouble targeting just the header row ('g' element) specifically. Here is the HTML: <script type= ...

"ReactJS error: Unable to upload file due to a 400

Every time I attempt to upload a file, I encounter this error: "Uncaught (in promise) Error: Request failed with status code 404". I'm puzzled as to why this is happening. Here's the section of my code that seems to be causing the issue. ...

CSS for decorating an image with a border and transparent background

Struggling to add a border while converting a PSD to HTML? Is it possible to apply a border to an image with a transparent background? For instance, let's consider the following image: Imagine wanting to place a border around the caret in the image l ...

What is the best way to vertically center content in Bootstrap 4.5?

Below is a snippet of the code I've been working on. <body style="height: 100vh; margin: 0; padding: 0; "> <!-- Form Section ============================================= --> <p>[wpshortcode1]</p> <div cl ...

Although React .map may seem like a function, it does not work for passing arrays in

My application root contains a lazyloaded component. App root : import Component from './app/components/component/component.lazy'; class App extends React.Component{ stuff: stuffType[] = [1, 2 , 3]; render() { return ( ...

Unending animated scrolling achieved through the use of ReactJS and CSS

I am currently working on creating a continuously looping animation scroll using React and CSS. The aim is to automatically scroll through a list, looping back to the beginning once the end of the list is reached. Here is what I have implemented so far: h ...

Encountering the error message "Expected token '<' instead of '{' when assigning Interfaces to a React Class Component"

Encountered a problem in my Laravel 8/Babel/Mix/React/TypeScript setup where a regular React class component is throwing a compilation error after migrating the project to TypeScript and passing an empty props and simple state interface to the component. ...

How to implement smooth scrolling in Bootstrap 4.1 with Scrollspy integration?

I am currently working on integrating the scrollspy function from bootstrap 4.1 into my navbar and I have successfully implemented it. However, I am facing challenges in adding smooth scrolling to it. Despite extensive research on the web, I have been unab ...

Dynamic navigation experiencing erratic behavior when using display flex

I attempted to enhance my previous projects by converting them into flexbox. However, I ran into an issue where the ul element was displaying as a block. To fix this, I used JavaScript to change it to display flex. Here is the fiddle: // JavaScript co ...

What causes the popper to suddenly jump to the top-left corner every time the underlying component updates?

I am currently using the Material-UI Popper component, which utilizes popper.js, to design a hovering toolbar. Overall, it is functioning well, but there is one peculiar issue: When text is selected, the hovering toolbar correctly appears above the text. ...