Skip to content

Commit 8e8f47c

Browse files
committed
Respect user prefix by default when creating component classes
1 parent 7a8f8d9 commit 8e8f47c

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

__tests__/processPlugins.test.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ test('plugins respect prefix and important options by default when adding utilit
616616
`)
617617
})
618618

619-
test("component declarations are not affected by the 'prefix' option", () => {
619+
test("component declarations respect the 'prefix' option by default", () => {
620620
const [components] = processPluginsWithValidConfig({
621621
plugins: [
622622
function({ addComponents }) {
@@ -632,6 +632,32 @@ test("component declarations are not affected by the 'prefix' option", () => {
632632
},
633633
})
634634

635+
expect(css(components)).toMatchCss(`
636+
.tw-btn-blue {
637+
background-color: blue
638+
}
639+
`)
640+
})
641+
642+
test("component declarations can optionally ignore 'prefix' option", () => {
643+
const [components] = processPluginsWithValidConfig({
644+
plugins: [
645+
function({ addComponents }) {
646+
addComponents(
647+
{
648+
'.btn-blue': {
649+
backgroundColor: 'blue',
650+
},
651+
},
652+
{ respectPrefix: false }
653+
)
654+
},
655+
],
656+
options: {
657+
prefix: 'tw-',
658+
},
659+
})
660+
635661
expect(css(components)).toMatchCss(`
636662
.btn-blue {
637663
background-color: blue
@@ -672,7 +698,8 @@ test("plugins can apply the user's chosen prefix to components manually", () =>
672698
backgroundColor: 'blue',
673699
},
674700
},
675-
})
701+
{ respectPrefix: false }
702+
)
676703
},
677704
],
678705
options: {
@@ -792,7 +819,8 @@ test('prefix will prefix all classes in a selector', () => {
792819
backgroundColor: 'blue',
793820
},
794821
},
795-
})
822+
{ respectPrefix: false }
823+
)
796824
},
797825
],
798826
options: {

src/util/processPlugins.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,18 @@ export default function(config) {
4646

4747
pluginUtilities.push(wrapWithVariants(styles.nodes, options.variants))
4848
},
49-
addComponents: components => {
50-
pluginComponents.push(...parseStyles(components))
49+
addComponents: (components, options) => {
50+
options = Object.assign({ respectPrefix: true }, options)
51+
52+
const styles = postcss.root({ nodes: parseStyles(components) })
53+
54+
styles.walkRules(rule => {
55+
if (options.respectPrefix) {
56+
rule.selector = prefixSelector(config.options.prefix, rule.selector)
57+
}
58+
})
59+
60+
pluginComponents.push(...styles.nodes)
5161
},
5262
})
5363
})

0 commit comments

Comments
 (0)