Home / Function/ createVariants() — tailwindcss Function Reference

createVariants() — tailwindcss Function Reference

Architecture documentation for the createVariants() function in variants.ts from the tailwindcss codebase.

Function typescript Oxide Extractor calls 21 called by 1

Entity Profile

Dependency Diagram

graph TD
  876035d0_94a8_cf2f_314a_d520aaae6e4b["createVariants()"]
  b638ddb2_c057_1f3c_8a1a_4993ad80cd58["variants.ts"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|defined in| b638ddb2_c057_1f3c_8a1a_4993ad80cd58
  e557c8a4_bb27_ee44_c462_9e238157ad04["buildDesignSystem()"]
  e557c8a4_bb27_ee44_c462_9e238157ad04 -->|calls| 876035d0_94a8_cf2f_314a_d520aaae6e4b
  ed286c50_134b_5b5b_ff58_76456160010c["compoundsForSelectors()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| ed286c50_134b_5b5b_ff58_76456160010c
  2afd4bc8_8773_552f_8cfb_323403b7f9a3["rule()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 2afd4bc8_8773_552f_8cfb_323403b7f9a3
  c58cbb33_f3cc_0b4f_844a_15bf66a1dc61["segment()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| c58cbb33_f3cc_0b4f_844a_15bf66a1dc61
  2f6881be_62d9_4b96_7331_a962ced095f7["atRule()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 2f6881be_62d9_4b96_7331_a962ced095f7
  1414bfb2_d251_dd0b_535b_ae5531c73cdb["compound()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 1414bfb2_d251_dd0b_535b_ae5531c73cdb
  4982d9ce_98d4_85d9_44af_7cc47b93c482["walk()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 4982d9ce_98d4_85d9_44af_7cc47b93c482
  2a22052d_f868_4f88_0a03_2033be58172d["styleRule()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 2a22052d_f868_4f88_0a03_2033be58172d
  63f70e1f_5643_1fef_598f_bc750c0d8c88["suggest()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 63f70e1f_5643_1fef_598f_bc750c0d8c88
  dcf12967_c6e5_9f98_abbc_968688c62cee["keys()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| dcf12967_c6e5_9f98_abbc_968688c62cee
  f5fdf8c5_9ef2_b78b_1247_5ede58487336["compoundsWith()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| f5fdf8c5_9ef2_b78b_1247_5ede58487336
  d09294cc_b29f_8605_b873_6cdc178ea33d["atRoot()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| d09294cc_b29f_8605_b873_6cdc178ea33d
  1369a6dc_e395_347d_5d24_b88e22c5446d["decl()"]
  876035d0_94a8_cf2f_314a_d520aaae6e4b -->|calls| 1369a6dc_e395_347d_5d24_b88e22c5446d
  style 876035d0_94a8_cf2f_314a_d520aaae6e4b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/variants.ts lines 348–1163

export function createVariants(theme: Theme): Variants {
  // In the future we may want to support returning a rule here if some complex
  // variant requires it. For now pure mutation is sufficient and will be the
  // most performant.
  let variants = new Variants()

  /**
   * Register a static variant like `hover`.
   */
  function staticVariant(
    name: string,
    selectors: string[],
    { compounds }: { compounds?: Compounds } = {},
  ) {
    compounds = compounds ?? compoundsForSelectors(selectors)

    variants.static(
      name,
      (r) => {
        r.nodes = selectors.map((selector) => rule(selector, r.nodes))
      },
      { compounds },
    )
  }

  staticVariant('*', [':is(& > *)'], { compounds: Compounds.Never })
  staticVariant('**', [':is(& *)'], { compounds: Compounds.Never })

  function negateConditions(ruleName: string, conditions: string[]) {
    return conditions.map((condition) => {
      condition = condition.trim()

      let parts = segment(condition, ' ')

      // @media not {query}
      // @supports not {query}
      // @container not {query}
      if (parts[0] === 'not') {
        return parts.slice(1).join(' ')
      }

      if (ruleName === '@container') {
        // @container {query}
        if (parts[0][0] === '(') {
          return `not ${condition}`
        }

        // @container {name} not {query}
        else if (parts[1] === 'not') {
          return `${parts[0]} ${parts.slice(2).join(' ')}`
        }

        // @container {name} {query}
        else {
          return `${parts[0]} not ${parts.slice(1).join(' ')}`
        }
      }

      return `not ${condition}`
    })
  }

  let conditionalRules = ['@media', '@supports', '@container']

  function negateAtRule(rule: AtRule) {
    for (let ruleName of conditionalRules) {
      if (ruleName !== rule.name) continue

      let conditions = segment(rule.params, ',')

      // We don't support things like `@media screen, print` because
      // the negation would be `@media not screen and print` and we don't
      // want to deal with that complexity.
      if (conditions.length > 1) return null

      conditions = negateConditions(rule.name, conditions)
      return atRule(rule.name, conditions.join(', '))
    }

    return null
  }

Domain

Subdomains

Frequently Asked Questions

What does createVariants() do?
createVariants() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/variants.ts.
Where is createVariants() defined?
createVariants() is defined in packages/tailwindcss/src/variants.ts at line 348.
What does createVariants() call?
createVariants() calls 21 function(s): atRoot, atRule, compareBreakpoints, compound, compoundsForSelectors, compoundsWith, decl, functional, and 13 more.
What calls createVariants()?
createVariants() is called by 1 function(s): buildDesignSystem.

Analyze Your Own Codebase

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

Try Supermodel Free