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

Fix defining colors as functions when color opacity plugins are disabled #5470

Merged
merged 1 commit into from
Sep 10, 2021
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
41 changes: 31 additions & 10 deletions src/corePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,11 @@ export let divideColor = ({ matchUtilities, theme, corePlugins }) => {
{
divide: (value) => {
if (!corePlugins('divideOpacity')) {
return { ['& > :not([hidden]) ~ :not([hidden])']: { 'border-color': value } }
return {
['& > :not([hidden]) ~ :not([hidden])']: {
'border-color': toColorValue(value),
},
}
}

return {
Expand Down Expand Up @@ -1174,9 +1178,10 @@ export let borderStyle = ({ addUtilities }) => {

export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
if (!corePlugins('borderOpacity')) {
let value = theme('borderColor.DEFAULT', 'currentColor')
addBase({
'@defaults border-width': {
'border-color': theme('borderColor.DEFAULT', 'currentColor'),
'border-color': toColorValue(value),
},
})
} else {
Expand All @@ -1193,7 +1198,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
{
border: (value) => {
if (!corePlugins('borderOpacity')) {
return { 'border-color': value }
return {
'border-color': toColorValue(value),
}
}

return withAlphaVariable({
Expand All @@ -1213,7 +1220,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
{
'border-t': (value) => {
if (!corePlugins('borderOpacity')) {
return { 'border-top-color': value }
return {
'border-top-color': toColorValue(value),
}
}

return withAlphaVariable({
Expand All @@ -1224,7 +1233,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
},
'border-r': (value) => {
if (!corePlugins('borderOpacity')) {
return { 'border-right-color': value }
return {
'border-right-color': toColorValue(value),
}
}

return withAlphaVariable({
Expand All @@ -1235,7 +1246,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
},
'border-b': (value) => {
if (!corePlugins('borderOpacity')) {
return { 'border-bottom-color': value }
return {
'border-bottom-color': toColorValue(value),
}
}

return withAlphaVariable({
Expand All @@ -1246,7 +1259,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
},
'border-l': (value) => {
if (!corePlugins('borderOpacity')) {
return { 'border-left-color': value }
return {
'border-left-color': toColorValue(value),
}
}

return withAlphaVariable({
Expand All @@ -1272,7 +1287,9 @@ export let backgroundColor = ({ matchUtilities, theme, corePlugins }) => {
{
bg: (value) => {
if (!corePlugins('backgroundOpacity')) {
return { 'background-color': value }
return {
'background-color': toColorValue(value),
}
}

return withAlphaVariable({
Expand Down Expand Up @@ -1539,7 +1556,7 @@ export let textColor = ({ matchUtilities, theme, corePlugins }) => {
{
text: (value) => {
if (!corePlugins('textOpacity')) {
return { color: value }
return { color: toColorValue(value) }
}

return withAlphaVariable({
Expand Down Expand Up @@ -1583,7 +1600,11 @@ export let placeholderColor = ({ matchUtilities, theme, corePlugins }) => {
{
placeholder: (value) => {
if (!corePlugins('placeholderOpacity')) {
return { '&::placeholder': { color: value } }
return {
'&::placeholder': {
color: toColorValue(value),
},
}
}

return {
Expand Down
15 changes: 0 additions & 15 deletions tests/opacity.test.css

This file was deleted.

17 changes: 0 additions & 17 deletions tests/opacity.test.html

This file was deleted.

88 changes: 80 additions & 8 deletions tests/opacity.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import fs from 'fs'
import path from 'path'

import { run } from './util/run'
import { run, html, css } from './util/run'

test('opacity', () => {
let config = {
darkMode: 'class',
content: [path.resolve(__dirname, './opacity.test.html')],
content: [
{
raw: html`
<div class="divide-black"></div>
<div class="border-black"></div>
<div class="bg-black"></div>
<div class="text-black"></div>
<div class="placeholder-black"></div>
`,
},
],
corePlugins: {
backgroundOpacity: false,
borderOpacity: false,
Expand All @@ -17,9 +24,74 @@ test('opacity', () => {
}

return run('@tailwind utilities', config).then((result) => {
let expectedPath = path.resolve(__dirname, './opacity.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')
expect(result.css).toMatchCss(css`
.divide-black > :not([hidden]) ~ :not([hidden]) {
border-color: #000;
}
.border-black {
border-color: #000;
}
.bg-black {
background-color: #000;
}
.text-black {
color: #000;
}
.placeholder-black::placeholder {
color: #000;
}
`)
})
})

test('colors defined as functions work when opacity plugins are disabled', () => {
let config = {
darkMode: 'class',
content: [
{
raw: html`
<div class="divide-primary"></div>
<div class="border-primary"></div>
<div class="bg-primary"></div>
<div class="text-primary"></div>
<div class="placeholder-primary"></div>
`,
},
],
theme: {
colors: {
primary: ({ opacityValue }) =>
opacityValue === undefined
? 'rgb(var(--color-primary))'
: `rgb(var(--color-primary) / ${opacityValue})`,
},
},
corePlugins: {
backgroundOpacity: false,
borderOpacity: false,
divideOpacity: false,
placeholderOpacity: false,
textOpacity: false,
},
}

expect(result.css).toMatchCss(expected)
return run('@tailwind utilities', config).then((result) => {
expect(result.css).toMatchCss(css`
.divide-primary > :not([hidden]) ~ :not([hidden]) {
border-color: rgb(var(--color-primary));
}
.border-primary {
border-color: rgb(var(--color-primary));
}
.bg-primary {
background-color: rgb(var(--color-primary));
}
.text-primary {
color: rgb(var(--color-primary));
}
.placeholder-primary::placeholder {
color: rgb(var(--color-primary));
}
`)
})
})