Skip to content

Commit ad0a167

Browse files
committed
add substituteAtVariant utility
1 parent ce90b65 commit ad0a167

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

packages/tailwindcss/src/variants.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Features } from '.'
12
import {
23
WalkAction,
34
atRoot,
@@ -12,6 +13,8 @@ import {
1213
type StyleRule,
1314
} from './ast'
1415
import { type Variant } from './candidate'
16+
import { applyVariant } from './compile'
17+
import type { DesignSystem } from './design-system'
1518
import type { Theme } from './theme'
1619
import { compareBreakpoints } from './utils/compare-breakpoints'
1720
import { 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

Comments
 (0)