Skip to content

Commit 33ca3b7

Browse files
Ensure custom variants using the JS API have access to modifiers
1 parent 8b0d224 commit 33ca3b7

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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

1617
### Fixed

packages/tailwindcss/src/compat/plugin-api.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,52 @@ describe('matchVariant', () => {
22952295
}"
22962296
`)
22972297
})
2298+
2299+
test.only('should be called with eventual modifiers', async () => {
2300+
let { build } = await compile(
2301+
css`
2302+
@plugin "my-plugin";
2303+
@tailwind utilities;
2304+
`,
2305+
{
2306+
loadModule: async (id, base) => {
2307+
return {
2308+
base,
2309+
module: ({ matchVariant }: PluginAPI) => {
2310+
matchVariant('my-container', (value, { modifier }) => {
2311+
function parseValue(value: string) {
2312+
const numericValue = value.match(/^(\d+\.\d+|\d+|\.\d+)\D+/)?.[1] ?? null
2313+
if (numericValue === null) return null
2314+
2315+
return parseFloat(value)
2316+
}
2317+
2318+
const parsed = parseValue(value)
2319+
return parsed !== null ? `@container ${modifier ?? ''} (min-width: ${value})` : []
2320+
})
2321+
},
2322+
}
2323+
},
2324+
},
2325+
)
2326+
let compiled = build([
2327+
'my-container-[250px]:underline',
2328+
'my-container-[250px]/placement:underline',
2329+
])
2330+
expect(optimizeCss(compiled).trim()).toMatchInlineSnapshot(`
2331+
"@container (width >= 250px) {
2332+
.my-container-\\[250px\\]\\:underline {
2333+
text-decoration-line: underline;
2334+
}
2335+
}
2336+
2337+
@container placement (width >= 250px) {
2338+
.my-container-\\[250px\\]\\/placement\\:underline {
2339+
text-decoration-line: underline;
2340+
}
2341+
}"
2342+
`)
2343+
})
22982344
})
22992345

23002346
describe('addUtilities()', () => {

packages/tailwindcss/src/compat/plugin-api.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ export function buildPluginApi(
116116
designSystem.variants.group(
117117
() => {
118118
designSystem.variants.functional(name, (ruleNodes, variant) => {
119-
if (!variant.value || variant.modifier) {
119+
if (!variant.value) {
120120
if (options?.values && 'DEFAULT' in options.values) {
121-
ruleNodes.nodes = resolveVariantValue(options.values.DEFAULT, null, ruleNodes.nodes)
121+
ruleNodes.nodes = resolveVariantValue(
122+
options.values.DEFAULT,
123+
variant.modifier,
124+
ruleNodes.nodes,
125+
)
122126
return
123127
}
124128
return null
@@ -136,7 +140,7 @@ export function buildPluginApi(
136140
return
137141
}
138142

139-
ruleNodes.nodes = resolveVariantValue(defaultValue, null, ruleNodes.nodes)
143+
ruleNodes.nodes = resolveVariantValue(defaultValue, variant.modifier, ruleNodes.nodes)
140144
}
141145
})
142146
},

0 commit comments

Comments
 (0)