Skip to content

Commit

Permalink
Support using functions as colors when disabling color opacity utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Sep 10, 2021
1 parent 9daebe4 commit 7cbd9dc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 47 deletions.
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.

85 changes: 80 additions & 5 deletions tests/opacity.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
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 +27,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;
}
`)
})
})

expect(result.css).toMatchCss(expected)
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,
},
}

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));
}
`)
})
})

0 comments on commit 7cbd9dc

Please sign in to comment.