Home / Function/ applyVariant() — tailwindcss Function Reference

applyVariant() — tailwindcss Function Reference

Architecture documentation for the applyVariant() function in compile.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  8bb120ad_8027_cede_007f_1d5351f7ca96["applyVariant()"]
  3ea56425_6e9f_0dc3_e68f_304ee638d53d["compileAstNodes()"]
  3ea56425_6e9f_0dc3_e68f_304ee638d53d -->|calls| 8bb120ad_8027_cede_007f_1d5351f7ca96
  17785b1a_7f73_4e1a_312d_b1bf01e059c3["getVariants()"]
  17785b1a_7f73_4e1a_312d_b1bf01e059c3 -->|calls| 8bb120ad_8027_cede_007f_1d5351f7ca96
  716eb081_2da3_764c_1bd4_89abf211e018["substituteAtVariant()"]
  716eb081_2da3_764c_1bd4_89abf211e018 -->|calls| 8bb120ad_8027_cede_007f_1d5351f7ca96
  08f33202_11d1_569a_e8df_de23eb987e2f["rule()"]
  8bb120ad_8027_cede_007f_1d5351f7ca96 -->|calls| 08f33202_11d1_569a_e8df_de23eb987e2f
  a9af385a_fd12_f1d8_7cf0_ccb9b281ca18["atRule()"]
  8bb120ad_8027_cede_007f_1d5351f7ca96 -->|calls| a9af385a_fd12_f1d8_7cf0_ccb9b281ca18
  e9d556bc_f22d_356c_1bd2_27442c34b5c7["walk()"]
  8bb120ad_8027_cede_007f_1d5351f7ca96 -->|calls| e9d556bc_f22d_356c_1bd2_27442c34b5c7
  c9955487_1860_09fc_569b_44f655a0567c["get()"]
  8bb120ad_8027_cede_007f_1d5351f7ca96 -->|calls| c9955487_1860_09fc_569b_44f655a0567c
  style 8bb120ad_8027_cede_007f_1d5351f7ca96 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/compile.ts lines 177–258

export function applyVariant(
  node: Rule,
  variant: Variant,
  variants: Variants,
  depth: number = 0,
): null | void {
  if (variant.kind === 'arbitrary') {
    // Relative selectors are not valid as an entire arbitrary variant, only as
    // an arbitrary variant that is part of another compound variant.
    //
    // E.g. `[>img]:flex` is not valid, but `has-[>img]:flex` is
    if (variant.relative && depth === 0) return null

    node.nodes = [rule(variant.selector, node.nodes)]
    return
  }

  // SAFETY: At this point it is safe to use TypeScript's non-null assertion
  // operator because if the `candidate.root` didn't exist, `parseCandidate`
  // would have returned `null` and we would have returned early resulting in
  // not hitting this code path.
  let { applyFn } = variants.get(variant.root)!

  if (variant.kind === 'compound') {
    // Some variants traverse the AST to mutate the nodes. E.g.: `group-*` wants
    // to prefix every selector of the variant it's compounding with `.group`.
    //
    // E.g.:
    // ```
    // group-hover:[&_p]:flex
    // ```
    //
    // Should only prefix the `group-hover` part with `.group`, and not the `&_p` part.
    //
    // To solve this, we provide an isolated placeholder node to the variant.
    // The variant can now apply its logic to the isolated node without
    // affecting the original node.
    let isolatedNode = atRule('@slot')

    let result = applyVariant(isolatedNode, variant.variant, variants, depth + 1)
    if (result === null) return null

    if (variant.root === 'not' && isolatedNode.nodes.length > 1) {
      // The `not` variant cannot negate sibling rules / at-rules because these
      // are an OR relationship. Doing so would require transforming sibling
      // nodes into nesting while negating them. This isn't possible with the
      // current implementation of the `not` variant or with how variants are
      // applied in general (on a per-node basis).
      return null
    }

    for (let child of isolatedNode.nodes) {
      // Only some variants wrap children in rules. For example, the `force`
      // variant is a noop on the AST. And the `has` variant modifies the
      // selector rather than the children.
      //
      // This means `child` may be a declaration and we don't want to apply the
      // variant to it. This also means the entire variant as a whole is not
      // applicable to the rule and should generate nothing.
      if (child.kind !== 'rule' && child.kind !== 'at-rule') return null

      let result = applyFn(child, variant)
      if (result === null) return null
    }

    // Replace the placeholder node with the actual node
    {
      walk(isolatedNode.nodes, (child) => {
        if ((child.kind === 'rule' || child.kind === 'at-rule') && child.nodes.length <= 0) {
          child.nodes = node.nodes
          return WalkAction.Skip
        }
      })
      node.nodes = isolatedNode.nodes
    }
    return
  }

  // All other variants
  let result = applyFn(node, variant)
  if (result === null) return null
}

Subdomains

Frequently Asked Questions

What does applyVariant() do?
applyVariant() is a function in the tailwindcss codebase.
What does applyVariant() call?
applyVariant() calls 4 function(s): atRule, get, rule, walk.
What calls applyVariant()?
applyVariant() is called by 3 function(s): compileAstNodes, getVariants, substituteAtVariant.

Analyze Your Own Codebase

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

Try Supermodel Free