1+ import { Features } from '.'
12import {
23 WalkAction ,
34 atRoot ,
@@ -12,6 +13,8 @@ import {
1213 type StyleRule ,
1314} from './ast'
1415import { type Variant } from './candidate'
16+ import { applyVariant } from './compile'
17+ import type { DesignSystem } from './design-system'
1518import type { Theme } from './theme'
1619import { compareBreakpoints } from './utils/compare-breakpoints'
1720import { DefaultMap } from './utils/default-map'
@@ -1198,3 +1201,30 @@ export function substituteAtSlot(ast: AstNode[], nodes: AstNode[]) {
11981201 }
11991202 } )
12001203}
1204+
1205+ export function substituteAtVariant ( ast : AstNode [ ] , designSystem : DesignSystem ) : Features {
1206+ let features = Features . None
1207+ walk ( ast , ( variantNode , { replaceWith } ) => {
1208+ if ( variantNode . kind !== 'at-rule' || variantNode . name !== '@variant' ) return
1209+
1210+ // Starting with the `&` rule node
1211+ let node = styleRule ( '&' , variantNode . nodes )
1212+
1213+ let variant = variantNode . params
1214+
1215+ let variantAst = designSystem . parseVariant ( variant )
1216+ if ( variantAst === null ) {
1217+ throw new Error ( `Cannot use \`@variant\` with unknown variant: ${ variant } ` )
1218+ }
1219+
1220+ let result = applyVariant ( node , variantAst , designSystem . variants )
1221+ if ( result === null ) {
1222+ throw new Error ( `Cannot use \`@variant\` with variant: ${ variant } ` )
1223+ }
1224+
1225+ // Update the variant at-rule node, to be the `&` rule node
1226+ replaceWith ( node )
1227+ features |= Features . Variants
1228+ } )
1229+ return features
1230+ }
0 commit comments