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

Add @headlessui/tailwindcss plugin #1487

Merged
merged 10 commits into from May 24, 2022
Merged

Conversation

RobinMalfait
Copy link
Collaborator

@RobinMalfait RobinMalfait commented May 22, 2022

This PR adds a new @headlessui/tailwindcss plugin so that you can style your components using just
the css without requiring a render prop or a v-slot in your templates.

Before:

<Menu.Button as={Fragment}>
  {({ open }) => (
    <button className={open ? 'text-blue-500' : 'text-red-500'}>
      Options
      <svg className={open && 'rotate-90'}>
        <path ... />
      </svg>
    </button>
  )}
</Menu.Button>

After:

<Menu.Button className="ui-open:text-blue-500 ui-not-open:text-red-500">
  Options
  <svg className="ui-open:rotate-90">
    <path ... />
  </svg>
</Menu.Button>

@vercel
Copy link

vercel bot commented May 22, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
headlessui-react ✅ Ready (Inspect) Visit Preview May 24, 2022 at 7:26PM (UTC)
headlessui-vue ✅ Ready (Inspect) Visit Preview May 24, 2022 at 7:26PM (UTC)

@adamwathan
Copy link
Member

FB643ED8-A4C9-4EB1-87CE-B9A858708D0F

@ecklf
Copy link

ecklf commented May 22, 2022

image

All components that expose boolean props in their render prop / v-slot
will receive a `data-headlessui-state="..."` attribute.

If it exposes boolean values but all are false, then there will be an
empty `data-headlessui-state=""`. If the current component is rendering
a `Fragment` then we don't expose those attributes.
We were using the CDN, but now that we have the
`@headlessui/tailwindcss` plugin, it's a bit easier to configure it
natively and import the plugin.
@RobinMalfait RobinMalfait changed the title WIP: Add @headlessui/tailwindcss plugin Add @headlessui/tailwindcss plugin May 24, 2022
@RobinMalfait RobinMalfait merged commit 39c5bd3 into main May 24, 2022
@RobinMalfait RobinMalfait deleted the feat/add-tailwindcss-plugin branch May 24, 2022 20:51
@hailwood
Copy link
Contributor

hailwood commented May 24, 2022

@RobinMalfait I'm curious about the addition of the ui-not-x prefixes.
Isn't ui-not-selected:text-red ui-selected:text-green then same as text-red ui-selected:text-green?
I.e. aren't the not states just the default and should be overridden by the ui-x states since variants are listed after the base utilities in the final css?

@RobinMalfait
Copy link
Collaborator Author

@hailwood in a lot of cases that's true, but you can also combine those and then it can be useful: ui-active:ui-not-selected:...

Here is an example where you explicitly check for the ui-not-selected case as well: https://play.tailwindcss.com/HfMdcJk8yp

@cairin
Copy link

cairin commented May 25, 2022

The ui-not-x feels like a tailwind anti-pattern.

I say this because, as far as I can tell, this new plugin is only used for state variants. And traditionally state variants only have additive value. i.e. you dont have a not-focus state variant in normal tailwind.

As for your example:
text-blue-500 ui-active:ui-not-selected:text-red-500 is the same as text-blue-500 ui-active:text-red-500 ui-active:ui-selected:text-blue-500. https://play.tailwindcss.com/rd5RDfrjB2

You could argue that the second option is more code, but it's also (maybe) more verbose. It's also more familiar when compared to other tailwind classes.

I suppose you could just not use the ui-not- variants. But it's adding complexity to tailwind, that maybe isn't needed.

@adamwathan
Copy link
Member

The ui-not-x feels like a tailwind anti-pattern.

Totally see what you're saying and agree on the simplicity sticking to things being additive most of the time but I don't think this is an anti-pattern, to me it's no different than how we have support for both valid and invalid pseudo-classes:

image

We also recently added the enabled class for similar reasons:

tailwindlabs/tailwindcss#7905

We'll likely be merging this PR or some flavor of it that adds not-focus, not-hover, etc. too:

tailwindlabs/tailwindcss#7976

I don't think people will need to reach for this stuff super often but I think it's helpful to have 👍🏻

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

Successfully merging this pull request may close these issues.

None yet

5 participants