question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Ideas on transitioning height with <Transition />?

See original GitHub issue

I know CSS doesn’t let you transition height: 0 to height: auto so just curious if you’ve thought about any ways to accomplish this with the <Transition /> component.

I was thinking of bringing in a hook to calculate the final height of the DOM node but then I think I’d need another API to pass it to <Transition />, maybe enterToStyle?

<Transition
  show={open}
  enter="transition ease-out"
  enterFrom="transform h-0 opacity-0"
  enterTo="transform h-40 opacity-100"
  enterToStyle={{
    height: finalHeight
  }}
>

Something like this. Any thoughts?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
thien-docommented, Sep 23, 2021

Hey guys, I actually found a way to animating “height” using Tailwind and HeadlessUI’s Transition component 😄 the key is to programmatically set the height before the animation happens:

.height-0-important {
  height: 0 !important; /* to win the inline style we set */
}
<Transition
  enter="transition, you know the stuff"
  enterFrom="height-0-important"
  enterTo="" /* don't need this! */
  beforeEnter={() => {
	  const elm = panel.current; /* React's ref */
	  elm.parentElement.style.height = `${elm.clientHeight}px`;
  }}
>
  <Disclosure.Panel ref={panel}>
    {props.item.answer}
  </Disclosure.Panel>
4reactions
farmerpaulcommented, Jun 15, 2022

Here’s my TypeScript-friendly version of @thien-do’s suggested implementation (brilliant, @thien-do!) with the addition of the following:

  • included the leave transition
  • content height will continue to resize appropriately based on its content and container/window after transition is complete
  • updated for Tailwind 3 (no extra CSS classes need to be defined)
  const ref = useRef<HTMLDivElement>(null);
  const [shown, setShown] = useState(false);

  const captureHeight = () => ref.current && (ref.current.style.height = `${ref.current.clientHeight}px`);
  const clearHeight = () => ref.current && (ref.current.style.height = '');

  return (
    <Transition
      ref={ref}
      show={shown}
      enter="transition-[height] duration-300 overflow-hidden"
      enterFrom="!h-0"
      beforeEnter={captureHeight}
      afterEnter={clearHeight}
      leave="transition-[height] duration-300 overflow-hidden"
      leaveTo="!h-0"
      beforeLeave={captureHeight}
      afterLeave={clearHeight}
    >
      <div>Some variable height content</div>
    </Transition>
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I transition height: 0; to height: auto; using CSS?
First, the height transition only works between 0 and 100%, two numeric values. Since "auto" is not a numeric value, fractional increments don't ......
Read more >
Using CSS Transitions on Auto Dimensions
We can't transition height , but we can transition max-height , since it has an explicit value.
Read more >
How To Do CSS Transitions With Height: Auto - Carl Anderson
Method 1: Use transform. Despite its rampant use, you should avoid CSS transitions on the height or width properties (among some others). These ......
Read more >
Using CSS Transitions on the Height Property
The Solution. The hack is to transition the max-height property instead of the height property. First, we have to estimate the greatest height...
Read more >
How to make transition height from 0 to auto using CSS
Approach: Replace the height with max height so that the height of the container does not overcome the actual size of the container...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found