-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Migrate aria-*
, data-*
and supports-*
variants from arbitrary values to bare values
#14644
Conversation
This brings me such joy 🥳 I think we can migrate |
variant.kind === 'functional' && | ||
variant.root === 'aria' && | ||
variant.value?.kind === 'arbitrary' && | ||
variant.value.value.endsWith('="true"') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it valid to pass all CSS attribute selectors here? e.g. :!=
or *=
etc. This would match more often than we want it in that case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal here is to match what the user would've typed in the variant in v3 that is equivalent to the variant in v4 so anything other than ="true"
would be wrong because ="true"
is what v4 uses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah but I see what @philipp-spiess means. If we have aria-[foo*="true"]
then this could will match, but it should not match because we can't safely migrate that. Will adjust and add a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha yeah that's what I meant! This will then migrate to aria-foo*
which seems unexpected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooohhhh I see! yep that's def an issue
a99b4b8
to
85f1a07
Compare
data-*
and aria-*
variants from arbitrary values to bare valuesaria-*
, data-*
and supports-*
variants from arbitrary values to bare values
(variant.value.value.endsWith('=true') || | ||
variant.value.value.endsWith('="true"') || | ||
variant.value.value.endsWith("='true'")) | ||
) { | ||
let [key, _value] = segment(variant.value.value, '=') | ||
if ( | ||
// aria-[foo~="true"] | ||
key[key.length - 1] === '~' || | ||
// aria-[foo|="true"] | ||
key[key.length - 1] === '|' || | ||
// aria-[foo^="true"] | ||
key[key.length - 1] === '^' || | ||
// aria-[foo$="true"] | ||
key[key.length - 1] === '$' || | ||
// aria-[foo*="true"] | ||
key[key.length - 1] === '*' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a regex that covered all of these cases but it was horrible so made it more explicit 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol its almost all special regex chars 😂 — I don't think any of them need to be escaped in a character class tho right? (unless ^
was listed first iirc). I think its this right /[~|^$*]$/.test(key)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit list of cases is fine though. It definitely reads better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the one I used originally /[^~|^$*]=(['"]?)true\1/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahhh yeah that is kinda awful lol
0ce28b0
to
180b5aa
Compare
180b5aa
to
524cdbc
Compare
This PR adds a new codemod that can migrate
data-*
andaria-*
variants using arbitrary values to bare values.In Tailwind CSS v3, if you want to conditionally apply a class using data attributes, then you can write
data-[selected]:flex
. This requires the DOM element to have adata-selected=""
attribute. In Tailwind CSS v4 we can simplify this, by dropping the brackets and by usingdata-selected:flex
directly.This migration operates on the internal AST, which means that this also just works for compound variants such as
group-has-data-[selected]:flex
(which turns intogroup-has-data-selected:flex
).Additionally, this codemod is also applicable to
aria-*
variants. The biggest difference is that in v4aria-selected
maps to an attribute ofaria-selected="true"
. This means that we can only migratearia=[selected="true"]:flex
toaria-selected:flex
.Last but not least, we also migrate
supports-[gap]
tosupports-gap
if the passed in value looks like a property. If not, e.g.:supports-[display:grid]
then it stays as-is.