Skip to content

Commit b4ef24f

Browse files
committed
migrate theme(…) to --theme(…)
1 parent 6baa7b1 commit b4ef24f

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ it('should migrate `theme(…)` to `var(…)`', async () => {
3535
}
3636
`),
3737
).toMatchInlineSnapshot(`
38-
"@media theme(--breakpoint-sm) {
38+
"@media --theme(--breakpoint-sm) {
3939
.foo {
4040
background-color: var(--color-red-900);
41-
color: theme(--color-red-900 / 75%);
42-
border-color: theme(--color-red-200 / 75%);
41+
color: --theme(--color-red-900 / 75%);
42+
border-color: --theme(--color-red-200 / 75%);
4343
}
4444
}"
4545
`)

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test.each([
1414
['[--value:theme(spacing[1.25])]', '[--value:--spacing(1.25)]'],
1515

1616
// Should not convert invalid spacing values to calc
17-
['[--value:theme(spacing[1.1])]', '[--value:theme(spacing[1.1])]'],
17+
['[--value:theme(spacing[1.1])]', '[--value:--theme(spacing[1.1])]'],
1818

1919
// Convert to `var(…)` if we can resolve the path
2020
['[color:theme(colors.red.500)]', '[color:var(--color-red-500)]'], // Arbitrary property
@@ -26,13 +26,13 @@ test.each([
2626
['bg-[theme(colors.red.500,red)]', 'bg-(--color-red-500,red)'],
2727

2828
// Keep `theme(…)` if we can't resolve the path
29-
['bg-[theme(colors.foo.1000)]', 'bg-[theme(colors.foo.1000)]'],
29+
['bg-[theme(colors.foo.1000)]', 'bg-[--theme(colors.foo.1000)]'],
3030

3131
// Keep `theme(…)` if we can't resolve the path, but still try to convert the
3232
// fallback value.
3333
[
3434
'bg-[theme(colors.foo.1000,theme(colors.red.500))]',
35-
'bg-[theme(colors.foo.1000,var(--color-red-500))]',
35+
'bg-[--theme(colors.foo.1000,var(--color-red-500))]',
3636
],
3737

3838
// Use `theme(…)` (deeply nested) inside of a `calc(…)` function
@@ -47,11 +47,11 @@ test.each([
4747
// to a candidate modifier _if_ all `theme(…)` calls use the same modifier.
4848
[
4949
'[color:theme(colors.red.500/50,theme(colors.blue.500/50))]',
50-
'[color:theme(--color-red-500/50,theme(--color-blue-500/50))]',
50+
'[color:--theme(--color-red-500/50,--theme(--color-blue-500/50))]',
5151
],
5252
[
5353
'[color:theme(colors.red.500/50,theme(colors.blue.500/50))]/50',
54-
'[color:theme(--color-red-500/50,theme(--color-blue-500/50))]/50',
54+
'[color:--theme(--color-red-500/50,--theme(--color-blue-500/50))]/50',
5555
],
5656

5757
// Convert the `theme(…)`, but try to move the inline modifier (e.g. `50%`),
@@ -75,52 +75,52 @@ test.each([
7575
['bg-[theme(colors.red.500/12.34%)]', 'bg-(--color-red-500)/[12.34%]'],
7676

7777
// Arbitrary property that already contains a modifier
78-
['[color:theme(colors.red.500/50%)]/50', '[color:theme(--color-red-500/50%)]/50'],
78+
['[color:theme(colors.red.500/50%)]/50', '[color:--theme(--color-red-500/50%)]/50'],
7979

8080
// Values that don't contain only `theme(…)` calls should not be converted to
8181
// use a modifier since the color is not the whole value.
8282
[
8383
'shadow-[shadow:inset_0px_1px_theme(colors.white/15%)]',
84-
'shadow-[shadow:inset_0px_1px_theme(--color-white/15%)]',
84+
'shadow-[shadow:inset_0px_1px_--theme(--color-white/15%)]',
8585
],
8686

8787
// Arbitrary value, where the candidate already contains a modifier
8888
// This should still migrate the `theme(…)` syntax to the modern syntax.
89-
['bg-[theme(colors.red.500/50%)]/50', 'bg-[theme(--color-red-500/50%)]/50'],
89+
['bg-[theme(colors.red.500/50%)]/50', 'bg-[--theme(--color-red-500/50%)]/50'],
9090

9191
// Variants, we can't use `var(…)` especially inside of `@media(…)`. We can
9292
// still upgrade the `theme(…)` to the modern syntax.
93-
['max-[theme(screens.lg)]:flex', 'max-[theme(--breakpoint-lg)]:flex'],
93+
['max-[theme(screens.lg)]:flex', 'max-[--theme(--breakpoint-lg)]:flex'],
9494
// There are no variables for `--spacing` multiples, so we can't convert this
95-
['max-[theme(spacing.4)]:flex', 'max-[theme(spacing.4)]:flex'],
95+
['max-[theme(spacing.4)]:flex', 'max-[--theme(spacing.4)]:flex'],
9696

9797
// This test in itself doesn't make much sense. But we need to make sure
9898
// that this doesn't end up as the modifier in the candidate itself.
99-
['max-[theme(spacing.4/50)]:flex', 'max-[theme(spacing.4/50)]:flex'],
99+
['max-[theme(spacing.4/50)]:flex', 'max-[--theme(spacing.4/50)]:flex'],
100100

101101
// `theme(…)` calls in another CSS function is replaced correctly.
102102
// Additionally we remove unnecessary whitespace.
103103
['grid-cols-[min(50%_,_theme(spacing.80))_auto]', 'grid-cols-[min(50%,--spacing(80))_auto]'],
104104

105105
// `theme(…)` calls valid in v3, but not in v4 should still be converted.
106-
['[--foo:theme(transitionDuration.500)]', '[--foo:theme(transitionDuration.500)]'],
106+
['[--foo:theme(transitionDuration.500)]', '[--foo:--theme(transitionDuration.500)]'],
107107

108108
// Renamed theme keys
109109
['max-w-[theme(screens.md)]', 'max-w-(--breakpoint-md)'],
110110
['w-[theme(maxWidth.md)]', 'w-(--container-md)'],
111111

112112
// Invalid cases
113-
['[--foo:theme(colors.red.500/50/50)]', '[--foo:theme(colors.red.500/50/50)]'],
114-
['[--foo:theme(colors.red.500/50/50)]/50', '[--foo:theme(colors.red.500/50/50)]/50'],
113+
['[--foo:theme(colors.red.500/50/50)]', '[--foo:--theme(colors.red.500/50/50)]'],
114+
['[--foo:theme(colors.red.500/50/50)]/50', '[--foo:--theme(colors.red.500/50/50)]/50'],
115115

116116
// Partially invalid cases
117117
[
118118
'[--foo:theme(colors.red.500/50/50)_theme(colors.blue.200)]',
119-
'[--foo:theme(colors.red.500/50/50)_var(--color-blue-200)]',
119+
'[--foo:--theme(colors.red.500/50/50)_var(--color-blue-200)]',
120120
],
121121
[
122122
'[--foo:theme(colors.red.500/50/50)_theme(colors.blue.200)]/50',
123-
'[--foo:theme(colors.red.500/50/50)_var(--color-blue-200)]/50',
123+
'[--foo:--theme(colors.red.500/50/50)_var(--color-blue-200)]/50',
124124
],
125125
])('%s => %s', async (candidate, result) => {
126126
let designSystem = await __unstable__loadDesignSystem(
@@ -158,7 +158,7 @@ test('extended space scale converts to var or calc', async () => {
158158
'[--value:var(--spacing-miami)]',
159159
)
160160
expect(themeToVar(designSystem, {}, '[--value:theme(spacing.nyc)]')).toEqual(
161-
'[--value:theme(spacing.nyc)]',
161+
'[--value:--theme(spacing.nyc)]',
162162
)
163163
})
164164

@@ -183,6 +183,6 @@ test('custom space scale converts to var', async () => {
183183
'[--value:var(--spacing-2)]',
184184
)
185185
expect(themeToVar(designSystem, {}, '[--value:theme(spacing.3)]')).toEqual(
186-
'[--value:theme(spacing.3)]',
186+
'[--value:--theme(spacing.3)]',
187187
)
188188
})

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export function createConverter(designSystem: DesignSystem, { prettyPrint = fals
149149

150150
// Multiple `/` separators, which makes this an invalid path
151151
if (parts.length > 2) {
152-
return null
152+
return `--theme(${parts.join('/')})`
153153
}
154154

155155
// The path contains a `/`, which means that there is a modifier such as
@@ -227,7 +227,9 @@ export function createConverter(designSystem: DesignSystem, { prettyPrint = fals
227227

228228
let modifier =
229229
parts.length > 0 ? (prettyPrint ? ` / ${parts.join(' / ')}` : `/${parts.join('/')}`) : ''
230-
return fallback ? `theme(${variable}${modifier}, ${fallback})` : `theme(${variable}${modifier})`
230+
return fallback
231+
? `--theme(${variable}${modifier}, ${fallback})`
232+
: `--theme(${variable}${modifier})`
231233
}
232234

233235
return convert
@@ -239,6 +241,8 @@ function substituteFunctionsInValue(
239241
) {
240242
ValueParser.walk(ast, (node, { replaceWith }) => {
241243
if (node.kind === 'function' && node.value === 'theme') {
244+
node.value = '--theme'
245+
242246
if (node.nodes.length < 1) return
243247

244248
// Ignore whitespace before the first argument

0 commit comments

Comments
 (0)