Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preset-mini): pseudo-class function for custom arguments #2694

Merged
merged 11 commits into from Jun 4, 2023
2 changes: 2 additions & 0 deletions packages/preset-wind/src/variants/default.ts
Expand Up @@ -7,6 +7,7 @@ import { variantStickyHover } from './experimental'
import { variantContrasts, variantMotions, variantOrientations } from './media'
import { variantSpaceAndDivide } from './misc'
import { placeholderModifier } from './placeholder'
import { variantHasPseudo } from './has'

export function variants(options: PresetWindOptions): Variant<Theme>[] {
return [
Expand All @@ -19,5 +20,6 @@ export function variants(options: PresetWindOptions): Variant<Theme>[] {
...variantCombinators,
...variantColorsScheme,
...variantStickyHover,
...variantHasPseudo,
]
}
74 changes: 74 additions & 0 deletions packages/preset-wind/src/variants/has.ts
@@ -0,0 +1,74 @@
import type { Variant } from '@unocss/core'
import { h, variantGetBracket } from '@unocss/preset-mini/utils'

export const variantHasPseudo: Variant[] = [
{
name: 'has',
match(input, ctx) {
const body = variantGetBracket('has-', input, ctx.generator.config.separators)
if (!body)
return

const [match, reset] = body
const bracketValue = h.bracket(match)
if (!bracketValue)
return

if (match) {
return {
matcher: reset,
handle: (input, next) => next({
...input,
selector: `${input.selector || ''}:has(${bracketValue})`,
}),
}
}
},
},
{
name: 'group-has',
match(input, ctx) {
const body = variantGetBracket('group-has-', input, ctx.generator.config.separators)
if (!body)
return

const [match, reset] = body
const bracketValue = h.bracket(match)
if (!bracketValue)
return

if (match) {
return {
matcher: reset,
handle: (input, next) => next({
...input,
selector: `.group:has(${bracketValue}) ${input.selector || ''}`,
Copy link
Member

Choose a reason for hiding this comment

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

We have an option to toggle .group style:

taggedPseudoClassMatcher('group', attributify ? '[group=""]' : '.group', ' '),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have done this and moved to the preset-mini package, had to add some code because the previous implementation might work not perfectly with custom arguments (and I'm kinda confused with it).

And implement both four pseudo-class functions with it (so change the pr title

}),
}
}
},
},
{
name: 'peer-has',
match(input, ctx) {
const body = variantGetBracket('peer-has-', input, ctx.generator.config.separators)
if (!body)
return

const [match, reset] = body
const bracketValue = h.bracket(match)
if (!bracketValue)
return

if (match) {
return {
matcher: reset,
handle: (input, next) => next({
...input,
selector: `.peer:has(${bracketValue}) ~ ${input.selector || ''}`,
Copy link
Member

Choose a reason for hiding this comment

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

Same for .peer

}),
}
}
},
},
]
3 changes: 3 additions & 0 deletions test/assets/output/preset-wind-targets.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/assets/preset-wind-targets.ts
Expand Up @@ -403,6 +403,11 @@ export const presetWindTargets: string[] = [
'@hover-text-red',
'@hover:[[open]_&]:text-blue',

// variants :has pseudo-class
'has-[[data-potato]]:text-blue',
'group-has-[[data-potato]]:text-white',
'peer-has-[[data-potato]]:text-black',

// variants - multiple parents
'@dark:contrast-more:p-10',

Expand Down