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 matchVariant API #14371

Merged
merged 9 commits into from
Sep 11, 2024
Merged

Add matchVariant API #14371

merged 9 commits into from
Sep 11, 2024

Conversation

philipp-spiess
Copy link
Contributor

@philipp-spiess philipp-spiess commented Sep 9, 2024

This PR adds support for the matchVariant plugin API. I've copied over all V3 tests and made sure they still pass.

Sorted order of stacked arbitrary variants

The only difference in behavior is regarding the sort order of stacked arbitrary variants: Sorting in this case now works by the latest defined matchVariant taking precedence.

So, if you define a plugin like this:

matchVariant('testmin', (value) => `@media (min-width: ${value})`, {
  sort(a, z) {
    return parseInt(a.value) - parseInt(z.value)
  },
})

matchVariant('testmax', (value) => `@media (max-width: ${value})`, {
  sort(a, z) {
    return parseInt(z.value) - parseInt(a.value)
  },
})

The resulting CSS is first sorted by the testmax values descending and then the testmin values ascending, so these candidates:

testmin-[150px]:testmax-[400px]:order-2
testmin-[100px]:testmax-[350px]:order-3
testmin-[100px]:testmax-[300px]:order-4
testmin-[100px]:testmax-[400px]:order-1

Will resolve to the order outlined by the order- utility.

At-rules and placeholders support

Since we added support for at-rules and placeholders in the matchVariant syntax like this:

matchVariant(
  'potato',
  (flavor) => `@media (potato: ${flavor}) { @supports (font:bold) { &:large-potato } }`,
)

We also added support for the same syntax to the addVariant API:

addVariant(
  'potato',
  '@media (max-width: 400px) { @supports (font:bold) { &:large-potato } }',
)

The only change necessary in core was to call functional variants for when the variant value is set to null. This allows functional variants to define the un-parameterized implementation like potato:underline as opposed to potato[big]:underline.

@philipp-spiess philipp-spiess changed the title WIP: Add matchVariant API Add matchVariant API Sep 9, 2024
@philipp-spiess philipp-spiess marked this pull request as ready for review September 9, 2024 15:57
packages/tailwindcss/src/plugin-api.ts Show resolved Hide resolved
packages/tailwindcss/src/plugin-api.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/plugin-api.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/plugin-api.ts Show resolved Hide resolved
@philipp-spiess philipp-spiess merged commit 1e0dfbc into next Sep 11, 2024
3 checks passed
@philipp-spiess philipp-spiess deleted the feat/matchVariant branch September 11, 2024 14:45
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.

3 participants