Skip to content

Commit 72c30d4

Browse files
authored
Support linear gradient angles as bare values (#14707)
This PR adds support for linear gradient angles as bare values, like this: ``` bg-linear-45 => linear-gradient(45deg, …) ``` We already support this for [conic gradients](#14467), so this makes these utilities more consistent. --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent feeb9f1 commit 72c30d4

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add first draft of new wide-gamut color palette ([#14693](https://github.com/tailwindlabs/tailwindcss/pull/14693))
13+
- Support linear gradient angles as bare values ([#14707](https://github.com/tailwindlabs/tailwindcss/pull/14707))
1314
- _Upgrade (experimental)_: Migrate `theme(…)` calls to `var(…)` or to the modern `theme(…)` syntax ([#14664](https://github.com/tailwindlabs/tailwindcss/pull/14664), [#14695](https://github.com/tailwindlabs/tailwindcss/pull/14695))
1415

1516
### Fixed

packages/tailwindcss/src/utilities.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9448,6 +9448,8 @@ test('bg', async () => {
94489448
'bg-linear-to-bl',
94499449
'bg-linear-to-l',
94509450
'bg-linear-to-tl',
9451+
'bg-linear-45',
9452+
'-bg-linear-45',
94519453

94529454
'bg-[url(/image.png)]',
94539455
'bg-[url:var(--my-url)]',
@@ -9554,6 +9556,11 @@ test('bg', async () => {
95549556
background-color: #0000;
95559557
}
95569558
9559+
.-bg-linear-45 {
9560+
--tw-gradient-position: calc(45deg * -1), ;
9561+
background-image: linear-gradient(var(--tw-gradient-stops, calc(45deg * -1)));
9562+
}
9563+
95579564
.-bg-linear-\\[1\\.3rad\\] {
95589565
--tw-gradient-position: calc(74.4845deg * -1), ;
95599566
background-image: linear-gradient(var(--tw-gradient-stops, calc(74.4845deg * -1)));
@@ -9604,6 +9611,11 @@ test('bg', async () => {
96049611
background-image: linear-gradient(var(--tw-gradient-stops));
96059612
}
96069613
9614+
.bg-linear-45 {
9615+
--tw-gradient-position: 45deg, ;
9616+
background-image: linear-gradient(var(--tw-gradient-stops, 45deg));
9617+
}
9618+
96079619
.bg-linear-\\[1\\.3rad\\] {
96089620
--tw-gradient-position: 74.4845deg, ;
96099621
background-image: linear-gradient(var(--tw-gradient-stops, 74.4845deg));

packages/tailwindcss/src/utilities.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2529,8 +2529,9 @@ export function createUtilities(theme: Theme) {
25292529
utilities.functional('bg-linear', (candidate) => {
25302530
if (!candidate.value || candidate.modifier) return
25312531

2532+
let value = candidate.value.value
2533+
25322534
if (candidate.value.kind === 'arbitrary') {
2533-
let value: string | null = candidate.value.value
25342535
let type = candidate.value.dataType ?? inferDataType(value, ['angle'])
25352536

25362537
switch (type) {
@@ -2551,6 +2552,15 @@ export function createUtilities(theme: Theme) {
25512552
]
25522553
}
25532554
}
2555+
} else {
2556+
if (!isPositiveInteger(value)) return
2557+
2558+
value = withNegative(`${value}deg`, candidate)
2559+
2560+
return [
2561+
decl('--tw-gradient-position', `${value},`),
2562+
decl('background-image', `linear-gradient(var(--tw-gradient-stops,${value}))`),
2563+
]
25542564
}
25552565
})
25562566

packages/tailwindcss/tests/ui.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ for (let [classes, expected] of [
4141
'bg-linear-to-r from-red to-blue',
4242
'linear-gradient(to right, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
4343
],
44+
[
45+
'bg-linear-45 from-red to-blue',
46+
'linear-gradient(45deg, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
47+
],
48+
[
49+
'-bg-linear-45 from-red to-blue',
50+
// Chrome reports a different (but also correct) computed value than Firefox/WebKit so we check
51+
// for both options.
52+
[
53+
'linear-gradient(-45deg, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
54+
'linear-gradient(calc(-45deg), rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
55+
],
56+
],
4457
[
4558
'bg-linear-to-r via-red to-blue',
4659
'linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(255, 0, 0) 50%, rgb(0, 0, 255) 100%)',
@@ -60,7 +73,11 @@ for (let [classes, expected] of [
6073
html`<div id="x" class="${classes}">Hello world</div>`,
6174
)
6275

63-
expect(await getPropertyValue('#x', 'background-image')).toEqual(expected)
76+
if (Array.isArray(expected)) {
77+
expect(expected).toContain(await getPropertyValue('#x', 'background-image'))
78+
} else {
79+
expect(await getPropertyValue('#x', 'background-image')).toEqual(expected)
80+
}
6481
})
6582
}
6683

0 commit comments

Comments
 (0)