Skip to content

Commit 62de02a

Browse files
authored
Support combining arbitrary shadows without a color with shadow color utilities (tailwindlabs#13876)
* Support combining arbitrary shadows without a color with shadow color utilities * Update changelog --------- Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
1 parent 332347e commit 62de02a

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Include variable in output for bare utilities like `rounded` ([#13836](https://github.com/tailwindlabs/tailwindcss/pull/13836))
1515
- Preserve list semantics for `ul`, `li`, and `menu` by default ([#13815](https://github.com/tailwindlabs/tailwindcss/pull/13815))
1616

17+
### Fixed
18+
19+
- Support combining arbitrary shadows without a color with shadow color utilities ([#13876](https://github.com/tailwindlabs/tailwindcss/pull/13876))
20+
1721
## [4.0.0-alpha.16] - 2024-06-07
1822

1923
### Fixed

packages/tailwindcss/src/utilities.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12019,7 +12019,7 @@ test('shadow', () => {
1201912019
1202012020
.shadow-\\[10px_10px\\] {
1202112021
--tw-shadow: 10px 10px;
12022-
--tw-shadow-colored: 10px 10px;
12022+
--tw-shadow-colored: 10px 10px var(--tw-shadow-color);
1202312023
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
1202412024
}
1202512025
@@ -12271,7 +12271,7 @@ test('inset-shadow', () => {
1227112271
1227212272
.inset-shadow-\\[10px_10px\\] {
1227312273
--tw-inset-shadow: inset 10px 10px;
12274-
--tw-inset-shadow-colored: inset 10px 10px;
12274+
--tw-inset-shadow-colored: inset 10px 10px var(--tw-inset-shadow-color);
1227512275
box-shadow: var(--tw-inset-shadow), var(--tw-inset-ring-shadow), var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow);
1227612276
}
1227712277

packages/tailwindcss/src/utils/replace-shadow-colors.test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,35 @@ import { replaceShadowColors } from './replace-shadow-colors'
33

44
const table = [
55
{
6-
input: ['var(--my-shadow)'],
6+
input: 'var(--my-shadow)',
77
output: 'var(--my-shadow)',
88
},
99
{
10-
input: ['1px var(--my-shadow)'],
10+
input: '1px var(--my-shadow)',
1111
output: '1px var(--my-shadow)',
1212
},
1313
{
14-
input: ['1px 1px var(--my-color)'],
14+
input: '1px 1px var(--my-color)',
1515
output: '1px 1px var(--tw-shadow-color)',
1616
},
1717
{
18-
input: ['0 0 0 var(--my-color)'],
18+
input: '0 0 0 var(--my-color)',
1919
output: '0 0 0 var(--tw-shadow-color)',
2020
},
2121
{
22-
input: ['var(--my-shadow)', '1px 1px var(--my-color)', '0 0 1px var(--my-color)'],
22+
input: '1px 2px',
23+
output: '1px 2px var(--tw-shadow-color)',
24+
},
25+
{
26+
input: '1px 2px 3px',
27+
output: '1px 2px 3px var(--tw-shadow-color)',
28+
},
29+
{
30+
input: '1px 2px 3px 4px',
31+
output: '1px 2px 3px 4px var(--tw-shadow-color)',
32+
},
33+
{
34+
input: ['var(--my-shadow)', '1px 1px var(--my-color)', '0 0 1px var(--my-color)'].join(', '),
2335
output: [
2436
'var(--my-shadow)',
2537
'1px 1px var(--tw-shadow-color)',
@@ -29,9 +41,9 @@ const table = [
2941
]
3042

3143
it.each(table)(
32-
'should be possible to get the names for an animation: $output',
44+
'should replace the color of box-shadow $input with $output',
3345
({ input, output }) => {
34-
let parsed = replaceShadowColors(input.join(', '), 'var(--tw-shadow-color)')
46+
let parsed = replaceShadowColors(input, 'var(--tw-shadow-color)')
3547
expect(parsed).toEqual(output)
3648
},
3749
)

packages/tailwindcss/src/utils/replace-shadow-colors.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ export function replaceShadowColors(input: string, replacement: string): string
2727
}
2828
}
2929

30-
// Only replace if we found an x-offset, y-offset, and color, otherwise we
31-
// may be replacing the wrong part of the box-shadow.
32-
if (offsetX !== null && offsetY !== null && color !== null) {
30+
// If the x and y offsets were not detected, the shadow is either invalid or
31+
// using a variable to represent more than one field in the shadow value, so
32+
// we can't know what to replace.
33+
if (offsetX === null || offsetY === null) continue
34+
35+
if (color !== null) {
36+
// If a color was found, replace the color.
3337
input = input.replace(color, replacement)
38+
} else {
39+
// If no color was found, assume the shadow is relying on the browser
40+
// default shadow color and append the replacement color.
41+
input = `${input} ${replacement}`
3442
}
3543
}
3644

packages/tailwindcss/tests/ui.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ test('shadow colors', async ({ page }) => {
166166
html`
167167
<div id="x" class="shadow shadow-red-500"></div>
168168
<div id="y" class="shadow-xl shadow-red-500"></div>
169+
<div id="z" class="shadow-[0px_2px_4px] shadow-red-500"></div>
169170
`,
170171
)
171172

@@ -187,6 +188,15 @@ test('shadow colors', async ({ page }) => {
187188
'rgb(239, 68, 68) 0px 20px 25px -5px, rgb(239, 68, 68) 0px 8px 10px -6px',
188189
].join(', '),
189190
)
191+
expect(await getPropertyValue('#z', 'box-shadow')).toEqual(
192+
[
193+
'rgba(0, 0, 0, 0) 0px 0px 0px 0px',
194+
'rgba(0, 0, 0, 0) 0px 0px 0px 0px',
195+
'rgba(0, 0, 0, 0) 0px 0px 0px 0px',
196+
'rgba(0, 0, 0, 0) 0px 0px 0px 0px',
197+
'rgb(239, 68, 68) 0px 2px 4px 0px',
198+
].join(', '),
199+
)
190200
})
191201

192202
test('outline style is optional', async ({ page }) => {

0 commit comments

Comments
 (0)