headlessui: Transition not working in NextJs
It appears like Transition classes are not there initially on any div(<Transition>
)
Btw I’m using ( tailwindcss(^1.8.4) , styled-components & twin-macro)
Sample code I’m trying to test on my NextJs WebApp
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 28
Here’s a working example for styled components and twin.macro: https://codesandbox.io/s/next-styled-components-tailwind-twin-starter-headless-ui-transition-forked-vow27?file=/pages/index.js
Isn’t perfect but works…
Hi, not sure if its the same issue. I just faced this a minute ago, and the solution was to hoist the classes in the inner div to the
<Transition>
initial:
After
I think this is because the <Transition> component renders the child in another div. so having your classes there might solve it.
Since
twin.macro
is a macro (hence the name), many things happens in build time so you can’t use<Transition enter="transition-opacity delay-500 duration-1000" ...
just like that.Here is how I fixed it using the default twin.macro setup (
emotion
behind the scenes):I hope headlessui implements function props while keeping support for strings. Eg:
I am using twin.macro (tw``) with custom Transition wrapper component.
transition.tsx
@Barbariansyah see #681 for more information
stack: tailwind + headless UI + Next.js
I’ve also faced the same issue running with
next dev
, using the first example from https://headlessui.dev/react/transitionbut when I do
next build
thennext start
it works just fineis this an expected behavior? can anyone help explain what’s happening? thanks a lot 🙏
Having the same issue with styled components and twin.macro. Would be great if anyone can provide an example.
@puneetverma05 Did you solve this?
@puneetverma05 It would be very weird that we should support insert tools here. That said, if it is not the responsibility of
twin.macro
but rather styled-components / emotion, then it is up to those tools to generate classNames.For example, when you use emotion (I don’t know if styled-components exposes such an api) than you can do this: https://codesandbox.io/s/next-styled-components-tailwind-twin-starter-headless-ui-transition-forked-p7hbe?file=/pages/index.js:1633-2041
https://headlessui.dev/react/transition#transitioning-on-initial-mount
Based on a quick test it looks like keeping the
appear
property ontrue
solves this issue withNextJS
@puneetverma05
The transition currently works by reading the duration from the DOM. So this duration is defined by the
duration-100
class for example. However, when you usetwin.macro
theduration-100
class doesn’t exists, thus the duration will be 0 and immediately mounted/unmounted.Essentially if you know how to generate a className form the macro then it should work
@andrewgbliss What do you mean with
The div doesn't have any classes on it.
which div are you referring to? The oneTransition
generates (when it is mounted) or another one?