Skip to content

Commit a022905

Browse files
authored
Ensure --spacing-* variables take precedence over --container-* variables (#15180)
Fixes #15146. This PR updates the `w-*`, `max-w-*`, `min-w-*`, and `basis-*` utilities to make sure that `--spacing-*` values are preferred over `--container-*` values when there is a conflict. Given this theme configuration: ```css @theme { --spacing-sm: 8px; --container-sm: 256px; } ``` …utilities like `max-w-sm` will use `8px` instead of `256px` after this change. Users can still be explicit about the value they want to use if they've introduced a naming collision like this by using our variable shorthand like `max-w-(--container-sm)`. --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent 068b1c2 commit a022905

File tree

3 files changed

+83
-34
lines changed

3 files changed

+83
-34
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
- Ensure any necessary vendor prefixes are generated for iOS Safari, Firefox, and Chrome ([#15166](https://github.com/tailwindlabs/tailwindcss/pull/15166))
1313
- Ensure `.group` and `.peer` are prefixed when using the `prefix(…)` option ([#15174](https://github.com/tailwindlabs/tailwindcss/pull/15174))
1414
- Ensure 3D transforms render correctly in Safari ([#15179](https://github.com/tailwindlabs/tailwindcss/pull/15179))
15+
- Ensure `--spacing-*` variables take precedence over `--container-*` variables ([#15180](https://github.com/tailwindlabs/tailwindcss/pull/15180))
1516

1617
## [4.0.0-beta.2] - 2024-11-22
1718

packages/tailwindcss/src/utilities.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17025,6 +17025,40 @@ describe('spacing utilities', () => {
1702517025

1702617026
expect(optimizeCss(compiled).trim()).toEqual('')
1702717027
})
17028+
17029+
test('--spacing-* variables take precedence over --container-* variables', async () => {
17030+
let { build } = await compile(css`
17031+
@theme {
17032+
--spacing-sm: 8px;
17033+
--container-sm: 256px;
17034+
}
17035+
@tailwind utilities;
17036+
`)
17037+
let compiled = build(['w-sm', 'max-w-sm', 'min-w-sm', 'basis-sm'])
17038+
17039+
expect(optimizeCss(compiled).trim()).toMatchInlineSnapshot(`
17040+
":root {
17041+
--spacing-sm: 8px;
17042+
--container-sm: 256px;
17043+
}
17044+
17045+
.w-sm {
17046+
width: var(--spacing-sm);
17047+
}
17048+
17049+
.max-w-sm {
17050+
max-width: var(--spacing-sm);
17051+
}
17052+
17053+
.min-w-sm {
17054+
min-width: var(--spacing-sm);
17055+
}
17056+
17057+
.basis-sm {
17058+
flex-basis: var(--spacing-sm);
17059+
}"
17060+
`)
17061+
})
1702817062
})
1702917063

1703017064
describe('custom utilities', () => {

packages/tailwindcss/src/utilities.ts

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export function createUtilities(theme: Theme) {
374374

375375
function spacingUtility(
376376
name: string,
377-
themeNamespace: ThemeKey | ThemeKey[],
377+
themeKeys: ThemeKey[],
378378
handle: (value: string) => AstNode[] | undefined,
379379
{
380380
supportsNegative = false,
@@ -388,7 +388,6 @@ export function createUtilities(theme: Theme) {
388388
utilities.static(`-${name}-px`, () => handle('-1px'))
389389
}
390390
utilities.static(`${name}-px`, () => handle('1px'))
391-
let themeKeys = ([] as ThemeKey[]).concat(themeNamespace, '--spacing')
392391
functionalUtility(name, {
393392
themeKeys,
394393
supportsFractions,
@@ -522,7 +521,7 @@ export function createUtilities(theme: Theme) {
522521
staticUtility(`${name}-auto`, [[property, 'auto']])
523522
staticUtility(`${name}-full`, [[property, '100%']])
524523
staticUtility(`-${name}-full`, [[property, '-100%']])
525-
spacingUtility(name, '--inset', (value) => [decl(property, value)], {
524+
spacingUtility(name, ['--inset', '--spacing'], (value) => [decl(property, value)], {
526525
supportsNegative: true,
527526
supportsFractions: true,
528527
})
@@ -751,7 +750,7 @@ export function createUtilities(theme: Theme) {
751750
['ml', 'margin-left'],
752751
] as const) {
753752
staticUtility(`${namespace}-auto`, [[property, 'auto']])
754-
spacingUtility(namespace, '--margin', (value) => [decl(property, value)], {
753+
spacingUtility(namespace, ['--margin', '--spacing'], (value) => [decl(property, value)], {
755754
supportsNegative: true,
756755
})
757756
}
@@ -890,20 +889,20 @@ export function createUtilities(theme: Theme) {
890889

891890
spacingUtility(
892891
'size',
893-
['--size'],
892+
['--size', '--spacing'],
894893
(value) => [decl('--tw-sort', 'size'), decl('width', value), decl('height', value)],
895894
{
896895
supportsFractions: true,
897896
},
898897
)
899898

900899
for (let [name, namespaces, property] of [
901-
['w', ['--width', '--container'], 'width'],
902-
['min-w', ['--min-width', '--container'], 'min-width'],
903-
['max-w', ['--max-width', '--container'], 'max-width'],
904-
['h', ['--height'], 'height'],
905-
['min-h', ['--min-height', '--height'], 'min-height'],
906-
['max-h', ['--max-height', '--height'], 'max-height'],
900+
['w', ['--width', '--spacing', '--container'], 'width'],
901+
['min-w', ['--min-width', '--spacing', '--container'], 'min-width'],
902+
['max-w', ['--max-width', '--spacing', '--container'], 'max-width'],
903+
['h', ['--height', '--spacing'], 'height'],
904+
['min-h', ['--min-height', '--height', '--spacing'], 'min-height'],
905+
['max-h', ['--max-height', '--height', '--spacing'], 'max-height'],
907906
] as [string, ThemeKey[], string][]) {
908907
spacingUtility(name, namespaces, (value) => [decl(property, value)], {
909908
supportsFractions: true,
@@ -997,9 +996,14 @@ export function createUtilities(theme: Theme) {
997996
*/
998997
staticUtility('basis-auto', [['flex-basis', 'auto']])
999998
staticUtility('basis-full', [['flex-basis', '100%']])
1000-
spacingUtility('basis', ['--flex-basis', '--container'], (value) => [decl('flex-basis', value)], {
1001-
supportsFractions: true,
1002-
})
999+
spacingUtility(
1000+
'basis',
1001+
['--flex-basis', '--spacing', '--container'],
1002+
(value) => [decl('flex-basis', value)],
1003+
{
1004+
supportsFractions: true,
1005+
},
1006+
)
10031007

10041008
/**
10051009
* @css `table-layout`
@@ -1028,20 +1032,20 @@ export function createUtilities(theme: Theme) {
10281032
/**
10291033
* @css `border-spacing`
10301034
*/
1031-
spacingUtility('border-spacing', ['--border-spacing'], (value) => [
1035+
spacingUtility('border-spacing', ['--border-spacing', '--spacing'], (value) => [
10321036
borderSpacingProperties(),
10331037
decl('--tw-border-spacing-x', value),
10341038
decl('--tw-border-spacing-y', value),
10351039
decl('border-spacing', 'var(--tw-border-spacing-x) var(--tw-border-spacing-y)'),
10361040
])
10371041

1038-
spacingUtility('border-spacing-x', ['--border-spacing'], (value) => [
1042+
spacingUtility('border-spacing-x', ['--border-spacing', '--spacing'], (value) => [
10391043
borderSpacingProperties(),
10401044
decl('--tw-border-spacing-x', value),
10411045
decl('border-spacing', 'var(--tw-border-spacing-x) var(--tw-border-spacing-y)'),
10421046
])
10431047

1044-
spacingUtility('border-spacing-y', ['--border-spacing'], (value) => [
1048+
spacingUtility('border-spacing-y', ['--border-spacing', '--spacing'], (value) => [
10451049
borderSpacingProperties(),
10461050
decl('--tw-border-spacing-y', value),
10471051
decl('border-spacing', 'var(--tw-border-spacing-x) var(--tw-border-spacing-y)'),
@@ -1113,7 +1117,7 @@ export function createUtilities(theme: Theme) {
11131117

11141118
spacingUtility(
11151119
'translate',
1116-
['--translate'],
1120+
['--translate', '--spacing'],
11171121
(value) => [
11181122
translateProperties(),
11191123
decl('--tw-translate-x', value),
@@ -1136,7 +1140,7 @@ export function createUtilities(theme: Theme) {
11361140
])
11371141
spacingUtility(
11381142
`translate-${axis}`,
1139-
['--translate'],
1143+
['--translate', '--spacing'],
11401144
(value) => [
11411145
translateProperties(),
11421146
decl(`--tw-translate-${axis}`, value),
@@ -1151,7 +1155,7 @@ export function createUtilities(theme: Theme) {
11511155

11521156
spacingUtility(
11531157
`translate-z`,
1154-
['--translate'],
1158+
['--translate', '--spacing'],
11551159
(value) => [
11561160
translateProperties(),
11571161
decl(`--tw-translate-z`, value),
@@ -1615,9 +1619,14 @@ export function createUtilities(theme: Theme) {
16151619
['scroll-mb', 'scroll-margin-bottom'],
16161620
['scroll-ml', 'scroll-margin-left'],
16171621
] as const) {
1618-
spacingUtility(namespace, '--scroll-margin', (value) => [decl(property, value)], {
1619-
supportsNegative: true,
1620-
})
1622+
spacingUtility(
1623+
namespace,
1624+
['--scroll-margin', '--spacing'],
1625+
(value) => [decl(property, value)],
1626+
{
1627+
supportsNegative: true,
1628+
},
1629+
)
16211630
}
16221631

16231632
/**
@@ -1634,7 +1643,7 @@ export function createUtilities(theme: Theme) {
16341643
['scroll-pb', 'scroll-padding-bottom'],
16351644
['scroll-pl', 'scroll-padding-left'],
16361645
] as const) {
1637-
spacingUtility(namespace, '--scroll-padding', (value) => [decl(property, value)])
1646+
spacingUtility(namespace, ['--scroll-padding', '--spacing'], (value) => [decl(property, value)])
16381647
}
16391648

16401649
staticUtility('list-inside', [['list-style-position', 'inside']])
@@ -1816,13 +1825,13 @@ export function createUtilities(theme: Theme) {
18161825
staticUtility('justify-items-end', [['justify-items', 'end']])
18171826
staticUtility('justify-items-stretch', [['justify-items', 'stretch']])
18181827

1819-
spacingUtility('gap', ['--gap'], (value) => [decl('gap', value)])
1820-
spacingUtility('gap-x', ['--gap'], (value) => [decl('column-gap', value)])
1821-
spacingUtility('gap-y', ['--gap'], (value) => [decl('row-gap', value)])
1828+
spacingUtility('gap', ['--gap', '--spacing'], (value) => [decl('gap', value)])
1829+
spacingUtility('gap-x', ['--gap', '--spacing'], (value) => [decl('column-gap', value)])
1830+
spacingUtility('gap-y', ['--gap', '--spacing'], (value) => [decl('row-gap', value)])
18221831

18231832
spacingUtility(
18241833
'space-x',
1825-
['--space'],
1834+
['--space', '--spacing'],
18261835
(value) => [
18271836
atRoot([property('--tw-space-x-reverse', '0', '<number>')]),
18281837

@@ -1838,7 +1847,7 @@ export function createUtilities(theme: Theme) {
18381847

18391848
spacingUtility(
18401849
'space-y',
1841-
['--space'],
1850+
['--space', '--spacing'],
18421851
(value) => [
18431852
atRoot([property('--tw-space-y-reverse', '0', '<number>')]),
18441853
styleRule(':where(& > :not(:last-child))', [
@@ -2822,7 +2831,7 @@ export function createUtilities(theme: Theme) {
28222831
['pb', 'padding-bottom'],
28232832
['pl', 'padding-left'],
28242833
] as const) {
2825-
spacingUtility(name, '--padding', (value) => [decl(property, value)])
2834+
spacingUtility(name, ['--padding', '--spacing'], (value) => [decl(property, value)])
28262835
}
28272836

28282837
staticUtility('text-left', [['text-align', 'left']])
@@ -2832,9 +2841,14 @@ export function createUtilities(theme: Theme) {
28322841
staticUtility('text-start', [['text-align', 'start']])
28332842
staticUtility('text-end', [['text-align', 'end']])
28342843

2835-
spacingUtility('indent', ['--text-indent'], (value) => [decl('text-indent', value)], {
2836-
supportsNegative: true,
2837-
})
2844+
spacingUtility(
2845+
'indent',
2846+
['--text-indent', '--spacing'],
2847+
(value) => [decl('text-indent', value)],
2848+
{
2849+
supportsNegative: true,
2850+
},
2851+
)
28382852

28392853
staticUtility('align-baseline', [['vertical-align', 'baseline']])
28402854
staticUtility('align-top', [['vertical-align', 'top']])
@@ -3727,7 +3741,7 @@ export function createUtilities(theme: Theme) {
37273741
['--tw-leading', '1'],
37283742
['line-height', '1'],
37293743
])
3730-
spacingUtility('leading', ['--leading'], (value) => [
3744+
spacingUtility('leading', ['--leading', '--spacing'], (value) => [
37313745
atRoot([property('--tw-leading')]),
37323746
decl('--tw-leading', value),
37333747
decl('line-height', value),

0 commit comments

Comments
 (0)