Striving to implement a dynamic expand and collapse animation feature for a card in ReactJS

I'm attempting to create an expand and collapse animation effect on a card without relying on bootstrap or any external libraries. I have tried adding a transition property but it doesn't seem to work when the button is clicked.

Here is the component: Card.jsx

import React, { useState } from 'react';
import './Card.scss';

const Card = (props) => {
    const [collapse, toggleCollapse] = useState(true);

    return (<div className="card">
        <div className="card-header">
            <h4 className="card-title">Card Actions</h4>
            <div className="heading-elements">
                <button onClick={() => toggleCollapse(!collapse)}>Collapse</button>
            </div>
        </div>
        <div className={`card-content ${!collapse ? 'collapse show' : 'collapsing'}`}>
            <div className="card-body">
                <div className="row">
                    Hi there, this content needs to shown on button click
                </div>
            </div>
        </div>
    </div>);
}

export default Card;

Here is the scss file of the card: Card.scss

.card {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    word-wrap: break-word;
    background-color: #fff;
    background-clip: border-box;
    border-radius: 0.428rem;
    border: none;
    margin-bottom: 2rem;
    box-shadow: 0 4px 24px 0 rgba(34, 41, 47, 0.10);
    transition: all 0.3s ease-in-out, background 0s, color 0s, border-color 0s;
}

.card .card-header {
    position: relative;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    justify-content: space-between;
    border-bottom: none;
    padding: 1.5rem;
    background-color: transparent;
}

.card-header:first-child {
    border-radius: calc(0.428rem - 1px) calc(0.428rem - 1px) 0 0;
}

.collapse:not(.show) {
    display: none;
}

.collapsing {
    position: relative;
    height: 0;
    overflow: hidden;
    transition: height 2s ease;
}

Answer №1

One common issue arises when attempting to collapse a card by removing the show class, triggering the .collapse:not(show) selector due to the presence of display: none;. Unfortunately, using display: none; with a transition is not feasible according to CSS specifications. For a comprehensive list of properties that can be animated, refer to this resource. In such cases, adjusting the height should suffice for achieving the desired effect.

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

Scrolling seamlessly within a container that is fixed in position

For hours, I've been attempting to incorporate smooth scrolling into my project with no success. The issue lies in the container where the anchor tags are located. If it's set to fixed position or absolute, none of my attempts seem to work. In ...

What are the best methods for cropping SVG images effectively?

Is it possible to crop a large SVG background that has elements rendered on top of it so that it fits the foreground elements? I am using svg.js but have not been able to find a built-in function for this. Can an SVG be cropped in this way? ...

Whenever I attempt to type a command in Ubuntu, it shows me an error message saying "/usr/bin/env: ‘bash ’: No such file

Every time I try to type something in Ubuntu, it keeps showing the error message /usr/bin/env: ‘bash\r’: No such file or directory This issue first came up when I attempted to create a react app using the command: npx create-react-app app_name A ...

Stop any additional subscriptions if the current one has not yet been completed using Redux-Observable

I'm currently working on an exciting action that retrieves company data from a local server. This action is triggered in the componentDidMount method of a React component. Upon receiving the data, I create models and send them to the reducer to be pro ...

React application experiencing issues on Internet Explorer 11

I am facing a challenge with my React app as it needs to be compatible with Internet Explorer 11. The app utilizes Material-UI and other small libraries, but all I see is a blank page when running it on IE 11. I have tried the following solutions with no s ...

The challenge of integrating Bootstrap Dropdown with server side rendering in Next.js

I am currently working on creating a navigation bar with dropdown options that contain several links. While it appears to be functioning correctly in the browser, I have noticed that the HTML code for the dropdown menu is not visible in the page source. N ...

What is the best way to make a box modal function that displays a different image source than the one shown in the modal?

I'm looking to create a box modal that shows one image on the page, and then displays a different image in the popup when clicked. Here's what I currently have: <div class="row"> <div class="modal-image"><img id="myImg" src="http ...

Steps to customize the background color of a MaterialUI card in React:

Hey there, I'm looking to update the background color of a card in my React MaterialUI project. Check out the image attached to see what I have in mind. Screenshot: https://stackblitz.com/edit/react-snmhbw ...

Finding strikeout text within <s> or <del> tags can be identified by closely examining the HTML codes

The issue arises with the text that reads as follows: 316.6.1 Structures. Structures shall not be constructed This is represented in HTML as: <b> <s> <span style='font-size:10.0pt'>316.6.1 Structures</span> ...

What's the best way to vertically center text in a div with overflow hidden?

In my ASP GridView, I am trying to display a table with a fixed number of lines in each TD element. To achieve this, I have decided to place a div inside each TD so that I can customize the height and layout. table.XDataGridView td div.inner-table-div { h ...

Top method for achieving a smooth transition in a loading CSS3 animation

Having trouble implementing a fade in/fade out effect on a div with a CSS3 based login animation when a function is loaded. I have set up a jsfiddle but still can't get it to work. Any assistance would be greatly appreciated! http://jsfiddle.net/adam ...

Tips for customizing the selected and hover color of ListItem in Material UI

Having trouble getting the 'selected' or 'hover' colors to work for the ListItem component. I've tried setting its classes like this: <ListItem selected button key="home" classes={{ selected: classes.listItemSelected } ...

Encountering a theme issue in the makeStyles function within Material-UI version 5

While working on some React components, I created a styles.js file for each of them. I am following a YouTube tutorial that uses material-ui version 4, so I decided to upgrade to V5. Code snippet for the component (Form): import React from 'react&apo ...

The PHP counter conceals the comma upon loading and does not display it permanently

I'm currently working on a PHP counter and encountering an issue with the comma display. I have implemented Number Format in a PHP function to print counter digits with commas every 3 digits, but the comma doesn't remain visible after the page lo ...

Combining a component library for RSC (React Server Components) with the Next.js App Router

I am currently developing a library of React components that will be utilized in various Next.js projects. The goal is to follow the RSC approach, where the majority of components are server-side rendered with only certain nodes having the "use client" dir ...

Utilize the useEffect hook in Next.js to shuffle an array randomly

Every time the page is loaded, I want to randomize an array. I am working with Nextjs and facing the issue of client-server mismatch. I have been advised to use a useEffect hook to solve this problem, but I seem to be doing it incorrectly. Can someone plea ...

What could be causing the lack of downward rotation of my arrow in css? (specifically, the span element)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=de ...

Having trouble launching the project with `npm start` command in React

Recently, I've been working on a React project where I successfully developed the basic components. Everything was running smoothly until I decided to delve into a new React Native project. Upon installation using create-react-native-app, the npm star ...

Column div being obscured by footer

My footer is causing overlap issues with the div above it on my mobile website. Here's a visual representation: Current view on my phone: https://i.stack.imgur.com/RpvWM.png Desired view: https://i.stack.imgur.com/KHVcm.png The code for the are ...

Using jQuery to modify CSS attributes is proving to be more challenging than I anticipated

I am attempting to modify a CSS attribute using jQuery. I have used the following code: wrapperInner.css('overflow', 'visible'); However, the value remains unchanged. When I use an alert alert(wrapperInner.css('overflow')), ...