Skip to content

Commit 89b960d

Browse files
Fix resolution of alpha values inside color functions (#9008)
* Fix resolution of alpha values inside color functions * Update changelog
1 parent 0b5bfc8 commit 89b960d

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

CHANGELOG.md

+1
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
### Fixed
1111

1212
- Don’t prefix classes within reused arbitrary variants ([#8992](https://github.com/tailwindlabs/tailwindcss/pull/8992))
13+
- Fix usage of alpha values inside single-named colors that are functions ([#9008](https://github.com/tailwindlabs/tailwindcss/pull/9008))
1314

1415
## [3.1.7] - 2022-07-29
1516

src/util/resolveConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function resolveFunctionKeys(object) {
180180
val = val[path[index++]]
181181

182182
let shouldResolveAsFn =
183-
isFunction(val) && (path.alpha === undefined || index < path.length - 1)
183+
isFunction(val) && (path.alpha === undefined || index <= path.length - 1)
184184

185185
val = shouldResolveAsFn ? val(resolvePath, configUtils) : val
186186
}

tests/opacity.test.js

+67
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,70 @@ it('Theme functions can reference values with slashes in brackets', () => {
761761
`)
762762
})
763763
})
764+
765+
it('works with opacity values defined as a placeholder or a function in when colors is a function', () => {
766+
let config = {
767+
content: [
768+
{
769+
raw: html`
770+
<div
771+
class="bg-foo10 bg-foo20 bg-foo30 bg-foo40 bg-foo11 bg-foo21 bg-foo31 bg-foo41"
772+
></div>
773+
`,
774+
},
775+
],
776+
theme: {
777+
colors: () => ({
778+
foobar1: ({ opacityValue }) => `rgb(255 100 0 / ${opacityValue ?? '100%'})`,
779+
foobar2: `rgb(255 100 0 / <alpha-value>)`,
780+
foobar3: {
781+
100: ({ opacityValue }) => `rgb(255 100 0 / ${opacityValue ?? '100%'})`,
782+
200: `rgb(255 100 0 / <alpha-value>)`,
783+
},
784+
}),
785+
extend: {
786+
backgroundColor: ({ theme }) => ({
787+
foo10: theme('colors.foobar1'),
788+
foo20: theme('colors.foobar2'),
789+
foo30: theme('colors.foobar3.100'),
790+
foo40: theme('colors.foobar3.200'),
791+
foo11: theme('colors.foobar1 / 50%'),
792+
foo21: theme('colors.foobar2 / 50%'),
793+
foo31: theme('colors.foobar3.100 / 50%'),
794+
foo41: theme('colors.foobar3.200 / 50%'),
795+
}),
796+
},
797+
},
798+
}
799+
800+
return run('@tailwind utilities', config).then((result) => {
801+
expect(result.css).toMatchCss(css`
802+
.bg-foo10 {
803+
background-color: rgb(255 100 0 / 100%);
804+
}
805+
.bg-foo20 {
806+
--tw-bg-opacity: 1;
807+
background-color: rgb(255 100 0 / var(--tw-bg-opacity));
808+
}
809+
.bg-foo30 {
810+
background-color: rgb(255 100 0 / 100%);
811+
}
812+
.bg-foo40 {
813+
--tw-bg-opacity: 1;
814+
background-color: rgb(255 100 0 / var(--tw-bg-opacity));
815+
}
816+
.bg-foo11 {
817+
background-color: rgb(255 100 0 / 50%);
818+
}
819+
.bg-foo21 {
820+
background-color: rgb(255 100 0 / 50%);
821+
}
822+
.bg-foo31 {
823+
background-color: rgb(255 100 0 / 50%);
824+
}
825+
.bg-foo41 {
826+
background-color: rgb(255 100 0 / 50%);
827+
}
828+
`)
829+
})
830+
})

0 commit comments

Comments
 (0)