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

Only add ! to selector class matching template candidate #7664

Merged
merged 2 commits into from Feb 25, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Split box shadows on top-level commas only ([#7479](https://github.com/tailwindlabs/tailwindcss/pull/7479))
- Use local user CSS cache for `@apply` ([#7524](https://github.com/tailwindlabs/tailwindcss/pull/7524))
- Invalidate context when main CSS changes ([#7626](https://github.com/tailwindlabs/tailwindcss/pull/7626))
- Only add `!` to selector class matching template candidate when using important modifier with mutli-class selectors ([#7664](https://github.com/tailwindlabs/tailwindcss/pull/7664))

### Changed

Expand Down
9 changes: 6 additions & 3 deletions src/lib/generateRules.js
Expand Up @@ -88,7 +88,7 @@ function applyPrefix(matches, context) {
return matches
}

function applyImportant(matches) {
function applyImportant(matches, classCandidate) {
if (matches.length === 0) {
return matches
}
Expand All @@ -98,7 +98,10 @@ function applyImportant(matches) {
let container = postcss.root({ nodes: [rule.clone()] })
container.walkRules((r) => {
r.selector = updateAllClasses(r.selector, (className) => {
return `!${className}`
if (className === classCandidate) {
return `!${className}`
}
return className
})
r.walkDecls((d) => (d.important = true))
})
Expand Down Expand Up @@ -514,7 +517,7 @@ function* resolveMatches(candidate, context) {
matches = applyPrefix(matches, context)

if (important) {
matches = applyImportant(matches, context)
matches = applyImportant(matches, classCandidate)
}

for (let variant of variants) {
Expand Down
19 changes: 19 additions & 0 deletions tests/important-modifier.test.js
Expand Up @@ -17,6 +17,22 @@ test('important modifier', () => {
},
],
corePlugins: { preflight: false },
plugins: [
function ({ theme, matchUtilities }) {
matchUtilities(
{
'custom-parent': (value) => {
return {
'.custom-child': {
margin: value,
},
}
},
},
{ values: theme('spacing') }
)
},
],
}

let input = css`
Expand Down Expand Up @@ -57,6 +73,9 @@ test('important modifier', () => {
.\!font-bold {
font-weight: 700 !important;
}
.\!custom-parent-5 .custom-child {
margin: 1.25rem !important;
}
.hover\:\!text-center:hover {
text-align: center !important;
}
Expand Down