Skip to content

Commit 139e1cc

Browse files
committed
migrate theme.aria to custom @custom-variant aria-* variants
But we will make sure that we skip the ones that would be handled automatically by bare values. E.g.: `aria-foo:flex` will generate `[aria-foo="true"]`.
1 parent bfa6084 commit 139e1cc

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ async function migrateTheme(
118118
let prevSectionKey = ''
119119
let themeSection: string[] = []
120120
let keyframesCss = ''
121+
let variants = new Map<string, string>()
121122

122123
// Special handling of specific theme keys:
123124
{
@@ -139,6 +140,18 @@ async function migrateTheme(
139140
}
140141
delete resolvedConfig.theme.container
141142
}
143+
144+
if ('aria' in resolvedConfig.theme) {
145+
for (let [key, value] of Object.entries(resolvedConfig.theme.aria ?? {})) {
146+
// Will be handled by bare values if the names match.
147+
// E.g.: `aria-foo:flex` should produce `[aria-foo="true"]`
148+
if (new RegExp(`^${key}=['"]true['"]$`).test(`${value}`)) continue
149+
150+
// Create custom variant
151+
variants.set(`aria-${key}`, `&[aria-${value}]`)
152+
}
153+
delete resolvedConfig.theme.aria
154+
}
142155
}
143156

144157
// Convert theme values to CSS custom properties
@@ -208,6 +221,14 @@ async function migrateTheme(
208221
css += '}\n' // @tw-bucket
209222
}
210223

224+
if (variants.size > 0) {
225+
css += '\n@tw-bucket custom-variant {\n'
226+
for (let [name, selector] of variants) {
227+
css += `@custom-variant ${name} (${selector});\n`
228+
}
229+
css += '}\n'
230+
}
231+
211232
return css
212233
}
213234

@@ -368,7 +389,7 @@ const ALLOWED_THEME_KEYS = [
368389
// Used by @tailwindcss/container-queries
369390
'containers',
370391
]
371-
const BLOCKED_THEME_KEYS = ['supports', 'data', 'aria']
392+
const BLOCKED_THEME_KEYS = ['supports', 'data']
372393
function onlyAllowedThemeValues(theme: ThemeConfig): boolean {
373394
for (let key of Object.keys(theme)) {
374395
if (!ALLOWED_THEME_KEYS.includes(key)) {

0 commit comments

Comments
 (0)