Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Alpha-less colors #196

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 11 additions & 7 deletions src/corePlugins/backgroundColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const withAlphaVariable = require('tailwindcss/lib/util/withAlphaVariable').defa
const toColorValue = require('tailwindcss/lib/util/toColorValue').default
const { asColor, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
module.exports = function ({ corePlugins, matchUtilities, jit: { theme } }) {
let colorPalette = flattenColorPalette(theme.backgroundColor)

matchUtilities({
Expand All @@ -15,13 +15,17 @@ module.exports = function ({ matchUtilities, jit: { theme } }) {
return []
}

return {
[nameClass('bg', modifier)]: withAlphaVariable({
color: value,
property: 'background-color',
variable: '--tw-bg-opacity',
}),
if (corePlugins('backgroundOpacity')) {
return {
[nameClass('bg', modifier)]: withAlphaVariable({
color: value,
property: 'background-color',
variable: '--tw-bg-opacity',
}),
}
}

return { [nameClass('bg', modifier)]: { 'background-color': value } }
},
})
}
18 changes: 12 additions & 6 deletions src/corePlugins/borderColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const withAlphaVariable = require('tailwindcss/lib/util/withAlphaVariable').defa
const toColorValue = require('tailwindcss/lib/util/toColorValue').default
const { asColor, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
module.exports = function ({ corePlugins, matchUtilities, jit: { theme } }) {
let colorPalette = flattenColorPalette(theme.borderColor)

matchUtilities({
Expand All @@ -18,12 +18,18 @@ module.exports = function ({ matchUtilities, jit: { theme } }) {
return []
}

if (corePlugins('borderOpacity')) {
return {
[nameClass('border', modifier)]: withAlphaVariable({
color: value,
property: 'border-color',
variable: '--tw-border-opacity',
}),
}
}

return {
[nameClass('border', modifier)]: withAlphaVariable({
color: value,
property: 'border-color',
variable: '--tw-border-opacity',
}),
[nameClass('border', modifier)]: { 'border-color': value },
}
},
})
Expand Down
22 changes: 16 additions & 6 deletions src/corePlugins/divideColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const withAlphaVariable = require('tailwindcss/lib/util/withAlphaVariable').defa
const toColorValue = require('tailwindcss/lib/util/toColorValue').default
const { asColor, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
module.exports = function ({ corePlugins, matchUtilities, jit: { theme } }) {
let colorPalette = flattenColorPalette(theme.divideColor)

// TODO: Make sure there is no issue with DEFAULT here
Expand All @@ -15,12 +15,22 @@ module.exports = function ({ matchUtilities, jit: { theme } }) {
return []
}

if (corePlugins('divideOpacity')) {
return {
[`${nameClass('divide', modifier)} > :not([hidden]) ~ :not([hidden])`]: withAlphaVariable(
{
color: colorPalette[modifier],
property: 'border-color',
variable: '--tw-divide-opacity',
}
),
}
}

return {
[`${nameClass('divide', modifier)} > :not([hidden]) ~ :not([hidden])`]: withAlphaVariable({
color: colorPalette[modifier],
property: 'border-color',
variable: '--tw-divide-opacity',
}),
[`${nameClass('divide', modifier)} > :not([hidden]) ~ :not([hidden])`]: {
'border-color': value,
},
}
},
})
Expand Down
18 changes: 12 additions & 6 deletions src/corePlugins/placeholderColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const withAlphaVariable = require('tailwindcss/lib/util/withAlphaVariable').defa
const toColorValue = require('tailwindcss/lib/util/toColorValue').default
const { asColor, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
module.exports = function ({ corePlugins, matchUtilities, jit: { theme } }) {
let colorPalette = flattenColorPalette(theme.placeholderColor)

matchUtilities({
Expand All @@ -14,12 +14,18 @@ module.exports = function ({ matchUtilities, jit: { theme } }) {
return []
}

if (corePlugins('placeholderOpacity')) {
return {
[`${nameClass('placeholder', modifier)}::placeholder`]: withAlphaVariable({
color: value,
property: 'color',
variable: '--tw-placeholder-opacity',
}),
}
}

return {
[`${nameClass('placeholder', modifier)}::placeholder`]: withAlphaVariable({
color: value,
property: 'color',
variable: '--tw-placeholder-opacity',
}),
[`${nameClass('placeholder', modifier)}::placeholder`]: { color: value },
}
},
})
Expand Down
18 changes: 12 additions & 6 deletions src/corePlugins/textColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const withAlphaVariable = require('tailwindcss/lib/util/withAlphaVariable').defa
const toColorValue = require('tailwindcss/lib/util/toColorValue').default
const { asColor, nameClass } = require('../pluginUtils')

module.exports = function ({ matchUtilities, jit: { theme } }) {
module.exports = function ({ corePlugins, matchUtilities, jit: { theme } }) {
let colorPalette = flattenColorPalette(theme.textColor)

matchUtilities({
Expand All @@ -14,12 +14,18 @@ module.exports = function ({ matchUtilities, jit: { theme } }) {
return []
}

if (corePlugins('textOpacity')) {
return {
[nameClass('text', modifier)]: withAlphaVariable({
color: value,
property: 'color',
variable: '--tw-text-opacity',
}),
}
}

return {
[nameClass('text', modifier)]: withAlphaVariable({
color: value,
property: 'color',
variable: '--tw-text-opacity',
}),
[nameClass('text', modifier)]: { color: value },
}
},
})
Expand Down
24 changes: 24 additions & 0 deletions tests/opacity.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
* {
--tw-shadow: 0 0 #0000;
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
--tw-ring-offset-width: 0px;
--tw-ring-offset-color: #fff;
--tw-ring-color: rgba(59, 130, 246, 0.5);
--tw-ring-offset-shadow: 0 0 #0000;
--tw-ring-shadow: 0 0 #0000;
}
.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;
}
17 changes: 17 additions & 0 deletions tests/opacity.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
<link rel="stylesheet" href="./tailwind.css" />
</head>
<body>
<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>
</body>
</html>
38 changes: 38 additions & 0 deletions tests/opacity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const postcss = require('postcss')
const tailwind = require('../src/index.js')
const fs = require('fs')
const path = require('path')

function run(input, config = {}) {
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
}

test('opacity', () => {
let config = {
darkMode: 'class',
purge: [path.resolve(__dirname, './opacity.test.html')],
corePlugins: {
backgroundOpacity: false,
borderOpacity: false,
divideOpacity: false,
placeholderOpacity: false,
preflight: false,
textOpacity: false,
},
theme: {},
plugins: [],
}

let css = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

return run(css, config).then((result) => {
let expectedPath = path.resolve(__dirname, './opacity.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')

expect(result.css).toMatchCss(expected)
})
})