Skip to content

Commit

Permalink
Support arbitrary color with opacity modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Sep 26, 2021
1 parent 635d0d8 commit 48ab491
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const PATTERNS = [
/([^<>"'`\s]*\[\w*\("[^"'`\s]*"\)\])/.source, // bg-[url("...")]
/([^<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']`
/([^<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]`
/([^<>"'`\s]*\[[^"'`\s]+\])/.source, // `fill-[#bada55]`
/([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50`
/([^<>"'`\s]*[^"'`\s:])/.source, // `px-1.5`, `uppercase` but not `uppercase:`].join('|')
].join('|')
const BROAD_MATCH_GLOBAL_REGEXP = new RegExp(PATTERNS, 'g')
Expand Down
13 changes: 10 additions & 3 deletions src/util/pluginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,23 @@ export function asColor(modifier, lookup = {}, tailwindConfig = {}) {

let [color, alpha] = splitAlpha(modifier)

if (lookup[color] !== undefined) {
if (alpha !== undefined) {
let normalizedColor =
lookup[color] ?? (isArbitraryValue(color) ? color.slice(1, -1) : undefined)

if (normalizedColor === undefined) {
return undefined
}

if (isArbitraryValue(alpha)) {
return withAlphaValue(lookup[color], alpha.slice(1, -1))
return withAlphaValue(normalizedColor, alpha.slice(1, -1))
}

if (tailwindConfig.theme?.opacity?.[alpha] === undefined) {
return undefined
}

return withAlphaValue(lookup[color], tailwindConfig.theme.opacity[alpha])
return withAlphaValue(normalizedColor, tailwindConfig.theme.opacity[alpha])
}

return asValue(modifier, lookup, { validate: validateColor })
Expand Down
65 changes: 65 additions & 0 deletions tests/color-opacity-modifiers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,71 @@ test('missing alpha generates nothing', async () => {
})
})

test('arbitrary color with opacity from scale', async () => {
let config = {
mode: 'jit',
purge: [
{
raw: 'bg-[wheat]/50',
},
],
theme: {},
plugins: [],
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
.bg-\\[wheat\\]\\/50 {
background-color: rgb(245 222 179 / 0.5);
}
`)
})
})

test('arbitrary color with arbitrary opacity', async () => {
let config = {
mode: 'jit',
purge: [
{
raw: 'bg-[#bada55]/[0.2]',
},
],
theme: {},
plugins: [],
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
.bg-\\[\\#bada55\\]\\/\\[0\\.2\\] {
background-color: rgb(186 218 85 / 0.2);
}
`)
})
})

test('undefined theme color with opacity from scale', async () => {
let config = {
mode: 'jit',
purge: [
{
raw: 'bg-garbage/50',
},
],
theme: {},
plugins: [],
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(``)
})
})

test('values not in the opacity config are ignored', async () => {
let config = {
content: [{ raw: html`<div class="bg-red-500/29"></div>` }],
Expand Down

0 comments on commit 48ab491

Please sign in to comment.