Skip to content

Commit

Permalink
Throw if contains a class
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed May 8, 2020
1 parent 99a5fea commit ad850ac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/processPlugins.test.js
Expand Up @@ -960,6 +960,25 @@ test('when important is a selector it is used to scope utilities instead of addi
`)
})

test('when important contains a class an error is thrown', () => {
expect(() => {
processPlugins(
[
function({ addUtilities }) {
addUtilities({
'.rotate-90': {
transform: 'rotate(90deg)',
},
})
},
],
makeConfig({
important: '#app .project',
})
)
}).toThrow()
})

test('when important is a selector it scopes all selectors in a rule, even though defining utilities like this is stupid', () => {
const { utilities } = processPlugins(
[
Expand Down
15 changes: 15 additions & 0 deletions src/util/processPlugins.js
Expand Up @@ -9,6 +9,7 @@ import parseObjectStyles from '../util/parseObjectStyles'
import prefixSelector from '../util/prefixSelector'
import wrapWithVariants from '../util/wrapWithVariants'
import increaseSpecificity from '../util/increaseSpecificity'
import selectorParser from 'postcss-selector-parser'

function parseStyles(styles) {
if (!Array.isArray(styles)) {
Expand All @@ -18,6 +19,14 @@ function parseStyles(styles) {
return _.flatMap(styles, style => (style instanceof Node ? style : parseObjectStyles(style)))
}

function containsClass(value) {
return selectorParser(selectors => {
let classFound = false
selectors.walkClasses(() => (classFound = true))
return classFound
}).transformSync(value)
}

export default function(plugins, config) {
const pluginBaseStyles = []
const pluginComponents = []
Expand Down Expand Up @@ -86,6 +95,12 @@ export default function(plugins, config) {
if (config.important === true) {
rule.walkDecls(decl => (decl.important = true))
} else if (typeof config.important === 'string') {
if (containsClass(config.important)) {
throw rule.error(
`Classes are not allowed when using the \`important\` option with a string argument. Please use an ID instead.`
)
}

rule.selectors = rule.selectors.map(selector => {
return increaseSpecificity(config.important, selector)
})
Expand Down

1 comment on commit ad850ac

@duyhoang-wirestone
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamwathan Whats the approach for having tailwind classes scoped to multiple areas of a page?

We are integrating TW into an existing website, and cannot blanket activate preflight. Instead we had previously used the important: '.tw' utility to create an activating selector.

Please sign in to comment.