Home / Class/ Variants Class — tailwindcss Architecture

Variants Class — tailwindcss Architecture

Architecture documentation for the Variants class in variants.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa["Variants"]
  db9eae47_695c_23c3_68bd_bae5bb363efe["variants.ts"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|defined in| db9eae47_695c_23c3_68bd_bae5bb363efe
  774af68b_6fbe_668c_5fdf_6bbcef9f7ab6["static()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 774af68b_6fbe_668c_5fdf_6bbcef9f7ab6
  34338b14_fec6_6308_0fd7_15af8d4da01b["fromAst()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 34338b14_fec6_6308_0fd7_15af8d4da01b
  0b45bc8b_6be0_3c76_dfd2_c6a379f6b1ec["functional()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 0b45bc8b_6be0_3c76_dfd2_c6a379f6b1ec
  47e70eea_1a5b_2b9b_d1b3_e2b1c34eec79["compound()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 47e70eea_1a5b_2b9b_d1b3_e2b1c34eec79
  687e64a5_22c0_2f4f_117f_d39492284510["group()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 687e64a5_22c0_2f4f_117f_d39492284510
  92aee128_92e0_8faf_f51a_80a217b47f7f["has()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 92aee128_92e0_8faf_f51a_80a217b47f7f
  9b8bd17a_1b05_f86d_02dc_1f5d07860201["get()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 9b8bd17a_1b05_f86d_02dc_1f5d07860201
  6d6d5efc_47a1_d4d5_abb7_5fed71effa04["kind()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 6d6d5efc_47a1_d4d5_abb7_5fed71effa04
  b1efac17_4504_4725_555d_d07e8cf1a9ea["compoundsWith()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| b1efac17_4504_4725_555d_d07e8cf1a9ea
  c67f3732_4d34_491a_b073_a44a28c0af88["suggest()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| c67f3732_4d34_491a_b073_a44a28c0af88
  2f38bdb9_4da7_87d4_1625_a8124b9a1417["getCompletions()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 2f38bdb9_4da7_87d4_1625_a8124b9a1417
  7ab199f0_ea5e_0bae_2efa_8b253a0f26bd["compare()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 7ab199f0_ea5e_0bae_2efa_8b253a0f26bd
  528e3869_06a3_92d8_aeac_662764557707["keys()"]
  ff716ee9_bd1e_1568_4100_c1d5b3ab8daa -->|method| 528e3869_06a3_92d8_aeac_662764557707

Relationship Graph

Source Code

packages/tailwindcss/src/variants.ts lines 39–317

export class Variants {
  public compareFns = new Map<number, CompareFn>()
  public variants = new Map<
    string,
    {
      kind: Variant['kind']
      order: number
      applyFn: VariantFn<any>

      // The kind of rules that are allowed in this compound variant
      compoundsWith: Compounds

      // The kind of rules that are generated by this variant
      // Determines whether or not a compound variant can use this variant
      compounds: Compounds
    }
  >()

  private completions = new Map<string, () => string[]>()

  /**
   * Registering a group of variants should result in the same sort number for
   * all the variants. This is to ensure that the variants are applied in the
   * correct order.
   */
  private groupOrder: null | number = null

  /**
   * Keep track of the last sort order instead of using the size of the map to
   * avoid unnecessarily skipping order numbers.
   */
  private lastOrder = 0

  static(
    name: string,
    applyFn: VariantFn<'static'>,
    { compounds, order }: { compounds?: Compounds; order?: number } = {},
  ) {
    this.set(name, {
      kind: 'static',
      applyFn,
      compoundsWith: Compounds.Never,
      compounds: compounds ?? Compounds.StyleRules,
      order,
    })
  }

  fromAst(name: string, ast: AstNode[], designSystem: DesignSystem) {
    let selectors: string[] = []

    let usesAtVariant = false
    walk(ast, (node) => {
      if (node.kind === 'rule') {
        selectors.push(node.selector)
      } else if (node.kind === 'at-rule' && node.name === '@variant') {
        usesAtVariant = true
      } else if (node.kind === 'at-rule' && node.name !== '@slot') {
        selectors.push(`${node.name} ${node.params}`)
      }
    })

    this.static(
      name,
      (r) => {
        let body = ast.map(cloneAstNode)
        if (usesAtVariant) substituteAtVariant(body, designSystem)
        substituteAtSlot(body, r.nodes)
        r.nodes = body
      },
      { compounds: compoundsForSelectors(selectors) },
    )
  }

  functional(
    name: string,
    applyFn: VariantFn<'functional'>,
    { compounds, order }: { compounds?: Compounds; order?: number } = {},
  ) {
    this.set(name, {
      kind: 'functional',
      applyFn,

Domain

Frequently Asked Questions

What is the Variants class?
Variants is a class in the tailwindcss codebase, defined in packages/tailwindcss/src/variants.ts.
Where is Variants defined?
Variants is defined in packages/tailwindcss/src/variants.ts at line 39.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free