Guide on utilizing `ReactCSSTransitionGroup` for developing an expandable/collapsible list

Utilizing ReactJS to implement a collapsible/expandable list with animation has been my latest endeavor. To access the code, simply visit this link. Upon clicking the Extend button located at the top of the list, I anticipate the items to appear within 1 second while simultaneously shifting the footer to the bottom. Regrettably, my current code fails to deliver the desired outcome. Firstly, the list items are not displayed with the intended animation style - appearing as though they are all rendered simultaneously. Secondly, upon clicking the extend button, the footer promptly moves to the bottom. Any suggestions on how I can correctly implement the list with ReactJS animation?

See this interactive demonstration of React animations created by joey (@zhaoyi0113) on CodePen.

Below is the ReactJS code snippet that I am currently using:

const ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;

const Container = React.createClass({

    getInitialState() {
        return {
            items: ['Click', 'To', 'Remove', 'An', 'Item'],
            extend: false
        };
    },

    render() {

        return (
            <div className="container">
                <div className="animation-container">
                    <div onClick={() => this.expendItems()} className="item">
                        {this.state.extend?'Collapse':'Extend'}
                    </div>
                    <ReactCSSTransitionGroup transitionName="example">
                        {this.renderItems()}
                    </ReactCSSTransitionGroup>

                    <div>Footer</div>
                </div>
            </div>
        );
    },

    renderItems() {
        const items = this.state.extend? this.state.items : [];

        console.log('render items ', items);
        return items.map((item, i) => {
            return (
                <div key={item} className="item">
                    {item}
                </div>
            );  
        }); 
    },

    expendItems() {
        this.setState({extend: !this.state.extend});
    }

});

ReactDOM.render(<Container/>, document.body);

Answer №1

In ReactCSSTransitionGroup, all components are rendered simultaneously.

Additionally, adjusting the initial translation values is crucial. Transitioning from negative values to 0 gives the impression that the buttons are sliding down from under the expand button.

body {
    font-family: 'Open Sans', sans-serif;
    padding: 25px;
    background-color: $color6;
    text-align: center;
}
.container {
    text-align: center;
    vertical-align: middle;
    overflow: hidden;
}
.animation-container {
    display: inline-block;
}
.item {
    background-color: $color3;
    width: 400px;
    text-align: center;
    padding: 10px 5px;
    margin-top: 10px;
    border-radius: 8px;
    font-weight: 600;
    color: mix(black, $color3, 60%);
    position: relative;

    &:hover {
        cursor: pointer;
    }
}
div > .item {
    margin-top: 0px;
}
.example-enter {
    top: -240px;
}
.example-enter.example-enter-active {
    top: 0px;
    transition: top 1s ease;
}
.example-leave {
    top: 0px;
    transition: top 1s ease;
}
.example-leave.example-leave-active {
    top: -240px;
}

https://codepen.io/anon/pen/Gvrgqm

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

A sporadic glitch in IE10 appears while attempting to translate text with a border-radius feature

I need some advice regarding a rendering issue I am experiencing in IE10. I have created an animated-flip effect that works perfectly in all the browsers I need it to. However, during testing, I noticed random border-like lines appearing for no apparent re ...

Adjust the size of the child div to fit the remaining space within the parent div

I'm dealing with a situation where there are two child divs inside a parent div. The first child div takes up 32% of the width, while the second child div takes up 68% of the width. If I were to set the display of the first child div to "none", how ca ...

"Using a CSS pseudo element to create stylish separators between elements

