Skip to content

Commit 8331d9f

Browse files
committed
Support applying non-prefixed class when using important scope
1 parent 763ee71 commit 8331d9f

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

__tests__/applyAtRule.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ test('you can apply utility classes without using the given prefix when using a
243243
})
244244
})
245245

246-
test('you can apply utility classes without specificity prefix even if important (selector) is used.', () => {
246+
test('you can apply utility classes without specificity prefix even if important (selector) is used', () => {
247247
const input = `
248248
.foo { @apply .mt-8 .mb-8; }
249249
`
@@ -264,3 +264,26 @@ test('you can apply utility classes without specificity prefix even if important
264264
expect(result.warnings().length).toBe(0)
265265
})
266266
})
267+
268+
test('you can apply utility classes without using the given prefix even if important (selector) is used', () => {
269+
const input = `
270+
.foo { @apply .tw-mt-4 .mb-4; }
271+
`
272+
273+
const expected = `
274+
.foo { margin-top: 1rem; margin-bottom: 1rem; }
275+
`
276+
277+
const config = resolveConfig([
278+
{
279+
...defaultConfig,
280+
prefix: 'tw-',
281+
important: '#app',
282+
},
283+
])
284+
285+
return run(input, config, processPlugins(corePlugins(config), config).utilities).then(result => {
286+
expect(result.css).toEqual(expected)
287+
expect(result.warnings().length).toBe(0)
288+
})
289+
})

src/lib/substituteClassApplyAtRules.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,41 @@ export default function(config, generatedUtilities) {
8383

8484
return _.reduce(
8585
[
86-
() => findClass(classToApply, classLookup, onError),
87-
() => findClass(classToApply, shadowLookup, onError),
88-
() => findClass(prefixSelector(config.prefix, classToApply), shadowLookup, onError),
89-
// prettier-ignore
90-
() => findClass(increaseSpecificity(config.important, classToApply), shadowLookup, onError),
86+
// Find exact class match in user's CSS
87+
() => {
88+
return findClass(classToApply, classLookup, onError)
89+
},
90+
// Find exact class match in shadow lookup
91+
() => {
92+
return findClass(classToApply, shadowLookup, onError)
93+
},
94+
// Find prefixed version of class in shadow lookup
95+
() => {
96+
return findClass(
97+
prefixSelector(config.prefix, classToApply),
98+
shadowLookup,
99+
onError
100+
)
101+
},
102+
// Find important-scoped version of class in shadow lookup
103+
() => {
104+
return findClass(
105+
increaseSpecificity(config.important, classToApply),
106+
shadowLookup,
107+
onError
108+
)
109+
},
110+
// Find important-scoped and prefixed version of class in shadow lookup
111+
() => {
112+
return findClass(
113+
increaseSpecificity(
114+
config.important,
115+
prefixSelector(config.prefix, classToApply)
116+
),
117+
shadowLookup,
118+
onError
119+
)
120+
},
91121
() => {
92122
// prettier-ignore
93123
throw onError(`\`@apply\` cannot be used with \`${classToApply}\` because \`${classToApply}\` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that \`${classToApply}\` exists, make sure that any \`@import\` statements are being properly processed *before* Tailwind CSS sees your CSS, as \`@apply\` can only be used for classes in the same CSS tree.`)

0 commit comments

Comments
 (0)