Skip to content

Commit

Permalink
Support linear gradient angles as bare values (#14707)
Browse files Browse the repository at this point in the history
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 <4323180+adamwathan@users.noreply.github.com>
  • Loading branch information
adamwathan and adamwathan authored Oct 17, 2024
1 parent feeb9f1 commit 72c30d4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add first draft of new wide-gamut color palette ([#14693](https://github.com/tailwindlabs/tailwindcss/pull/14693))
- Support linear gradient angles as bare values ([#14707](https://github.com/tailwindlabs/tailwindcss/pull/14707))
- _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))

### Fixed
Expand Down
12 changes: 12 additions & 0 deletions packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9448,6 +9448,8 @@ test('bg', async () => {
'bg-linear-to-bl',
'bg-linear-to-l',
'bg-linear-to-tl',
'bg-linear-45',
'-bg-linear-45',

'bg-[url(/image.png)]',
'bg-[url:var(--my-url)]',
Expand Down Expand Up @@ -9554,6 +9556,11 @@ test('bg', async () => {
background-color: #0000;
}
.-bg-linear-45 {
--tw-gradient-position: calc(45deg * -1), ;
background-image: linear-gradient(var(--tw-gradient-stops, calc(45deg * -1)));
}
.-bg-linear-\\[1\\.3rad\\] {
--tw-gradient-position: calc(74.4845deg * -1), ;
background-image: linear-gradient(var(--tw-gradient-stops, calc(74.4845deg * -1)));
Expand Down Expand Up @@ -9604,6 +9611,11 @@ test('bg', async () => {
background-image: linear-gradient(var(--tw-gradient-stops));
}
.bg-linear-45 {
--tw-gradient-position: 45deg, ;
background-image: linear-gradient(var(--tw-gradient-stops, 45deg));
}
.bg-linear-\\[1\\.3rad\\] {
--tw-gradient-position: 74.4845deg, ;
background-image: linear-gradient(var(--tw-gradient-stops, 74.4845deg));
Expand Down
12 changes: 11 additions & 1 deletion packages/tailwindcss/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2529,8 +2529,9 @@ export function createUtilities(theme: Theme) {
utilities.functional('bg-linear', (candidate) => {
if (!candidate.value || candidate.modifier) return

let value = candidate.value.value

if (candidate.value.kind === 'arbitrary') {
let value: string | null = candidate.value.value
let type = candidate.value.dataType ?? inferDataType(value, ['angle'])

switch (type) {
Expand All @@ -2551,6 +2552,15 @@ export function createUtilities(theme: Theme) {
]
}
}
} else {
if (!isPositiveInteger(value)) return

value = withNegative(`${value}deg`, candidate)

return [
decl('--tw-gradient-position', `${value},`),
decl('background-image', `linear-gradient(var(--tw-gradient-stops,${value}))`),
]
}
})

Expand Down
19 changes: 18 additions & 1 deletion packages/tailwindcss/tests/ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ for (let [classes, expected] of [
'bg-linear-to-r from-red to-blue',
'linear-gradient(to right, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
],
[
'bg-linear-45 from-red to-blue',
'linear-gradient(45deg, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
],
[
'-bg-linear-45 from-red to-blue',
// Chrome reports a different (but also correct) computed value than Firefox/WebKit so we check
// for both options.
[
'linear-gradient(-45deg, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
'linear-gradient(calc(-45deg), rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)',
],
],
[
'bg-linear-to-r via-red to-blue',
'linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(255, 0, 0) 50%, rgb(0, 0, 255) 100%)',
Expand All @@ -60,7 +73,11 @@ for (let [classes, expected] of [
html`<div id="x" class="${classes}">Hello world</div>`,
)

expect(await getPropertyValue('#x', 'background-image')).toEqual(expected)
if (Array.isArray(expected)) {
expect(expected).toContain(await getPropertyValue('#x', 'background-image'))
} else {
expect(await getPropertyValue('#x', 'background-image')).toEqual(expected)
}
})
}

Expand Down

0 comments on commit 72c30d4

Please sign in to comment.