Can CSS pseudo elements :after or :before be used to clear floats between elements? If not, what other method can be used to clear floats between <div class="float"> and <section class="inner">? The HTML and CSS structure is as follows: * { ...

Tailwind component library - personalize color schemes for your project's design

I recently created a component library using Tailwind and React, and I've utilized Rollup for building the distribution files. In my tailwind.config.js file, you'll find the following color declarations: const colors = require('tailwindcss/c ...

Pause for a brief moment before proceeding to the next task within the map

My situation involves having a list of usernames that represent accounts: let users = [ "user1","user2","user3","user4","user5","user6","user7" ] users.map(async (user, i) => { co ...

Looking for guidance on how to specify the angular direction in the ng-repeat

I am facing a challenge with the structure of my ng-repeat directive and I am having trouble navigating through it. Here is a representation of the data in array format JSON style {"data":[ ["sameer","1001", [ {"button_icon": ...

When using applyMiddleware inside the createStore() function, the listener state is unable to detect changes

I'm currently working with React, react-redux, and redux. Recently, I encountered an issue. When I implemented the following code: const store = createStore(reducers); store.subscribe(() => { console.log(getState()); }); => ...

Go through each row in a table and retrieve the value of a column only if the checkbox

Is there a way to extract only the values in the serial column if the checkbox is checked for that row using jquery? I am currently able to retrieve the serial value, but unsure how to incorporate the checkbox condition. HTML: <table id="usersTable"&g ...

Ways to avoid a bootstrap column from wrapping onto a new line when the parent width is decreased

https://i.sstatic.net/gKdAN.png I attempted to decrease the form width by applying padding on both sides of the form using 'px-5'. However, when I implemented this change, the column holding the '#lastName' input shifted to a new line. ...

Tips on locating file encoding and decoding?

I have been tasked with understanding the decoding process of a file left on my system by Spotify. The specific file is named context_player_state_restore and can be found in the directory /Users/<myuser>/Library/Application Support/Spotify/Persiste ...

Display a message on React when hovering over

Is there a way to display a message when the user hovers over the 'Validate' button while it is disabled? I attempted the code below but it did not work as expected. <button type="button" className="frm_btn" ...

Develop a built-in Button for Liking posts

Currently, I am encountering an issue with the JavaScript SDK where I am unable to create a built-in Like button. After researching, I came across this helpful resource: https://developers.facebook.com/docs/opengraph/actions/builtin/likes/ The solution pr ...

Are the events objects failing to render or is the scope not being broadcasted properly

I'm having trouble displaying my events using the ionic-calendar plugin by calling the $scope.loadEvents method. Unfortunately, the events are not showing up on the calendar. Here is the link to the plugin I am using: https://github.com/twinssbc/Ioni ...

Updating the data-tooltip via AJAX is not functioning as expected

In my html template, there is a button: <a class="tooltipped" id="stash_recipe_tooltip" data-position="bottom" data-delay="50" data-tooltip="{{ stash_tooltip }}"> <div id="stash_recipe_btn" class="detail ...

Adjust item size and move them into the panel

I am looking to create a panel that arranges items in a specific layout shown here: https://i.stack.imgur.com/qw3lS.png Below is the code I have attempted so far: import React, { useCallback, useEffect } from "react"; import { Box, Grid, Tab, T ...

JavaScript enables running Acrobat Run profiles with variables

Is it possible to set a value to a preflight created using Acrobat before running it? var oProfile = Preflight.getProfileByName("myPreflight"); var oThermometer = app.thermometer; var textSize = 10; //oProfile.SetVariable("textSize",t ...

Establishing a connection to a remote node.js socket.io server using JavaScript on a different network

I successfully developed a functional chat program using socket.io with express for server setup, following the instructions on the socket.io website. When a client connects to the IP address via a browser, the Client HTML File is transmitted to them. va ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...

Looking to modify the height and width of an image when it is hovered over using inline CSS

My current project involves working with a dynamic template where the HTML code is generated from the back-end using TypeScript. I am trying to implement inline CSS on hover, but despite having written the necessary code, it does not seem to work as intend ...

Achieve the effect of background images fading in once the page loads, then disappearing when an accordion tab is

Currently on a project, I have implemented two background images that cross-fade on this website: www.alexarodulfo.com. I am interested in exploring the following possibilities: 1) Achieving a more subtle fade-in effect for the images, perhaps allowing th ...