@@ -16,7 +16,6 @@ import type { Theme } from './theme'
1616import { compareBreakpoints } from './utils/compare-breakpoints'
1717import { DefaultMap } from './utils/default-map'
1818import { isPositiveInteger } from './utils/infer-data-type'
19- import { isAlpha } from './utils/is-alpha'
2019import { segment } from './utils/segment'
2120
2221type VariantFn < T extends Variant [ 'kind' ] > = (
@@ -712,16 +711,33 @@ export function createVariants(theme: Theme): Variants {
712711
713712 staticVariant ( 'inert' , [ '&:is([inert], [inert] *)' ] )
714713
715- variants . functional ( 'in' , ( ruleNode , variant ) => {
716- if ( ! variant . value || variant . modifier ) return null
714+ variants . compound ( 'in' , Compounds . StyleRules , ( ruleNode , variant ) => {
715+ if ( variant . modifier ) return null
717716
718- // Named values should be alpha (tag selector). This prevents `in-foo-bar`
719- // from being used as a variant.
720- if ( variant . value . kind === 'named' && ! isAlpha ( variant . value . value ) ) {
721- return null
722- }
717+ let didApply = false
718+
719+ walk ( [ ruleNode ] , ( node , { path } ) => {
720+ if ( node . kind !== 'rule' ) return WalkAction . Continue
721+
722+ // Throw out any candidates with variants using nested style rules
723+ for ( let parent of path . slice ( 0 , - 1 ) ) {
724+ if ( parent . kind !== 'rule' ) continue
723725
724- ruleNode . nodes = [ styleRule ( `:where(${ variant . value . value } ) &` , ruleNode . nodes ) ]
726+ didApply = false
727+ return WalkAction . Stop
728+ }
729+
730+ // Replace `&` in target variant with `*`, so variants like `&:hover`
731+ // become `:where(*:hover) &`. The `*` will often be optimized away.
732+ node . selector = `:where(${ node . selector . replaceAll ( '&' , '*' ) } ) &`
733+
734+ // Track that the variant was actually applied
735+ didApply = true
736+ } )
737+
738+ // If the node wasn't modified, this variant is not compatible with
739+ // `in-*` so discard the candidate.
740+ if ( ! didApply ) return null
725741 } )
726742
727743 variants . compound ( 'has' , Compounds . StyleRules , ( ruleNode , variant ) => {
0 commit comments