|
| 1 | +import type { Config } from 'tailwindcss' |
| 2 | +import { walk, type AstNode } from '../../../../tailwindcss/src/ast' |
| 3 | +import type { Variant } from '../../../../tailwindcss/src/candidate' |
| 4 | +import type { DesignSystem } from '../../../../tailwindcss/src/design-system' |
| 5 | +import { printCandidate } from '../candidates' |
| 6 | + |
| 7 | +export function variantOrder( |
| 8 | + designSystem: DesignSystem, |
| 9 | + _userConfig: Config, |
| 10 | + rawCandidate: string, |
| 11 | +): string { |
| 12 | + for (let candidate of designSystem.parseCandidate(rawCandidate)) { |
| 13 | + if (candidate.variants.length <= 1) { |
| 14 | + continue |
| 15 | + } |
| 16 | + |
| 17 | + let atRuleVariants = [] |
| 18 | + let regularVariants = [] |
| 19 | + let pseudoElementVariants = [] |
| 20 | + |
| 21 | + let originalOrder = candidate.variants |
| 22 | + |
| 23 | + for (let variant of candidate.variants) { |
| 24 | + if (isAtRuleVariant(designSystem, variant)) { |
| 25 | + atRuleVariants.push(variant) |
| 26 | + } else if (isEndOfSelectorPseudoElement(designSystem, variant)) { |
| 27 | + pseudoElementVariants.push(variant) |
| 28 | + } else { |
| 29 | + regularVariants.push(variant) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // We only need to reorder regular variants if order is important |
| 34 | + let regularVariantsNeedReordering = regularVariants.some((v) => |
| 35 | + isCombinatorVariant(designSystem, v), |
| 36 | + ) |
| 37 | + |
| 38 | + // The candidate list in the AST need to be in reverse order |
| 39 | + let newOrder = [ |
| 40 | + ...pseudoElementVariants, |
| 41 | + ...(regularVariantsNeedReordering ? regularVariants.reverse() : regularVariants), |
| 42 | + ...atRuleVariants, |
| 43 | + ] |
| 44 | + |
| 45 | + if (orderMatches(originalOrder, newOrder)) { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + return printCandidate(designSystem, { ...candidate, variants: newOrder }) |
| 50 | + } |
| 51 | + return rawCandidate |
| 52 | +} |
| 53 | + |
| 54 | +function isAtRuleVariant(designSystem: DesignSystem, variant: Variant) { |
| 55 | + // Handle the dark variant as an at-rule variant |
| 56 | + if (variant.kind === 'static' && variant.root === 'dark') { |
| 57 | + return true |
| 58 | + } |
| 59 | + let stack = getAppliedNodeStack(designSystem, variant) |
| 60 | + return stack.every((node) => node.kind === 'rule' && node.selector[0] === '@') |
| 61 | +} |
| 62 | + |
| 63 | +function isCombinatorVariant(designSystem: DesignSystem, variant: Variant) { |
| 64 | + let stack = getAppliedNodeStack(designSystem, variant) |
| 65 | + return stack.some( |
| 66 | + (node) => |
| 67 | + node.kind === 'rule' && |
| 68 | + // Ignore at-rules as they are hoisted |
| 69 | + node.selector[0] !== '@' && |
| 70 | + // Combinators include any of the following characters |
| 71 | + (node.selector.includes(' ') || |
| 72 | + node.selector.includes('>') || |
| 73 | + node.selector.includes('+') || |
| 74 | + node.selector.includes('~')), |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +function isEndOfSelectorPseudoElement(designSystem: DesignSystem, variant: Variant) { |
| 79 | + let stack = getAppliedNodeStack(designSystem, variant) |
| 80 | + return stack.some( |
| 81 | + (node) => |
| 82 | + node.kind === 'rule' && |
| 83 | + (node.selector.includes('::after') || |
| 84 | + node.selector.includes('::backdrop') || |
| 85 | + node.selector.includes('::before') || |
| 86 | + node.selector.includes('::first-letter') || |
| 87 | + node.selector.includes('::first-line') || |
| 88 | + node.selector.includes('::marker') || |
| 89 | + node.selector.includes('::placeholder') || |
| 90 | + node.selector.includes('::selection')), |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +function getAppliedNodeStack(designSystem: DesignSystem, variant: Variant): AstNode[] { |
| 95 | + let stack: AstNode[] = [] |
| 96 | + let ast = designSystem |
| 97 | + .compileAstNodes({ |
| 98 | + kind: 'arbitrary', |
| 99 | + property: 'color', |
| 100 | + value: 'red', |
| 101 | + modifier: null, |
| 102 | + variants: [variant], |
| 103 | + important: false, |
| 104 | + raw: 'candidate', |
| 105 | + }) |
| 106 | + .map((c) => c.node) |
| 107 | + |
| 108 | + walk(ast, (node) => { |
| 109 | + // Ignore the variant root class |
| 110 | + if (node.kind === 'rule' && node.selector === '.candidate') { |
| 111 | + return |
| 112 | + } |
| 113 | + // Ignore the dummy declaration |
| 114 | + if (node.kind === 'declaration' && node.property === 'color' && node.value === 'red') { |
| 115 | + return |
| 116 | + } |
| 117 | + stack.push(node) |
| 118 | + }) |
| 119 | + return stack |
| 120 | +} |
| 121 | + |
| 122 | +function orderMatches<T>(a: T[], b: T[]): boolean { |
| 123 | + if (a.length !== b.length) { |
| 124 | + return false |
| 125 | + } |
| 126 | + return a.every((v, i) => b[i] === v) |
| 127 | +} |
0 commit comments