Skip to content
Prev Previous commit
compute variant ahead of time
  • Loading branch information
RobinMalfait committed Nov 9, 2024
commit 642fa95178ab6b86367c3ab81e4038d508657bce
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export function modernizeArbitraryValues(
// Expecting a single selector node
if (ast.nodes.length !== 1) continue

// Track whether we need to add a `*:` variant
let addStarVariant = false
let prefixedVariant: Variant | null = null

// Track whether we need to add a `**:` variant
let addStarStarVariant = false
Expand All @@ -46,7 +45,7 @@ export function modernizeArbitraryValues(
ast.nodes[0].nodes[2].type === 'attribute'
) {
ast.nodes[0].nodes = [ast.nodes[0].nodes[2]]
addStarVariant = true
prefixedVariant = designSystem.parseVariant('*')
}

// Handling a grand child combinator. E.g.: `[&_[data-visible]]` => `**:data-visible`
Expand All @@ -63,7 +62,7 @@ export function modernizeArbitraryValues(
ast.nodes[0].nodes[2].type === 'attribute'
) {
ast.nodes[0].nodes = [ast.nodes[0].nodes[2]]
addStarStarVariant = true
prefixedVariant = designSystem.parseVariant('**')
}

// Filter out `&`. E.g.: `&[data-foo]` => `[data-foo]`
Expand Down Expand Up @@ -252,20 +251,12 @@ export function modernizeArbitraryValues(
}
}

if (addStarVariant) {
if (prefixedVariant) {
let idx = clone.variants.indexOf(variant)
if (idx === -1) continue

// Ensure we have the `*:` variant
clone.variants.splice(idx, 1, variant, { kind: 'static', root: '*' })
}

if (addStarStarVariant) {
let idx = clone.variants.indexOf(variant)
if (idx === -1) continue

// Ensure we have the `**:` variant
clone.variants.splice(idx, 1, variant, { kind: 'static', root: '**' })
// Ensure we inject the prefixed variant
clone.variants.splice(idx, 1, variant, prefixedVariant)
}
}

Expand Down