Skip to content

Commit a1e083a

Browse files
Upgrade: Handle darkMode value with block syntax (#16507)
Closes #16171 This PR handles `darkMode` variant configs containing braces (so creating sub-rules) the same way we handle it in the interop layer. Since the interop layer runs inside the `addVariant` API that we do not run here, I instead oped to copy the one liner. ## Test plan Updated one of the migration tests to include a rule that wasn't working before. Ensured the new output works via https://play.tailwindcss.com/nR99uhKtv3
1 parent 63b9be9 commit a1e083a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Ensure drop shadow utilities don't inherit unexpectedly ([#16471](https://github.com/tailwindlabs/tailwindcss/pull/16471))
2222
- Export backwards compatible config and plugin types from `tailwindcss/plugin` ([#16505](https://github.com/tailwindlabs/tailwindcss/pull/16505))
2323
- Upgrade: Report errors when updating dependencies ([#16504](https://github.com/tailwindlabs/tailwindcss/pull/16504))
24+
- Upgrade: Ensure a `darkMode` JS config setting with block syntax converts to use `@slot` ([#16507](https://github.com/tailwindlabs/tailwindcss/pull/16507))
2425

2526
## [4.0.6] - 2025-02-10
2627

Diff for: integrations/upgrade/js-config.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ test(
318318
import customPlugin from './custom-plugin'
319319
320320
export default {
321+
darkMode: [
322+
'variant',
323+
[
324+
'@media not print { .dark & }',
325+
'@media not eink { .dark & }',
326+
'&:where(.dark, .dark *)',
327+
],
328+
],
321329
plugins: [
322330
typography,
323331
customPlugin({
@@ -379,6 +387,22 @@ test(
379387
is-arr-mixed: null, true, false, 1234567, 1.35, 'foo', 'bar', 'true';
380388
}
381389
390+
@custom-variant dark {
391+
@media not print {
392+
.dark & {
393+
@slot;
394+
}
395+
}
396+
@media not eink {
397+
.dark & {
398+
@slot;
399+
}
400+
}
401+
&:where(.dark, .dark *) {
402+
@slot;
403+
}
404+
}
405+
382406
/*
383407
The default border color has changed to \`currentColor\` in Tailwind CSS v4,
384408
so we've added these compatibility styles to make sure everything still

Diff for: packages/@tailwindcss-upgrade/src/migrate-js-config.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,38 @@ async function migrateTheme(
196196
}
197197

198198
function migrateDarkMode(unresolvedConfig: Config & { darkMode: any }): string {
199-
let variant: string = ''
199+
let variant: string | string[] = ''
200200
let addVariant = (_name: string, _variant: string) => (variant = _variant)
201201
let config = () => unresolvedConfig.darkMode
202202
darkModePlugin({ config, addVariant })
203203

204204
if (variant === '') {
205205
return ''
206206
}
207-
return `\n@tw-bucket custom-variant {\n@custom-variant dark (${variant});\n}\n`
207+
208+
if (!Array.isArray(variant)) {
209+
variant = [variant]
210+
}
211+
212+
if (variant.length === 1 && !variant[0].includes('{')) {
213+
return `\n@tw-bucket custom-variant {\n@custom-variant dark (${variant[0]});\n}\n`
214+
}
215+
216+
let customVariant = ''
217+
for (let variantName of variant) {
218+
// Convert to the block syntax if a block is used
219+
if (variantName.includes('{')) {
220+
customVariant += variantName.replace('}', '{ @slot }}') + '\n'
221+
} else {
222+
customVariant += variantName + '{ @slot }\n'
223+
}
224+
}
225+
226+
if (customVariant !== '') {
227+
return `\n@tw-bucket custom-variant {\n@custom-variant dark {${customVariant}};\n}\n`
228+
}
229+
230+
return ''
208231
}
209232

210233
// Returns a string identifier used to section theme declarations

0 commit comments

Comments
 (0)