Skip to content

Commit

Permalink
Support applying non-prefixed class when using important scope
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Aug 6, 2019
1 parent 763ee71 commit 8331d9f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
25 changes: 24 additions & 1 deletion __tests__/applyAtRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ test('you can apply utility classes without using the given prefix when using a
})
})

test('you can apply utility classes without specificity prefix even if important (selector) is used.', () => {
test('you can apply utility classes without specificity prefix even if important (selector) is used', () => {
const input = `
.foo { @apply .mt-8 .mb-8; }
`
Expand All @@ -264,3 +264,26 @@ test('you can apply utility classes without specificity prefix even if important
expect(result.warnings().length).toBe(0)
})
})

test('you can apply utility classes without using the given prefix even if important (selector) is used', () => {
const input = `
.foo { @apply .tw-mt-4 .mb-4; }
`

const expected = `
.foo { margin-top: 1rem; margin-bottom: 1rem; }
`

const config = resolveConfig([
{
...defaultConfig,
prefix: 'tw-',
important: '#app',
},
])

return run(input, config, processPlugins(corePlugins(config), config).utilities).then(result => {
expect(result.css).toEqual(expected)
expect(result.warnings().length).toBe(0)
})
})
40 changes: 35 additions & 5 deletions src/lib/substituteClassApplyAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,41 @@ export default function(config, generatedUtilities) {

return _.reduce(
[
() => findClass(classToApply, classLookup, onError),
() => findClass(classToApply, shadowLookup, onError),
() => findClass(prefixSelector(config.prefix, classToApply), shadowLookup, onError),
// prettier-ignore
() => findClass(increaseSpecificity(config.important, classToApply), shadowLookup, onError),
// Find exact class match in user's CSS
() => {
return findClass(classToApply, classLookup, onError)
},
// Find exact class match in shadow lookup
() => {
return findClass(classToApply, shadowLookup, onError)
},
// Find prefixed version of class in shadow lookup
() => {
return findClass(
prefixSelector(config.prefix, classToApply),
shadowLookup,
onError
)
},
// Find important-scoped version of class in shadow lookup
() => {
return findClass(
increaseSpecificity(config.important, classToApply),
shadowLookup,
onError
)
},
// Find important-scoped and prefixed version of class in shadow lookup
() => {
return findClass(
increaseSpecificity(
config.important,
prefixSelector(config.prefix, classToApply)
),
shadowLookup,
onError
)
},
() => {
// prettier-ignore
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.`)
Expand Down

0 comments on commit 8331d9f

Please sign in to comment.