Skip to content

Commit

Permalink
Ensure custom variants using the JS API have access to modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Oct 10, 2024
1 parent 8b0d224 commit 33ca3b7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add support for `tailwindcss/colors.js`, `tailwindcss/defaultTheme.js`, and `tailwindcss/plugin.js` exports ([#14595](https://github.com/tailwindlabs/tailwindcss/pull/14595))
- Support `keyframes` in JS config file themes ([#14594](https://github.com/tailwindlabs/tailwindcss/pull/14594))
- Ensure custom variants using the JS API have access to modifiers
- _Upgrade (experimental)_: The upgrade tool now automatically discovers your JavaScript config ([#14597](https://github.com/tailwindlabs/tailwindcss/pull/14597))

### Fixed
Expand Down
46 changes: 46 additions & 0 deletions packages/tailwindcss/src/compat/plugin-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,52 @@ describe('matchVariant', () => {
}"
`)
})

test.only('should be called with eventual modifiers', async () => {
let { build } = await compile(
css`
@plugin "my-plugin";
@tailwind utilities;
`,
{
loadModule: async (id, base) => {
return {
base,
module: ({ matchVariant }: PluginAPI) => {
matchVariant('my-container', (value, { modifier }) => {
function parseValue(value: string) {
const numericValue = value.match(/^(\d+\.\d+|\d+|\.\d+)\D+/)?.[1] ?? null
if (numericValue === null) return null

return parseFloat(value)
}

const parsed = parseValue(value)
return parsed !== null ? `@container ${modifier ?? ''} (min-width: ${value})` : []
})
},
}
},
},
)
let compiled = build([
'my-container-[250px]:underline',
'my-container-[250px]/placement:underline',
])
expect(optimizeCss(compiled).trim()).toMatchInlineSnapshot(`
"@container (width >= 250px) {
.my-container-\\[250px\\]\\:underline {
text-decoration-line: underline;
}
}
@container placement (width >= 250px) {
.my-container-\\[250px\\]\\/placement\\:underline {
text-decoration-line: underline;
}
}"
`)
})
})

describe('addUtilities()', () => {
Expand Down
10 changes: 7 additions & 3 deletions packages/tailwindcss/src/compat/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ export function buildPluginApi(
designSystem.variants.group(
() => {
designSystem.variants.functional(name, (ruleNodes, variant) => {
if (!variant.value || variant.modifier) {
if (!variant.value) {
if (options?.values && 'DEFAULT' in options.values) {
ruleNodes.nodes = resolveVariantValue(options.values.DEFAULT, null, ruleNodes.nodes)
ruleNodes.nodes = resolveVariantValue(
options.values.DEFAULT,
variant.modifier,
ruleNodes.nodes,
)
return
}
return null
Expand All @@ -136,7 +140,7 @@ export function buildPluginApi(
return
}

ruleNodes.nodes = resolveVariantValue(defaultValue, null, ruleNodes.nodes)
ruleNodes.nodes = resolveVariantValue(defaultValue, variant.modifier, ruleNodes.nodes)
}
})
},
Expand Down

0 comments on commit 33ca3b7

Please sign in to comment.