Home / Function/ arbitraryUtilities() — tailwindcss Function Reference

arbitraryUtilities() — tailwindcss Function Reference

Architecture documentation for the arbitraryUtilities() function in canonicalize-candidates.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  1fde95ca_8980_95b5_3738_6b5133f8ff3e["arbitraryUtilities()"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|defined in| 7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e
  87ba7eab_3a52_d53e_dfd4_e507e9763b55["printCandidate()"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|calls| 87ba7eab_3a52_d53e_dfd4_e507e9763b55
  ae4aae64_c516_49b5_ec89_bc7918645ee4["allVariablesAreUsed()"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|calls| ae4aae64_c516_49b5_ec89_bc7918645ee4
  ef9ad758_33f4_0d23_fff8_8eeaf71d00d8["parseCandidate()"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|calls| ef9ad758_33f4_0d23_fff8_8eeaf71d00d8
  a15b3c4a_76ff_0090_fc86_bac24f0a4135["isValidSpacingMultiplier()"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|calls| a15b3c4a_76ff_0090_fc86_bac24f0a4135
  a6395463_48d3_f807_4ce5_c8a5f1546d06["printModifier()"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|calls| a6395463_48d3_f807_4ce5_c8a5f1546d06
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  1fde95ca_8980_95b5_3738_6b5133f8ff3e -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  style 1fde95ca_8980_95b5_3738_6b5133f8ff3e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 967–1143

function arbitraryUtilities(candidate: Candidate, options: InternalCanonicalizeOptions): Candidate {
  // We are only interested in arbitrary properties and arbitrary values
  if (
    // Arbitrary property
    candidate.kind !== 'arbitrary' &&
    // Arbitrary value
    !(candidate.kind === 'functional' && candidate.value?.kind === 'arbitrary')
  ) {
    return candidate
  }

  let designSystem = options.designSystem
  let utilities = designSystem.storage[PRE_COMPUTED_UTILITIES_KEY].get(options.signatureOptions)
  let signatures = designSystem.storage[UTILITY_SIGNATURE_KEY].get(options.signatureOptions)

  let targetCandidateString = designSystem.printCandidate(candidate)

  // Compute the signature for the target candidate
  let targetSignature = signatures.get(targetCandidateString)
  if (typeof targetSignature !== 'string') return candidate

  // Try a few options to find a suitable replacement utility
  for (let replacementCandidate of tryReplacements(targetSignature, candidate)) {
    let replacementString = designSystem.printCandidate(replacementCandidate)
    let replacementSignature = signatures.get(replacementString)
    if (replacementSignature !== targetSignature) {
      continue
    }

    // Ensure that if CSS variables were used, that they are still used
    if (!allVariablesAreUsed(designSystem, candidate, replacementCandidate)) {
      continue
    }

    return replacementCandidate
  }

  return candidate

  function* tryReplacements(
    targetSignature: string,
    candidate: Extract<Candidate, { kind: 'functional' | 'arbitrary' }>,
  ): Generator<Candidate> {
    // Find a corresponding utility for the same signature
    let replacements = utilities.get(targetSignature)

    // Multiple utilities can map to the same signature. Not sure how to migrate
    // this one so let's just skip it for now.
    //
    // TODO: Do we just migrate to the first one?
    if (replacements.length > 1) return

    // If we didn't find any replacement utilities, let's try to strip the
    // modifier and find a replacement then. If we do, we can try to re-add the
    // modifier later and verify if we have a valid migration.
    //
    // This is necessary because `text-red-500/50` will not be pre-computed,
    // only `text-red-500` will.
    if (replacements.length === 0 && candidate.modifier) {
      let candidateWithoutModifier = { ...candidate, modifier: null }
      let targetSignatureWithoutModifier = signatures.get(
        designSystem.printCandidate(candidateWithoutModifier),
      )
      if (typeof targetSignatureWithoutModifier === 'string') {
        for (let replacementCandidate of tryReplacements(
          targetSignatureWithoutModifier,
          candidateWithoutModifier,
        )) {
          yield Object.assign({}, replacementCandidate, { modifier: candidate.modifier })
        }
      }
    }

    // If only a single utility maps to the signature, we can use that as the
    // replacement.
    if (replacements.length === 1) {
      for (let replacementCandidate of parseCandidate(designSystem, replacements[0])) {
        yield replacementCandidate
      }
    }

Subdomains

Frequently Asked Questions

What does arbitraryUtilities() do?
arbitraryUtilities() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is arbitraryUtilities() defined?
arbitraryUtilities() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 967.
What does arbitraryUtilities() call?
arbitraryUtilities() calls 6 function(s): allVariablesAreUsed, get, isValidSpacingMultiplier, parseCandidate, printCandidate, printModifier.

Analyze Your Own Codebase

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

Try Supermodel Free