Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@headlessui/tailwindcss 'ui-open' utility class inheritance inside Dialog #3077

Closed
anzuj opened this issue Apr 3, 2024 · 2 comments
Closed
Assignees

Comments

@anzuj
Copy link

anzuj commented Apr 3, 2024

Describe your issue
I have a Listbox component within a Dialog. The Listbox component is styled with the ui-open utility class from @headlessui/tailwindcss.
Expected: ui-open styles on the Listbox only take effect when the Listbox itself is open, regardless of where it is in the app hierarchy.
What happens: Within a Dialog component, Listbox always has the ui-open styles active, inheriting the need for them from the Dialog's open state, not from the Listbox's open state.

What package within Headless UI are you using?
"@headlessui/react": "latest",
"@headlessui/tailwindcss": "^0.2.0",

What browser are you using?
Chrome

Reproduction URL
I really tried to provide a reproduction but I can't get the "@headlessui/tailwindcss" plugin to work at all in the starting project: https://codesandbox.io/p/devbox/my-tailwind-vwrs78?file=%2Fsrc%2FApp.jsx%3A12%2C25
Unfortunately the "ui-open"classes have no effect in that sandbox. Would appreciate tips on how to make it work there in the config so that I could accurately reproduce it for you!
In the above sandbox, it is expected that when the Listbox is opened, Button bottom border radius is set to 0 and the background goes light blue with @headlessui/tailwindcss util classes:
<Listbox.Button className="rounded-t-md rounded-b-md ui-open:rounded-b-none bg-white ui-open:bg-sky-300">

@RobinMalfait RobinMalfait self-assigned this Apr 3, 2024
@RobinMalfait
Copy link
Collaborator

Hey!

The problem you are seeing is that the ui-open also applies because the Dialog also exposes that information. To solve this, you can explicitly mark the bg-white with ui-not-open:

- <Listbox.Button className="rounded-t-md rounded-b-md ui-open:rounded-b-none bg-white ui-open:bg-sky-300">
+ <Listbox.Button className="rounded-t-md rounded-b-md ui-open:rounded-b-none uit-not-open:bg-white ui-open:bg-sky-300">

It's a bit of an unfortunate situation, but this should solve your issue.
If this still won't work, then you can fallback to using JS based values using render props: https://headlessui.com/react/listbox#using-render-props

We are aware of this inconvenience, and to handle this in a more explicit and reliable way we made the following changes to the next versions of Headless UI and Tailwind CSS (both are currently in an alpha stage, but wanted to share this with you already):

  • In Headless UI v2, we expose these values as data attributes directly, this means that you will be able to use data-[open]:… directly.
  • In Tailwind CSS v4, data attributes will be available as data-open:… which is easier to type than data-[open]:….
  • If you combine these with named group classes (https://tailwindcss.com/docs/hover-focus-and-other-states#differentiating-nested-groups) then you can add a named group to the listbox and handle it explicitly:

This means that eventually, you will be able to do something like this to explicitly target the correct data:

<Listbox as="div" className="group/my-listbox">
  <ListboxButton className="group-data-open/my-listbox:…" />
</Listbox>

But for now, explicitly adding ui-not-open:bg-white or using the render prop should do the trick.

Hope this helps!

@anzuj
Copy link
Author

anzuj commented Apr 15, 2024

@RobinMalfait truly, thank you for taking the time for such a comprehensive response! ui-not-open class indeed did the trick and the inputs within the dialog correctly get styled only when they are open. Appreciate the sneak peek to future features as well!

Would you perhaps have at hand any online codesandbox where @headlessui/tailwindcss plugin is working alongside @headlessui/react? It would be a hugely helpful asset to make online demos of HeadlessUI components for my team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants