Skip to content

Commit fc261bd

Browse files
Upgrade: Migrate max-w-screen-* candidates (#14754)
This PR migrates `max-w-screen-*` candidates to the v4 equivalent relying on the breakpoint var: `max-w-[var(--breakpoint-*)]`
1 parent c0f2922 commit fc261bd

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-2
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
- _Upgrade (experimental)_: Migrate `plugins` with options to CSS ([#14700](https://github.com/tailwindlabs/tailwindcss/pull/14700))
1313
- _Upgrade (experimental)_: Allow JS configuration files with `corePlugins` options to be migrated to CSS ([#14742](https://github.com/tailwindlabs/tailwindcss/pull/14742))
14+
- _Upgrade (experimental)_: Migrate `max-w-screen-*` utilities to `max-w-[var(…)]`([#14754](https://github.com/tailwindlabs/tailwindcss/pull/14754))
1415
- _Upgrade (experimental)_: Migrate `@variants` and `@responsive` directives ([#14748](https://github.com/tailwindlabs/tailwindcss/pull/14748))
1516
- _Upgrade (experimental)_: Migrate `@screen` directive ([#14749](https://github.com/tailwindlabs/tailwindcss/pull/14749))
1617

integrations/upgrade/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test(
2020
`,
2121
'src/index.html': html`
2222
<h1>🤠👋</h1>
23-
<div class="!flex sm:!block bg-gradient-to-t bg-[--my-red]"></div>
23+
<div class="!flex sm:!block bg-gradient-to-t bg-[--my-red] max-w-screen-md"></div>
2424
`,
2525
'src/input.css': css`
2626
@tailwind base;
@@ -42,7 +42,7 @@ test(
4242
"
4343
--- ./src/index.html ---
4444
<h1>🤠👋</h1>
45-
<div class="flex! sm:block! bg-linear-to-t bg-[var(--my-red)]"></div>
45+
<div class="flex! sm:block! bg-linear-to-t bg-[var(--my-red)] max-w-[var(--breakpoint-md)]"></div>
4646
4747
--- ./src/input.css ---
4848
@import 'tailwindcss';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { __unstable__loadDesignSystem } from '@tailwindcss/node'
2+
import { expect, test } from 'vitest'
3+
import { maxWidthScreen } from './max-width-screen'
4+
5+
test('converts max-w-screen-* to max-w-[theme(screens.*)] (so it will later be converted to the var injection)', async () => {
6+
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', {
7+
base: __dirname,
8+
})
9+
10+
let migrated = maxWidthScreen(designSystem, {}, 'max-w-screen-md')
11+
expect(migrated).toMatchInlineSnapshot(`"max-w-[theme(screens.md)]"`)
12+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Config } from 'tailwindcss'
2+
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
3+
import { printCandidate } from '../candidates'
4+
5+
export function maxWidthScreen(
6+
designSystem: DesignSystem,
7+
_userConfig: Config,
8+
rawCandidate: string,
9+
): string {
10+
for (let candidate of designSystem.parseCandidate(rawCandidate)) {
11+
if (
12+
candidate.kind === 'functional' &&
13+
candidate.root === 'max-w' &&
14+
candidate.value?.value.startsWith('screen-')
15+
) {
16+
return printCandidate(designSystem, {
17+
...candidate,
18+
value: {
19+
...candidate.value,
20+
value: `[theme(screens.${candidate.value.value.slice(7)})]`,
21+
},
22+
})
23+
}
24+
}
25+
return rawCandidate
26+
}

packages/@tailwindcss-upgrade/src/template/codemods/theme-to-var.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ test.each([
8686
// `theme(…)` calls valid in v3, but not in v4 should still be converted.
8787
['[--foo:theme(fontWeight.semibold)]', '[--foo:theme(fontWeight.semibold)]'],
8888

89+
// `screens` values
90+
['max-w-[theme(screens.md)]', 'max-w-[var(--breakpoint-md)]'],
91+
8992
// Invalid cases
9093
['[--foo:theme(colors.red.500/50/50)]', '[--foo:theme(colors.red.500/50/50)]'],
9194
['[--foo:theme(colors.red.500/50/50)]/50', '[--foo:theme(colors.red.500/50/50)]/50'],

packages/@tailwindcss-upgrade/src/template/migrate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { arbitraryValueToBareValue } from './codemods/arbitrary-value-to-bare-va
77
import { automaticVarInjection } from './codemods/automatic-var-injection'
88
import { bgGradient } from './codemods/bg-gradient'
99
import { important } from './codemods/important'
10+
import { maxWidthScreen } from './codemods/max-width-screen'
1011
import { prefix } from './codemods/prefix'
1112
import { simpleLegacyClasses } from './codemods/simple-legacy-classes'
1213
import { themeToVar } from './codemods/theme-to-var'
@@ -31,6 +32,7 @@ export const DEFAULT_MIGRATIONS: Migration[] = [
3132
bgGradient,
3233
simpleLegacyClasses,
3334
arbitraryValueToBareValue,
35+
maxWidthScreen,
3436
themeToVar,
3537
variantOrder,
3638
]

0 commit comments

Comments
 (0)