Skip to content

Commit 05ac797

Browse files
committed
migrate supports theme key to @custom-variant
1 parent 844d738 commit 05ac797

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

packages/@tailwindcss-upgrade/src/codemods/config/migrate-js-config.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
isValidOpacityValue,
2525
isValidSpacingMultiplier,
2626
} from '../../../../tailwindcss/src/utils/infer-data-type'
27+
import * as ValueParser from '../../../../tailwindcss/src/value-parser'
2728
import { findStaticPlugins, type StaticPluginOptions } from '../../utils/extract-static-plugins'
2829
import { highlight, info, relative } from '../../utils/renderer'
2930

@@ -164,6 +165,35 @@ async function migrateTheme(
164165
}
165166
delete resolvedConfig.theme.data
166167
}
168+
169+
if ('supports' in resolvedConfig.theme) {
170+
for (let [key, value] of Object.entries(resolvedConfig.theme.supports ?? {})) {
171+
// Will be handled by bare values if the value of the declaration is a
172+
// CSS variable.
173+
let parsed = ValueParser.parse(`${value}`)
174+
175+
// Unwrap the parens, e.g.: `(foo: var(--bar))` → `foo: var(--bar)`
176+
if (parsed.length === 1 && parsed[0].kind === 'function' && parsed[0].value === '') {
177+
parsed = parsed[0].nodes
178+
}
179+
180+
// Verify structure: `foo: var(--bar)`
181+
// ^^^ ← must match the `key`
182+
if (
183+
parsed.length === 3 &&
184+
parsed[0].kind === 'word' &&
185+
parsed[0].value === key &&
186+
parsed[2].kind === 'function' &&
187+
parsed[2].value === 'var'
188+
) {
189+
continue
190+
}
191+
192+
// Create custom variant
193+
variants.set(`supports-${key}`, `{@supports(${value}){@slot;}}`)
194+
}
195+
delete resolvedConfig.theme.supports
196+
}
167197
}
168198

169199
// Convert theme values to CSS custom properties
@@ -241,7 +271,11 @@ async function migrateTheme(
241271
if (previousRoot !== root) css += '\n'
242272
previousRoot = root
243273

244-
css += `@custom-variant ${name} (${selector});\n`
274+
if (selector.startsWith('{')) {
275+
css += `@custom-variant ${name} ${selector}\n`
276+
} else {
277+
css += `@custom-variant ${name} (${selector});\n`
278+
}
245279
}
246280
css += '}\n'
247281
}
@@ -406,15 +440,11 @@ const ALLOWED_THEME_KEYS = [
406440
// Used by @tailwindcss/container-queries
407441
'containers',
408442
]
409-
const BLOCKED_THEME_KEYS = ['supports']
410443
function onlyAllowedThemeValues(theme: ThemeConfig): boolean {
411444
for (let key of Object.keys(theme)) {
412445
if (!ALLOWED_THEME_KEYS.includes(key)) {
413446
return false
414447
}
415-
if (BLOCKED_THEME_KEYS.includes(key)) {
416-
return false
417-
}
418448
}
419449

420450
if ('screens' in theme && typeof theme.screens === 'object' && theme.screens !== null) {

0 commit comments

Comments
 (0)