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

Move important selector to the front when @apply-ing selector-modifying variants in custom utilities #8313

Merged
merged 2 commits into from May 9, 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 @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove default `[hidden]` style in preflight ([#8248](https://github.com/tailwindlabs/tailwindcss/pull/8248))
- Only check selectors containing base apply candidates for circular dependencies ([#8222](https://github.com/tailwindlabs/tailwindcss/pull/8222))
- Rewrite default class extractor ([#8204](https://github.com/tailwindlabs/tailwindcss/pull/8204))
- Move `important` selector to the front when `@apply`-ing selector-modifying variants in custom utilities ([#8313](https://github.com/tailwindlabs/tailwindcss/pull/8313))

### Changed

Expand Down
22 changes: 21 additions & 1 deletion src/lib/expandApplyAtRules.js
Expand Up @@ -471,7 +471,27 @@ function processApply(root, context, localCache) {
return
}

rule.selector = replaceSelector(parent.selector, rule.selector, applyCandidate)
// Strip the important selector from the parent selector if at the beginning
let importantSelector =
typeof context.tailwindConfig.important === 'string'
? context.tailwindConfig.important
: null

// We only want to move the "important" selector if this is a Tailwind-generated utility
// We do *not* want to do this for user CSS that happens to be structured the same
let isGenerated = parent.raws.tailwind !== undefined

let parentSelector =
isGenerated && importantSelector && parent.selector.indexOf(importantSelector) === 0
? parent.selector.slice(importantSelector.length)
: parent.selector

rule.selector = replaceSelector(parentSelector, rule.selector, applyCandidate)

// And then re-add it if it was removed
if (importantSelector && parentSelector !== parent.selector) {
rule.selector = `${importantSelector} ${rule.selector}`
}

rule.walkDecls((d) => {
d.important = meta.important || important
Expand Down
81 changes: 81 additions & 0 deletions tests/apply.test.js
Expand Up @@ -1467,3 +1467,84 @@ it('should apply using the updated user CSS when the source has changed', async
}
`)
})

it('apply + layer utilities + selector variants (like group) + important selector', async () => {
let config = {
important: '#myselector',
content: [{ raw: html`<div class="custom-utility"></div>` }],
plugins: [],
}

let input = css`
@tailwind utilities;
@layer utilities {
.custom-utility {
@apply font-normal group-hover:underline;
}
}
`

let result = await run(input, config)

expect(result.css).toMatchFormattedCss(css`
#myselector .custom-utility {
font-weight: 400;
}

#myselector .group:hover .custom-utility {
text-decoration-line: underline;
}
`)
})

it('apply + user CSS + selector variants (like group) + important selector (1)', async () => {
let config = {
important: '#myselector',
content: [{ raw: html`<div class="custom-utility"></div>` }],
plugins: [],
}

let input = css`
.custom-utility {
@apply font-normal group-hover:underline;
}
`

let result = await run(input, config)

expect(result.css).toMatchFormattedCss(css`
.custom-utility {
font-weight: 400;
}

.group:hover .custom-utility {
text-decoration-line: underline;
}
`)
})

it('apply + user CSS + selector variants (like group) + important selector (2)', async () => {
let config = {
important: '#myselector',
content: [{ raw: html`<div class="custom-utility"></div>` }],
plugins: [],
}

let input = css`
#myselector .custom-utility {
@apply font-normal group-hover:underline;
}
`

let result = await run(input, config)

expect(result.css).toMatchFormattedCss(css`
#myselector .custom-utility {
font-weight: 400;
}

.group:hover #myselector .custom-utility {
text-decoration-line: underline;
}
`)
})