Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support linear gradient angles as bare values #14707

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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