Skip to content

Commit c50de93

Browse files
authored
Replace default explicit spacing scale with multiplier system (#14857)
This PR replaces the default spacing scale (`--spacing-*`) with a generative system based on a default spacing _unit_. Instead of the default theme containing values like `--spacing-4`, `--spacing-6`, `--spacing-8`, etc., instead we just define a single `--spacing` value: ```css @theme { --spacing: 0.25rem; } ``` Utilities like `px-4` are derived from this unit by multiplying it by the value in the utility (4 in this case): ```css .px-4 { padding-inline: calc(var(--spacing) * 4); } ``` The biggest consequence of this change is that every value is available now, rather than just the explicitly configured values. This means utilities like `px-42` will work now, whereas prior to this PR only `px-40` and `px-44` were valid utilities. I personally found it very difficult to know which values actually existed at the higher end of the scale without IntelliSense, and in practice even when working with a skilled designer like [Steve](https://x.com/steveschoger) who helped design Tailwind's default spacing scale, I'd very often need to break out of it to implement a design, and trying to round to a value that was in the scale made the design worse, not better. This PR allows you to use any whole number, as well as decimal numbers that are multiples of `0.25` to ensure classes like `px-1.5` continue to work. While this means you can now technically do things like `pt-97.25`, I think the presence of the fractional value will be enough of a signal to developers that they are doing something a little unusual, and they can use their judgment as to whether they are making the right decision or not. I'll update this PR with a lot more detail when I have a chance, as there are a few other things to explain like: - Unifying all of the values for width/min-width/max-width/height/min-height/max-height utilities - Deriving numeric line-height values from the spacing multiplier instead of a separate line-height scale - Using `--spacing: initial` to disable the multiplier - How you can still use an explicit spacing scale and ignore this change - How we plan to use IntelliSense to surface a more curated set of spacing values even if smaller increments work when you type them explicitly --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent b63f493 commit c50de93

File tree

11 files changed

+4094
-888
lines changed

11 files changed

+4094
-888
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Support derived spacing scales based on a single `--spacing` theme value ([#14857](https://github.com/tailwindlabs/tailwindcss/pull/14857))
13+
- Add `svh`, `dvh`, `svw`, `dvw`, and `auto` values to all width/height/size utilities ([#14857](https://github.com/tailwindlabs/tailwindcss/pull/14857))
1214
- _Upgrade (experimental)_: Migrate `grid-cols-[subgrid]` and `grid-rows-[subgrid]` to `grid-cols-subgrid` and `grid-rows-subgrid` ([#14840](https://github.com/tailwindlabs/tailwindcss/pull/14840))
1315
- _Upgrade (experimental)_: Support migrating projects with multiple config files ([#14863](https://github.com/tailwindlabs/tailwindcss/pull/14863))
1416

@@ -29,7 +31,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2931
- Rename `drop-shadow` to `drop-shadow-sm` and `drop-shadow-sm` to `drop-shadow-xs` ([#14849](https://github.com/tailwindlabs/tailwindcss/pull/14849))
3032
- Rename `rounded` to `rounded-sm` and `rounded-sm` to `rounded-xs` ([#14849](https://github.com/tailwindlabs/tailwindcss/pull/14849))
3133
- Rename `blur` to `blur-sm` and `blur-sm` to `blur-xs` ([#14849](https://github.com/tailwindlabs/tailwindcss/pull/14849))
34+
- Remove fixed line-height theme values and derive `leading-*` utilites from `--spacing-*` scale ([#14857](https://github.com/tailwindlabs/tailwindcss/pull/14857))
3235
- Remove `--transition-timing-function-linear` from the default theme in favor of a static `ease-linear` utility ([#14880](https://github.com/tailwindlabs/tailwindcss/pull/14880))
36+
- Remove default `--spacing-*` scale in favor of `--spacing` multiplier ([#14857](https://github.com/tailwindlabs/tailwindcss/pull/14857))
3337

3438
## [4.0.0-alpha.31] - 2024-10-29
3539

integrations/upgrade/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test(
6969
'src/index.html': html`
7070
<h1>🤠👋</h1>
7171
<div
72-
class="!flex sm:!block bg-gradient-to-t bg-[--my-red] max-w-screen-md ml-[theme(spacing[1.5])]"
72+
class="!flex sm:!block bg-gradient-to-t bg-[--my-red] max-w-screen-md ml-[theme(screens.md)]"
7373
></div>
7474
`,
7575
'src/input.css': css`
@@ -93,7 +93,7 @@ test(
9393
--- ./src/index.html ---
9494
<h1>🤠👋</h1>
9595
<div
96-
class="flex! sm:block! bg-linear-to-t bg-[var(--my-red)] max-w-[var(--breakpoint-md)] ml-[var(--spacing-1_5)]"
96+
class="flex! sm:block! bg-linear-to-t bg-[var(--my-red)] max-w-[var(--breakpoint-md)] ml-[var(--breakpoint-md)]"
9797
></div>
9898
9999
--- ./src/input.css ---
@@ -154,7 +154,7 @@ test(
154154
candidate`bg-linear-to-t`,
155155
candidate`bg-[var(--my-red)]`,
156156
candidate`max-w-[var(--breakpoint-md)]`,
157-
candidate`ml-[var(--spacing-1\_5)`,
157+
candidate`ml-[var(--breakpoint-md)`,
158158
])
159159
},
160160
)

packages/@tailwindcss-postcss/src/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -296,41 +296,7 @@ exports[`\`@import 'tailwindcss'\` is replaced with the generated CSS 1`] = `
296296
--drop-shadow-lg: 0 10px 8px #0000000a, 0 4px 3px #0000001a;
297297
--drop-shadow-xl: 0 20px 13px #00000008, 0 8px 5px #00000014;
298298
--drop-shadow-2xl: 0 25px 25px #00000026;
299-
--spacing-px: 1px;
300-
--spacing-0: 0px;
301-
--spacing-0_5: .125rem;
302-
--spacing-1: .25rem;
303-
--spacing-1_5: .375rem;
304-
--spacing-2: .5rem;
305-
--spacing-2_5: .625rem;
306-
--spacing-3: .75rem;
307-
--spacing-3_5: .875rem;
308-
--spacing-4: 1rem;
309-
--spacing-5: 1.25rem;
310-
--spacing-6: 1.5rem;
311-
--spacing-7: 1.75rem;
312-
--spacing-8: 2rem;
313-
--spacing-9: 2.25rem;
314-
--spacing-10: 2.5rem;
315-
--spacing-11: 2.75rem;
316-
--spacing-12: 3rem;
317-
--spacing-14: 3.5rem;
318-
--spacing-16: 4rem;
319-
--spacing-20: 5rem;
320-
--spacing-24: 6rem;
321-
--spacing-28: 7rem;
322-
--spacing-32: 8rem;
323-
--spacing-36: 9rem;
324-
--spacing-40: 10rem;
325-
--spacing-44: 11rem;
326-
--spacing-48: 12rem;
327-
--spacing-52: 13rem;
328-
--spacing-56: 14rem;
329-
--spacing-60: 15rem;
330-
--spacing-64: 16rem;
331-
--spacing-72: 18rem;
332-
--spacing-80: 20rem;
333-
--spacing-96: 24rem;
299+
--spacing: .25rem;
334300
--width-3xs: 16rem;
335301
--width-2xs: 18rem;
336302
--width-xs: 20rem;
@@ -386,14 +352,6 @@ exports[`\`@import 'tailwindcss'\` is replaced with the generated CSS 1`] = `
386352
--line-height-normal: 1.5;
387353
--line-height-relaxed: 1.625;
388354
--line-height-loose: 2;
389-
--line-height-3: .75rem;
390-
--line-height-4: 1rem;
391-
--line-height-5: 1.25rem;
392-
--line-height-6: 1.5rem;
393-
--line-height-7: 1.75rem;
394-
--line-height-8: 2rem;
395-
--line-height-9: 2.25rem;
396-
--line-height-10: 2.5rem;
397355
--perspective-dramatic: 100px;
398356
--perspective-near: 300px;
399357
--perspective-normal: 500px;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function migrate(input: string) {
2424
it('should migrate `theme(…)` to `var(…)`', async () => {
2525
expect(
2626
await migrate(css`
27-
@media theme(spacing.4) {
27+
@media theme(screens.sm) {
2828
.foo {
2929
background-color: theme(colors.red.900);
3030
color: theme(colors.red.900 / 75%);
@@ -33,7 +33,7 @@ it('should migrate `theme(…)` to `var(…)`', async () => {
3333
}
3434
`),
3535
).toMatchInlineSnapshot(`
36-
"@media theme(--spacing-4) {
36+
"@media theme(--breakpoint-sm) {
3737
.foo {
3838
background-color: var(--color-red-900);
3939
color: theme(--color-red-900 / 75%);

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { __unstable__loadDesignSystem } from '@tailwindcss/node'
22
import { expect, test } from 'vitest'
33
import { themeToVar } from './theme-to-var'
44

5+
let css = String.raw
6+
57
test.each([
68
// Keep candidates that don't contain `theme(…)` or `theme(…, …)`
79
['[color:red]', '[color:red]'],
@@ -107,9 +109,51 @@ test.each([
107109
'[--foo:theme(colors.red.500/50/50)_var(--color-blue-200)]/50',
108110
],
109111
])('%s => %s', async (candidate, result) => {
110-
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', {
111-
base: __dirname,
112-
})
112+
let designSystem = await __unstable__loadDesignSystem(
113+
css`
114+
@import 'tailwindcss';
115+
@theme {
116+
--spacing-px: 1px;
117+
--spacing-0: 0px;
118+
--spacing-0_5: 0.125rem;
119+
--spacing-1: 0.25rem;
120+
--spacing-1_5: 0.375rem;
121+
--spacing-2: 0.5rem;
122+
--spacing-2_5: 0.625rem;
123+
--spacing-3: 0.75rem;
124+
--spacing-3_5: 0.875rem;
125+
--spacing-4: 1rem;
126+
--spacing-5: 1.25rem;
127+
--spacing-6: 1.5rem;
128+
--spacing-7: 1.75rem;
129+
--spacing-8: 2rem;
130+
--spacing-9: 2.25rem;
131+
--spacing-10: 2.5rem;
132+
--spacing-11: 2.75rem;
133+
--spacing-12: 3rem;
134+
--spacing-14: 3.5rem;
135+
--spacing-16: 4rem;
136+
--spacing-20: 5rem;
137+
--spacing-24: 6rem;
138+
--spacing-28: 7rem;
139+
--spacing-32: 8rem;
140+
--spacing-36: 9rem;
141+
--spacing-40: 10rem;
142+
--spacing-44: 11rem;
143+
--spacing-48: 12rem;
144+
--spacing-52: 13rem;
145+
--spacing-56: 14rem;
146+
--spacing-60: 15rem;
147+
--spacing-64: 16rem;
148+
--spacing-72: 18rem;
149+
--spacing-80: 20rem;
150+
--spacing-96: 24rem;
151+
}
152+
`,
153+
{
154+
base: __dirname,
155+
},
156+
)
113157

114158
expect(themeToVar(designSystem, {}, candidate)).toEqual(result)
115159
})

packages/tailwindcss/src/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -295,41 +295,7 @@ exports[`compiling CSS > \`@tailwind utilities\` is replaced by utilities using
295295
--drop-shadow-lg: 0 10px 8px #0000000a, 0 4px 3px #0000001a;
296296
--drop-shadow-xl: 0 20px 13px #00000008, 0 8px 5px #00000014;
297297
--drop-shadow-2xl: 0 25px 25px #00000026;
298-
--spacing-px: 1px;
299-
--spacing-0: 0px;
300-
--spacing-0_5: .125rem;
301-
--spacing-1: .25rem;
302-
--spacing-1_5: .375rem;
303-
--spacing-2: .5rem;
304-
--spacing-2_5: .625rem;
305-
--spacing-3: .75rem;
306-
--spacing-3_5: .875rem;
307-
--spacing-4: 1rem;
308-
--spacing-5: 1.25rem;
309-
--spacing-6: 1.5rem;
310-
--spacing-7: 1.75rem;
311-
--spacing-8: 2rem;
312-
--spacing-9: 2.25rem;
313-
--spacing-10: 2.5rem;
314-
--spacing-11: 2.75rem;
315-
--spacing-12: 3rem;
316-
--spacing-14: 3.5rem;
317-
--spacing-16: 4rem;
318-
--spacing-20: 5rem;
319-
--spacing-24: 6rem;
320-
--spacing-28: 7rem;
321-
--spacing-32: 8rem;
322-
--spacing-36: 9rem;
323-
--spacing-40: 10rem;
324-
--spacing-44: 11rem;
325-
--spacing-48: 12rem;
326-
--spacing-52: 13rem;
327-
--spacing-56: 14rem;
328-
--spacing-60: 15rem;
329-
--spacing-64: 16rem;
330-
--spacing-72: 18rem;
331-
--spacing-80: 20rem;
332-
--spacing-96: 24rem;
298+
--spacing: .25rem;
333299
--width-3xs: 16rem;
334300
--width-2xs: 18rem;
335301
--width-xs: 20rem;
@@ -385,14 +351,6 @@ exports[`compiling CSS > \`@tailwind utilities\` is replaced by utilities using
385351
--line-height-normal: 1.5;
386352
--line-height-relaxed: 1.625;
387353
--line-height-loose: 2;
388-
--line-height-3: .75rem;
389-
--line-height-4: 1rem;
390-
--line-height-5: 1.25rem;
391-
--line-height-6: 1.5rem;
392-
--line-height-7: 1.75rem;
393-
--line-height-8: 2rem;
394-
--line-height-9: 2.25rem;
395-
--line-height-10: 2.5rem;
396354
--perspective-dramatic: 100px;
397355
--perspective-near: 300px;
398356
--perspective-normal: 500px;
@@ -404,7 +362,7 @@ exports[`compiling CSS > \`@tailwind utilities\` is replaced by utilities using
404362
}
405363
406364
.w-4 {
407-
width: var(--spacing-4, 1rem);
365+
width: calc(var(--spacing, .25rem) * 4);
408366
}
409367
410368
.bg-red-500 {

0 commit comments

Comments
 (0)