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 OxideEngine Extractor calls 21 called by 1

Entity Profile

Dependency Diagram

graph TD
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66["createVariants()"]
  db9eae47_695c_23c3_68bd_bae5bb363efe["variants.ts"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|defined in| db9eae47_695c_23c3_68bd_bae5bb363efe
  9b965fd7_d8e9_0b43_cd5d_c9294ab598ed["buildDesignSystem()"]
  9b965fd7_d8e9_0b43_cd5d_c9294ab598ed -->|calls| c1a769fc_95ca_ceea_62f9_aedeeedf7c66
  759ab8d0_0e80_5789_e945_a5f814874e59["compoundsForSelectors()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 759ab8d0_0e80_5789_e945_a5f814874e59
  2d30c5fc_1830_2311_ea0e_e71c801b6c71["rule()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 2d30c5fc_1830_2311_ea0e_e71c801b6c71
  f712ed47_45d4_4e5a_dd73_fdefa1da71da["segment()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| f712ed47_45d4_4e5a_dd73_fdefa1da71da
  f9b19679_c1f0_28d6_4d1a_31a10c52e42d["atRule()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| f9b19679_c1f0_28d6_4d1a_31a10c52e42d
  47e70eea_1a5b_2b9b_d1b3_e2b1c34eec79["compound()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 47e70eea_1a5b_2b9b_d1b3_e2b1c34eec79
  ed78da58_8727_ad98_120c_61f35cea357a["walk()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| ed78da58_8727_ad98_120c_61f35cea357a
  36be1773_d660_31ac_0b0b_88dbd7f6f7a8["styleRule()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 36be1773_d660_31ac_0b0b_88dbd7f6f7a8
  c67f3732_4d34_491a_b073_a44a28c0af88["suggest()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| c67f3732_4d34_491a_b073_a44a28c0af88
  528e3869_06a3_92d8_aeac_662764557707["keys()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 528e3869_06a3_92d8_aeac_662764557707
  b1efac17_4504_4725_555d_d07e8cf1a9ea["compoundsWith()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| b1efac17_4504_4725_555d_d07e8cf1a9ea
  04953c5c_51e1_25ee_0e61_539631b1aaa9["atRoot()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| 04953c5c_51e1_25ee_0e61_539631b1aaa9
  c203f636_607a_d332_b4c5_6a40c108f778["decl()"]
  c1a769fc_95ca_ceea_62f9_aedeeedf7c66 -->|calls| c203f636_607a_d332_b4c5_6a40c108f778
  style c1a769fc_95ca_ceea_62f9_aedeeedf7c66 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