In my previous article, I showed how to build a reusable TransitionExpand component for Vue that smoothly animates an element’s height using Vue’s built-in <Transition> component. If you haven’t read it yet, I recommend starting there before continuing with this article: Building a Reusable Expand/Collapse Transition Component in Vue 3.

After publishing it, I integrated the component into an application that uses dynamically loaded Web Components and discovered an interesting edge case that wasn’t immediately obvious.

The original implementation worked perfectly with native HTML elements and Vue components, but failed when used with dynamically loaded Web Components.

Let’s look at why.

Originally published on Medium.

The Problem

Imagine you have a notification component built as a Web Component.

<transition-expand>
  <notification-wrapper v-if="showError" />
</transition-expand>

When showError becomes true, Vue inserts the component into the DOM and immediately calls the enter transition hook.

For regular Vue components, that’s exactly what we want.

However, many Web Components are upgraded asynchronously.

The sequence actually looks like this:

v-if becomes true
        ↓
Vue inserts the element
        ↓
Transition enter() runs
        ↓
Height is still 0px
        ↓
Web Component finishes rendering
        ↓
Actual height becomes 160px

Our transition measures the height before the component has finished rendering.

As a result, the animation goes from 0px to 0px, making it look like the transition never happened.

The First Thought

A common solution is to repeatedly check whether the element has a height.

Adding something like this before we calculate the height:

while (element.scrollHeight === 0) {
  await new Promise(requestAnimationFrame)
}

This works.

But we’re continuously asking the browser the same question every frame:

“Has the height changed yet?”

It gets the job done, but there’s a cleaner approach.

Refactoring the Original Code

Before we introduce ResizeObserver, we need to make one small change to the original implementation.

In the previous article, the enter() function was responsible for both measuring the element’s height and performing the animation.

Since we may now need to wait until the content has finished rendering, it’s cleaner to separate those responsibilities.

The animation logic itself doesn’t change—we simply move it into a new helper function called animateEnter().

Our new enter() function now has a single responsibility:

  • If the element already has a measurable height, start the animation immediately.
  • Otherwise, wait until the element is ready, then call animateEnter().

This small refactor makes the code easier to read while keeping the original animation logic completely unchanged.

A Better Solution: ResizeObserver

The browser already provides an API that tells us when an element changes size: ResizeObserver.

Instead of polling every frame, we simply wait until the browser tells us the element has a measurable height. With the animation logic extracted into animateEnter(), the new enter() function becomes much simpler.

function enter(element: HTMLElement) {
  if (element.scrollHeight > 0) {
    animateEnter(element)
    return
  }

  const observer = new ResizeObserver(() => {
    if (element.scrollHeight === 0)
      return

    observer.disconnect()
    requestAnimationFrame(() => {
      animateEnter(element)
    })
  })

  observer.observe(element)
}

If the element already has a height, we animate immediately.

If not, we wait for the browser to notify us that its size has changed before starting the transition.

The actual animation logic doesn’t change at all—only the moment when it begins.

Why This Is Better

Using ResizeObserver has a few advantages:

  • No polling or animation frame loops
  • Works with asynchronously upgraded Web Components
  • Works with lazy-loaded Vue components
  • Works with images or other content that changes size after rendering
  • Keeps the transition component completely generic

This means your TransitionExpand component doesn’t need to know what it’s rendering. It simply waits until the content has a measurable size before animating.

Final Thoughts

The original implementation from my previous article is still perfectly valid for most Vue applications.

This improvement simply makes the component more resilient when working with asynchronously rendered content, where layout isn’t always ready the moment Vue inserts an element into the DOM.

It was a small change in code, but a significant improvement in reliability.

One of the things I enjoy most about building reusable components is discovering these real-world edge cases. Every time one appears, the component becomes a little more robust—not just for the current project, but for every future one as well.

If you’ve encountered similar transition issues with Web Components or other asynchronous content, I’d love to hear how you solved them.