Skip to content

Commit 02cb52a

Browse files
committed
Add codemod for migrating the @screen directive (#14749)
This PR adds a codemod for migrating the old `@screen` directive from Tailwind CSS v2 that also worked in Tailwind CSS v3 but wasn't documented anymore. Internally, this first migrates `@screen md` to `@media screen(md)`, then we rely on the existing migration that migrates the `screen(…)` function. Input: ```css @screen md { .foo { color: red; } } ``` Output (IR): ```css @media screen(md) { .foo { color: red; } } ``` Output: ```css @media theme(--breakpoint-md) { .foo { color: red; } } ```
1 parent 5bf2efb commit 02cb52a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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))
1414
- _Upgrade (experimental)_: Migrate `@variants` and `@responsive` directives ([#14748](https://github.com/tailwindlabs/tailwindcss/pull/14748))
15+
- _Upgrade (experimental)_: Migrate `@screen` directive ([#14749](https://github.com/tailwindlabs/tailwindcss/pull/14749))
1516

1617
### Fixed
1718

packages/@tailwindcss-upgrade/src/codemods/migrate-media-screen.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ it('should migrate a built-in breakpoint', async () => {
4141
`)
4242
})
4343

44+
it('should migrate `@screen` with a built-in breakpoint', async () => {
45+
expect(
46+
await migrate(css`
47+
@screen md {
48+
.foo {
49+
color: red;
50+
}
51+
}
52+
`),
53+
).toMatchInlineSnapshot(`
54+
"@media (width >= theme(--breakpoint-md)) {
55+
.foo {
56+
color: red;
57+
}
58+
}"
59+
`)
60+
})
61+
4462
it('should migrate a custom min-width screen (string)', async () => {
4563
expect(
4664
await migrate(

packages/@tailwindcss-upgrade/src/codemods/migrate-media-screen.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export function migrateMediaScreen({
2424
return value ? buildMediaQuery(value) : null
2525
})
2626

27+
// First migrate `@screen md` to `@media screen(md)`
28+
root.walkAtRules('screen', (node) => {
29+
node.name = 'media'
30+
node.params = `screen(${node.params})`
31+
})
32+
33+
// Then migrate the `screen(…)` function
2734
root.walkAtRules((rule) => {
2835
if (rule.name !== 'media') return
2936

0 commit comments

Comments
 (0)