Skip to content

Commit 275bac4

Browse files
committed
migrate supports theme key to @custom-variant
1 parent 7c9a0ce commit 275bac4

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
@@ -25,6 +25,7 @@ import {
2525
isValidOpacityValue,
2626
isValidSpacingMultiplier,
2727
} from '../../../../tailwindcss/src/utils/infer-data-type'
28+
import * as ValueParser from '../../../../tailwindcss/src/value-parser'
2829
import { findStaticPlugins, type StaticPluginOptions } from '../../utils/extract-static-plugins'
2930
import { highlight, info, relative } from '../../utils/renderer'
3031

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

170200
// Convert theme values to CSS custom properties
@@ -253,7 +283,11 @@ async function migrateTheme(
253283
if (previousRoot !== root) css += '\n'
254284
previousRoot = root
255285

256-
css += `@custom-variant ${name} (${selector});\n`
286+
if (selector.startsWith('{')) {
287+
css += `@custom-variant ${name} ${selector}\n`
288+
} else {
289+
css += `@custom-variant ${name} (${selector});\n`
290+
}
257291
}
258292
css += '}\n'
259293
}
@@ -418,15 +452,11 @@ const ALLOWED_THEME_KEYS = [
418452
// Used by @tailwindcss/container-queries
419453
'containers',
420454
]
421-
const BLOCKED_THEME_KEYS = ['supports']
422455
function onlyAllowedThemeValues(theme: ThemeConfig): boolean {
423456
for (let key of Object.keys(theme)) {
424457
if (!ALLOWED_THEME_KEYS.includes(key)) {
425458
return false
426459
}
427-
if (BLOCKED_THEME_KEYS.includes(key)) {
428-
return false
429-
}
430460
}
431461

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

0 commit comments

Comments
 (0)