Skip to content

Commit 8678309

Browse files
adamwathanreinink
andauthored
Use :where to reduce specificity (#203)
* Use :where to reduce specificity * Update index.js * Handle before/after with :where This is a fairly naive solution and could/should be made more robust to handle other pseudo-elements like ::first-letter, ::first-line, etc. but I'm inclined to just roll with this until someone actually opens an issue because they needed support for those. * Only wrap selectors with :where(), not property names * Add new `target` plugin option to enable/disable the use of `:where()` selectors Co-Authored-By: Adam Wathan <[email protected]> Co-authored-by: Jonathan Reinink <[email protected]>
1 parent 9bf60fd commit 8678309

File tree

2 files changed

+15354
-7671
lines changed

2 files changed

+15354
-7671
lines changed

Diff for: src/index.js

+36-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,44 @@ const computed = {
1010
// bulletColor: (color) => ({ 'ul > li::before': { backgroundColor: color } }),
1111
}
1212

13-
function configToCss(config = {}) {
14-
return merge(
15-
{},
16-
...Object.keys(config)
17-
.filter((key) => computed[key])
18-
.map((key) => computed[key](config[key])),
19-
...castArray(config.css || {})
13+
function inWhere(selector) {
14+
if (selector.endsWith('::before')) {
15+
return `:where(${selector.slice(0, -8)})::before`
16+
}
17+
18+
if (selector.endsWith('::after')) {
19+
return `:where(${selector.slice(0, -7)})::after`
20+
}
21+
22+
return `:where(${selector})`
23+
}
24+
25+
function configToCss(config = {}, target) {
26+
return Object.fromEntries(
27+
Object.entries(
28+
merge(
29+
{},
30+
...Object.keys(config)
31+
.filter((key) => computed[key])
32+
.map((key) => computed[key](config[key])),
33+
...castArray(config.css || {})
34+
)
35+
).map(([k, v]) => {
36+
if (target === 'legacy') {
37+
return [k, v]
38+
}
39+
40+
if (typeof v == 'object' && v.constructor == Object) {
41+
return [inWhere(k), v]
42+
}
43+
44+
return [k, v]
45+
})
2046
)
2147
}
2248

2349
module.exports = plugin.withOptions(
24-
({ modifiers, className = 'prose' } = {}) => {
50+
({ modifiers, className = 'prose', target = 'modern' } = {}) => {
2551
return function ({ addComponents, theme, variants }) {
2652
const DEFAULT_MODIFIERS = [
2753
'DEFAULT',
@@ -47,7 +73,8 @@ module.exports = plugin.withOptions(
4773
addComponents(
4874
all.map((modifier) => ({
4975
[modifier === 'DEFAULT' ? `.${className}` : `.${className}-${modifier}`]: configToCss(
50-
config[modifier]
76+
config[modifier],
77+
target,
5178
),
5279
})),
5380
variants('typography')

0 commit comments

Comments
 (0)