Skip to content

Commit

Permalink
Fix selector when using a non-default class (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed Nov 7, 2022
1 parent 743b682 commit 9999d2f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/index.js
Expand Up @@ -9,9 +9,11 @@ const computed = {
// bulletColor: (color) => ({ 'ul > li::before': { backgroundColor: color } }),
}

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

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

function configToCss(config = {}, { target, className, prefix }) {
function configToCss(config = {}, { target, className, modifier, prefix }) {
function updateSelector(k, v) {
if (target === 'legacy') {
return [k, v]
Expand All @@ -41,13 +43,13 @@ function configToCss(config = {}, { target, className, prefix }) {
let nested = Object.values(v).some(isObject)
if (nested) {
return [
inWhere(k, { className, prefix }),
inWhere(k, { className, modifier, prefix }),
v,
Object.fromEntries(Object.entries(v).map(([k, v]) => updateSelector(k, v))),
]
}

return [inWhere(k, { className, prefix }), v]
return [inWhere(k, { className, modifier, prefix }), v]
}

return [k, v]
Expand Down Expand Up @@ -121,6 +123,7 @@ module.exports = plugin.withOptions(
{
target,
className,
modifier,
prefix,
}
),
Expand Down
89 changes: 89 additions & 0 deletions src/index.test.js
Expand Up @@ -181,6 +181,89 @@ test('specificity is reduced with :where', async () => {
})
})

test('variants', async () => {
let config = {
content: [{ raw: html`<div class="sm:prose hover:prose-lg lg:prose-lg"></div>` }],
theme: {
typography: {
DEFAULT: {
css: [
{
color: 'red',
p: {
color: 'lime',
},
'> ul > li': {
color: 'purple',
},
},
],
},
lg: {
css: {
color: 'green',
p: {
color: 'tomato',
},
'> ul > li': {
color: 'blue',
},
},
},
xl: {
css: {
color: 'yellow',
'> ul > li': {
color: 'hotpink',
},
},
},
},
},
}

return run(config).then((result) => {
expect(result.css).toMatchFormattedCss(
css`
${defaults}
.hover\:prose-lg:hover {
color: green;
}
.hover\:prose-lg:hover :where(p):not(:where([class~='not-prose'] *)) {
color: tomato;
}
.hover\:prose-lg:hover
:where(.hover\:prose-lg:hover > ul > li):not(:where([class~='not-prose'] *)) {
color: blue;
}
@media (min-width: 640px) {
.sm\:prose {
color: red;
}
.sm\:prose :where(p):not(:where([class~='not-prose'] *)) {
color: lime;
}
.sm\:prose :where(.sm\:prose > ul > li):not(:where([class~='not-prose'] *)) {
color: purple;
}
}
@media (min-width: 1024px) {
.lg\:prose-lg {
color: green;
}
.lg\:prose-lg :where(p):not(:where([class~='not-prose'] *)) {
color: tomato;
}
.lg\:prose-lg :where(.lg\:prose-lg > ul > li):not(:where([class~='not-prose'] *)) {
color: blue;
}
}
`
)
})
})

test('modifiers', async () => {
let config = {
content: [{ raw: html`<div class="prose prose-lg"></div>` }],
Expand Down Expand Up @@ -242,6 +325,9 @@ test('modifiers', async () => {
marginTop: '40px',
marginBottom: '40px',
},
'> ul > li': {
paddingLeft: '12px',
},
h1: {
fontSize: '48px',
marginTop: '0',
Expand Down Expand Up @@ -320,6 +406,9 @@ test('modifiers', async () => {
margin-top: 40px;
margin-bottom: 40px;
}
.prose-lg :where(.prose-lg > ul > li):not(:where([class~='not-prose'] *)) {
padding-left: 12px;
}
.prose-lg :where(h1):not(:where([class~='not-prose'] *)) {
font-size: 48px;
margin-top: 0;
Expand Down

1 comment on commit 9999d2f

@vercel
Copy link

@vercel vercel bot commented on 9999d2f Nov 7, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.