Skip to content

Commit 9999d2f

Browse files
authored
Fix selector when using a non-default class (#289)
1 parent 743b682 commit 9999d2f

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

src/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ const computed = {
99
// bulletColor: (color) => ({ 'ul > li::before': { backgroundColor: color } }),
1010
}
1111

12-
function inWhere(selector, { className, prefix }) {
12+
function inWhere(selector, { className, modifier, prefix }) {
1313
let prefixedNot = prefix(`.not-${className}`).slice(1)
14-
let selectorPrefix = selector.startsWith('>') ? `.${className} ` : ''
14+
let selectorPrefix = selector.startsWith('>')
15+
? `${modifier === 'DEFAULT' ? `.${className}` : `.${className}-${modifier}`} `
16+
: ''
1517

1618
// Parse the selector, if every component ends in the same pseudo element(s) then move it to the end
1719
let [trailingPseudo, rebuiltSelector] = commonTrailingPseudos(selector)
@@ -27,7 +29,7 @@ function isObject(value) {
2729
return typeof value === 'object' && value !== null
2830
}
2931

30-
function configToCss(config = {}, { target, className, prefix }) {
32+
function configToCss(config = {}, { target, className, modifier, prefix }) {
3133
function updateSelector(k, v) {
3234
if (target === 'legacy') {
3335
return [k, v]
@@ -41,13 +43,13 @@ function configToCss(config = {}, { target, className, prefix }) {
4143
let nested = Object.values(v).some(isObject)
4244
if (nested) {
4345
return [
44-
inWhere(k, { className, prefix }),
46+
inWhere(k, { className, modifier, prefix }),
4547
v,
4648
Object.fromEntries(Object.entries(v).map(([k, v]) => updateSelector(k, v))),
4749
]
4850
}
4951

50-
return [inWhere(k, { className, prefix }), v]
52+
return [inWhere(k, { className, modifier, prefix }), v]
5153
}
5254

5355
return [k, v]
@@ -121,6 +123,7 @@ module.exports = plugin.withOptions(
121123
{
122124
target,
123125
className,
126+
modifier,
124127
prefix,
125128
}
126129
),

src/index.test.js

+89
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,89 @@ test('specificity is reduced with :where', async () => {
181181
})
182182
})
183183

184+
test('variants', async () => {
185+
let config = {
186+
content: [{ raw: html`<div class="sm:prose hover:prose-lg lg:prose-lg"></div>` }],
187+
theme: {
188+
typography: {
189+
DEFAULT: {
190+
css: [
191+
{
192+
color: 'red',
193+
p: {
194+
color: 'lime',
195+
},
196+
'> ul > li': {
197+
color: 'purple',
198+
},
199+
},
200+
],
201+
},
202+
lg: {
203+
css: {
204+
color: 'green',
205+
p: {
206+
color: 'tomato',
207+
},
208+
'> ul > li': {
209+
color: 'blue',
210+
},
211+
},
212+
},
213+
xl: {
214+
css: {
215+
color: 'yellow',
216+
'> ul > li': {
217+
color: 'hotpink',
218+
},
219+
},
220+
},
221+
},
222+
},
223+
}
224+
225+
return run(config).then((result) => {
226+
expect(result.css).toMatchFormattedCss(
227+
css`
228+
${defaults}
229+
230+
.hover\:prose-lg:hover {
231+
color: green;
232+
}
233+
.hover\:prose-lg:hover :where(p):not(:where([class~='not-prose'] *)) {
234+
color: tomato;
235+
}
236+
.hover\:prose-lg:hover
237+
:where(.hover\:prose-lg:hover > ul > li):not(:where([class~='not-prose'] *)) {
238+
color: blue;
239+
}
240+
@media (min-width: 640px) {
241+
.sm\:prose {
242+
color: red;
243+
}
244+
.sm\:prose :where(p):not(:where([class~='not-prose'] *)) {
245+
color: lime;
246+
}
247+
.sm\:prose :where(.sm\:prose > ul > li):not(:where([class~='not-prose'] *)) {
248+
color: purple;
249+
}
250+
}
251+
@media (min-width: 1024px) {
252+
.lg\:prose-lg {
253+
color: green;
254+
}
255+
.lg\:prose-lg :where(p):not(:where([class~='not-prose'] *)) {
256+
color: tomato;
257+
}
258+
.lg\:prose-lg :where(.lg\:prose-lg > ul > li):not(:where([class~='not-prose'] *)) {
259+
color: blue;
260+
}
261+
}
262+
`
263+
)
264+
})
265+
})
266+
184267
test('modifiers', async () => {
185268
let config = {
186269
content: [{ raw: html`<div class="prose prose-lg"></div>` }],
@@ -242,6 +325,9 @@ test('modifiers', async () => {
242325
marginTop: '40px',
243326
marginBottom: '40px',
244327
},
328+
'> ul > li': {
329+
paddingLeft: '12px',
330+
},
245331
h1: {
246332
fontSize: '48px',
247333
marginTop: '0',
@@ -320,6 +406,9 @@ test('modifiers', async () => {
320406
margin-top: 40px;
321407
margin-bottom: 40px;
322408
}
409+
.prose-lg :where(.prose-lg > ul > li):not(:where([class~='not-prose'] *)) {
410+
padding-left: 12px;
411+
}
323412
.prose-lg :where(h1):not(:where([class~='not-prose'] *)) {
324413
font-size: 48px;
325414
margin-top: 0;

0 commit comments

Comments
 (0)