Expanding animation instant, while collapsing animation is as expected. Why?

Viewed 14

I have a simple component in React that essentially expands and collapses when clicked. However, I noticed that the expanding animation is almost instant, while the collapsing animation fulfils the expectation of slowly collapsing the text till 18px.

Did I understand animations wrongly?

https://stackblitz.com/edit/react-plfamb?file=src/style.css

import React, { useState } from 'react';
import './style.css';

const text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

export default function App() {
  const [collapsed, setCollapsed] = useState(true);

  function handleOnClick() {
    setCollapsed(!collapsed);
  }

  return (
    <div
      className={collapsed ? 'collapsed' : 'expanded'}
      onClick={handleOnClick}
    >
      {text}
    </div>
  );
}

styles.css:

.collapsed {
  max-height: 18px;
  transition: max-height 0.3s ease;
  overflow: hidden;
}

.expanded {
  max-height: 500px;
  transition: max-height 0.3s ease;
}
1 Answers

Missed out overflow: hidden in the expanded class. Many thanks to @KonradLinkowski

Related