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

Add support for alpha values in safelist #8774

Merged
merged 1 commit into from Jul 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/setupContextUtils.js
Expand Up @@ -762,6 +762,17 @@ function registerPlugins(plugins, context) {
]
}

if ([].concat(options?.type).includes('color')) {
classes = [
...classes,
...classes.flatMap((cls) =>
Object.keys(context.tailwindConfig.theme.opacity).map(
(opacity) => `${cls}/${opacity}`
)
),
]
}

return classes
})()
: [util]
Expand Down
23 changes: 23 additions & 0 deletions tests/getClassList.test.js
Expand Up @@ -59,3 +59,26 @@ it('should generate every possible class while handling negatives and prefixes',

expect(classes).not.toContain('-tw-m-DEFAULT')
})

it('should not generate utilities with opacity by default', () => {
let config = {}
let context = createContext(resolveConfig(config))
let classes = context.getClassList()

expect(classes).not.toContain('bg-red-500/50')
})

it('should not generate utilities with opacity even if safe-listed', () => {
let config = {
safelist: [
{
pattern: /^bg-red-(400|500)(\/(40|50))?$/,
},
],
}

let context = createContext(resolveConfig(config))
let classes = context.getClassList()

expect(classes).not.toContain('bg-red-500/50')
})
92 changes: 87 additions & 5 deletions tests/safelist.test.js
Expand Up @@ -51,7 +51,7 @@ it('should safelist based on a pattern regex', () => {
content: [{ raw: html`<div class="uppercase"></div>` }],
safelist: [
{
pattern: /bg-(red)-(100|200)/,
pattern: /^bg-(red)-(100|200)$/,
variants: ['hover'],
},
],
Expand Down Expand Up @@ -92,15 +92,15 @@ it('should not generate duplicates', () => {
safelist: [
'uppercase',
{
pattern: /bg-(red)-(100|200)/,
pattern: /^bg-(red)-(100|200)$/,
variants: ['hover'],
},
{
pattern: /bg-(red)-(100|200)/,
pattern: /^bg-(red)-(100|200)$/,
variants: ['hover'],
},
{
pattern: /bg-(red)-(100|200)/,
pattern: /^bg-(red)-(100|200)$/,
variants: ['hover'],
},
],
Expand Down Expand Up @@ -141,7 +141,7 @@ it('should safelist when using a custom prefix', () => {
content: [{ raw: html`<div class="tw-uppercase"></div>` }],
safelist: [
{
pattern: /tw-bg-red-(100|200)/g,
pattern: /^tw-bg-red-(100|200)$/g,
},
],
}
Expand Down Expand Up @@ -222,3 +222,85 @@ it('should safelist negatives based on a pattern regex', () => {
`)
})
})

it('should safelist negatives based on a pattern regex', () => {
let config = {
content: [{ raw: html`<div class="uppercase"></div>` }],
safelist: [
{
pattern: /^bg-red-(400|500)(\/(40|50))?$/,
variants: ['hover'],
},
{
pattern: /^(fill|ring|text)-red-200\/50$/,
variants: ['hover'],
},
],
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchCss(css`
.bg-red-400 {
--tw-bg-opacity: 1;
background-color: rgb(248 113 113 / var(--tw-bg-opacity));
}
.bg-red-500 {
--tw-bg-opacity: 1;
background-color: rgb(239 68 68 / var(--tw-bg-opacity));
}
.bg-red-400\/40 {
background-color: rgb(248 113 113 / 0.4);
}
.bg-red-400\/50 {
background-color: rgb(248 113 113 / 0.5);
}
.bg-red-500\/40 {
background-color: rgb(239 68 68 / 0.4);
}
.bg-red-500\/50 {
background-color: rgb(239 68 68 / 0.5);
}
.fill-red-200\/50 {
fill: rgb(254 202 202 / 0.5);
}
.uppercase {
text-transform: uppercase;
}
.text-red-200\/50 {
color: rgb(254 202 202 / 0.5);
}
.ring-red-200\/50 {
--tw-ring-color: rgb(254 202 202 / 0.5);
}
.hover\:bg-red-400:hover {
--tw-bg-opacity: 1;
background-color: rgb(248 113 113 / var(--tw-bg-opacity));
}
.hover\:bg-red-500:hover {
--tw-bg-opacity: 1;
background-color: rgb(239 68 68 / var(--tw-bg-opacity));
}
.hover\:bg-red-400\/40:hover {
background-color: rgb(248 113 113 / 0.4);
}
.hover\:bg-red-400\/50:hover {
background-color: rgb(248 113 113 / 0.5);
}
.hover\:bg-red-500\/40:hover {
background-color: rgb(239 68 68 / 0.4);
}
.hover\:bg-red-500\/50:hover {
background-color: rgb(239 68 68 / 0.5);
}
.hover\:fill-red-200\/50:hover {
fill: rgb(254 202 202 / 0.5);
}
.hover\:text-red-200\/50:hover {
color: rgb(254 202 202 / 0.5);
}
.hover\:ring-red-200\/50:hover {
--tw-ring-color: rgb(254 202 202 / 0.5);
}
`)
})
})