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
  67c140cd_3928_7c30_7f6d_c67052da2a02["Variants"]
  b638ddb2_c057_1f3c_8a1a_4993ad80cd58["variants.ts"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|defined in| b638ddb2_c057_1f3c_8a1a_4993ad80cd58
  303a143d_958a_fac9_a18f_3ccf6d97d00a["static()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 303a143d_958a_fac9_a18f_3ccf6d97d00a
  63907b21_884d_e864_81c9_68f20a13c40b["fromAst()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 63907b21_884d_e864_81c9_68f20a13c40b
  6cc289c0_7885_e683_1053_2adb14a9ae30["functional()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 6cc289c0_7885_e683_1053_2adb14a9ae30
  1414bfb2_d251_dd0b_535b_ae5531c73cdb["compound()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 1414bfb2_d251_dd0b_535b_ae5531c73cdb
  15fe2878_a9ae_f5b5_2f4c_e3238b9272fe["group()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 15fe2878_a9ae_f5b5_2f4c_e3238b9272fe
  0b77235f_7858_8f45_ccef_3d69a97a50f0["has()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 0b77235f_7858_8f45_ccef_3d69a97a50f0
  553e63f4_f172_3689_a5a7_f8cf3b0bd124["get()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 553e63f4_f172_3689_a5a7_f8cf3b0bd124
  b540d2b5_4670_b95e_6e75_e5173757e257["kind()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| b540d2b5_4670_b95e_6e75_e5173757e257
  f5fdf8c5_9ef2_b78b_1247_5ede58487336["compoundsWith()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| f5fdf8c5_9ef2_b78b_1247_5ede58487336
  63f70e1f_5643_1fef_598f_bc750c0d8c88["suggest()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 63f70e1f_5643_1fef_598f_bc750c0d8c88
  f5dd6baa_c080_557a_f04f_aeb7ca491807["getCompletions()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| f5dd6baa_c080_557a_f04f_aeb7ca491807
  27d1b84b_20da_caf1_9cd4_42d8b55bdaa0["compare()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| 27d1b84b_20da_caf1_9cd4_42d8b55bdaa0
  dcf12967_c6e5_9f98_abbc_968688c62cee["keys()"]
  67c140cd_3928_7c30_7f6d_c67052da2a02 -->|method| dcf12967_c6e5_9f98_abbc_968688c62cee

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