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.

Opacity of Dialog.Overaly after transition

See original GitHub issue

What package within Headless UI are you using?

headlessui/react

What version of that package are you using?

1.0.0

What browser are you using?

Chromium Edge

Reproduction repository

https://github.com/cding91/IssueReproduce

Description

I grabbed the example code for Dialog. The example code uses transition on the opacity of the overlay. I intend to make use opacity-50 for the final looks of the overlay. I hope overlay’s opacity can transition from 0 to 50. What it did is shown in the code snippet below.

But it seems the overlay’s opacity changes from 0 to 50 then flashes to 100 (see the image). Is this the right behavior that I should expect? Is there something that I should or should not do to get the right transition? Thank you.

  <Transition.Child 
    as={Fragment}
    enter="transition duration-500"
    enterFrom="opacity-0"
    enterTo="opacity-50"
    leave="transition duraiton-500"
    leaveFrom="opacity-50"
    leaveTo="opacity-0"
  >
    <Dialog.Overlay className="fixed inset-0 opacity-50 bg-gray-500" />
  </Transition.Child>

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
adamwathancommented, Apr 17, 2021

Hey! The issue here is the opacity-50 class that is sitting on the overlay element permanently:

Screen Shot 2021-04-17 at 3 46 44 PM

The way the transition component works, it adds the enter classes to the element at the beginning of the transition, meaning the element has these classes at first:

<Dialog.Overlay className="fixed inset-0 opacity-50 bg-gray-500 transition duration-500 opacity-0" />

Notice how both opacity-50 and opacity-0 are on the element at the same time. This is a conflict, and whichever one appears later in the CSS file happens to win, in this case opacity-50 so opacity-0 is never applied.

The solution in general it to avoid conflicting classes being on an element at the same time. Using bg-opacity-50 for the overlay is a great way to do that, another approach is to just use two separate elements, where the inner element is the actual 50% transparent overlay, and the outer element transitions from 0 to 100 opacity:

  <Transition.Child 
    as={Fragment}
    enter="transition duration-500"
    enterFrom="opacity-0"
    enterTo="opacity-100"
    leave="transition duraiton-500"
    leaveFrom="opacity-100"
    leaveTo="opacity-0"
  >
    <Dialog.Overlay className="fixed inset-0">
      <div className="absolute inset-0 opacity-50 bg-gray-500"/>
    </Dialog.Overlay>
  </Transition.Child>

Headless UI has no way to know it should remove the opacity-50 class when it starts the transition and adds the opacity-0 class because it has no idea what those class names mean or anything.

10reactions
mattelligcommented, Apr 17, 2021

I was having the same/similar issue. Something like this worked for me:

<Transition.Child
    as={Fragment}
    enter="ease-out duration-500"
    enterFrom="opacity-0"
    enterTo="opacity-100"
    leave="ease-in duration-500"
    leaveFrom="opacity-100"
    leaveTo="opacity-0"
>
    <Dialog.Overlay className="fixed inset-0 bg-gray-500 bg-opacity-50 transition-opacity" />
</Transition.Child>

Specify the desired opacity with a bg-opacity utility on the overlay itself. Animates nicely and has the desired effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Opacity and Transparency to Create a Modal in ...
You used opacity with pointer-events and transition to create a fade-in effect to display a modal on demand. You also used the various...
Read more >
Fade in overlay in modal dialog - jquery - Stack Overflow
Is it possible to make the background opacity fade from 0% to 50%? If so, how? Because currently it feels kind of like...
Read more >
Hide dialog overlay before the dialog is animated out of view ...
We have validated your query “How to hide the dialog overlay before the dialog animation out of view instead of after animation”.
Read more >
HeadlessUI Dialogue Part 3 -Transitions - The Javascript
Just leave the as and show props at the Transition. Use the <Transition.Child> to wrap the overlay and content of the dialogue separately....
Read more >
How to Use the Dialog Component with Headless UI and ...
<Dialog.Overlay className="fixed inset-0 bg-black opacity-30" /> Then, we can add style to the dialog contents. To style ...
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