Skip to content

Commit 5ce37c4

Browse files
Ensure we migrate theme(spacing.1) to var(--spacing-1) correctly (#14724)
This PR fixes an issue where `theme(…)` calls that contain a `.1` weren't correctly converted to `var(--spacing-1)`. The reason for this is that `.1` has some special meaning in cases like `fontSize.xs.1.lineHeight` where it should be converted to `--font-size-xs--line-height`, not `--font-size-xs-1-line-height`. To solve this, we make sure to only apply the `--` check if the `1` occurs somewhere in the middle instead of at the very end. With this change, the following migrations will happen correctly: ```diff - [--value:theme(spacing.1)] + [--value:var(--spacing-1)] ``` ```diff - [--value:theme(fontSize.xs.1.lineHeight)] + [--value:var(--font-size-xs--line-height)] ``` --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent 84200e2 commit 5ce37c4

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Allow spaces spaces around operators in attribute selector variants ([#14703](https://github.com/tailwindlabs/tailwindcss/pull/14703))
1717
- _Upgrade (experimental)_: Migrate `flex-grow` to `grow` and `flex-shrink` to `shrink` ([#14721](https://github.com/tailwindlabs/tailwindcss/pull/14721))
1818
- _Upgrade (experimental)_: Minify arbitrary values when printing candidates ([#14720](https://github.com/tailwindlabs/tailwindcss/pull/14720))
19+
- _Upgrade (experimental)_: Ensure legacy theme values ending in `1` (like `theme(spacing.1)`) are correctly migrated to custom properties ([#14724](https://github.com/tailwindlabs/tailwindcss/pull/14724))
1920
- _Upgrade (experimental)_: Migrate arbitrary values to bare values for the `from-*`, `via-*`, and `to-*` utilities ([#14725](https://github.com/tailwindlabs/tailwindcss/pull/14725))
2021

2122
### Changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ test.each([
66
// Keep candidates that don't contain `theme(…)` or `theme(…, …)`
77
['[color:red]', '[color:red]'],
88

9+
// Handle special cases around `.1` in the `theme(…)`
10+
['[--value:theme(spacing.1)]', '[--value:var(--spacing-1)]'],
11+
['[--value:theme(fontSize.xs.1.lineHeight)]', '[--value:var(--font-size-xs--line-height)]'],
12+
913
// Convert to `var(…)` if we can resolve the path
1014
['[color:theme(colors.red.500)]', '[color:var(--color-red-500)]'], // Arbitrary property
1115
['[color:theme(colors.red.500)]/50', '[color:var(--color-red-500)]/50'], // Arbitrary property + modifier

packages/tailwindcss/src/compat/apply-config-to-theme.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ export function keyPathToCssProperty(path: string[]) {
142142
// [1] should move into the nested object tuple. To create the CSS variable
143143
// name for this, we replace it with an empty string that will result in two
144144
// subsequent dashes when joined.
145-
.map((path) => (path === '1' ? '' : path))
145+
//
146+
// E.g.:
147+
// - `fontSize.xs.1.lineHeight` -> `font-size-xs--line-height`
148+
// - `spacing.1` -> `--spacing-1`
149+
.map((path, idx, all) => (path === '1' && idx !== all.length - 1 ? '' : path))
146150

147151
// Resolve the key path to a CSS variable segment
148152
.map((part) =>

0 commit comments

Comments
 (0)