Home / Function/ collapseCandidates() — tailwindcss Function Reference

collapseCandidates() — tailwindcss Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  6b36dfc9_f213_8083_55ea_8d0541032f05["collapseCandidates()"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|defined in| 7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e
  e4f6e1fd_7086_5c1c_46c8_96f3c90e6782["canonicalizeCandidates()"]
  e4f6e1fd_7086_5c1c_46c8_96f3c90e6782 -->|calls| 6b36dfc9_f213_8083_55ea_8d0541032f05
  f712ed47_45d4_4e5a_dd73_fdefa1da71da["segment()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| f712ed47_45d4_4e5a_dd73_fdefa1da71da
  06ed9408_12cf_7ddd_a435_8cdd942de1d4["add()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| 06ed9408_12cf_7ddd_a435_8cdd942de1d4
  8a1ac231_1225_a863_301e_a2e539358b72["entries()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| 8a1ac231_1225_a863_301e_a2e539358b72
  43b980e4_33bc_5e85_f71f_8016b6d2e809["keysInNamespaces()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| 43b980e4_33bc_5e85_f71f_8016b6d2e809
  a15b3c4a_76ff_0090_fc86_bac24f0a4135["isValidSpacingMultiplier()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| a15b3c4a_76ff_0090_fc86_bac24f0a4135
  80b238bf_83b5_f1c4_8bb5_c4c28573f0c8["intersection()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| 80b238bf_83b5_f1c4_8bb5_c4c28573f0c8
  ab8d344f_10ae_ff6d_9378_cf7d5cb6ec72["combinations()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| ab8d344f_10ae_ff6d_9378_cf7d5cb6ec72
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  6b36dfc9_f213_8083_55ea_8d0541032f05 -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  style 6b36dfc9_f213_8083_55ea_8d0541032f05 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 200–417

function collapseCandidates(options: InternalCanonicalizeOptions, candidates: string[]): string[] {
  if (candidates.length <= 1) return candidates
  let designSystem = options.designSystem

  // To keep things simple, we group candidates such that we only collapse
  // candidates with the same variants and important modifier together.
  let groups = new DefaultMap((_before: string) => {
    return new DefaultMap((_after: string) => {
      return new Set<string>()
    })
  })

  let prefix = options.designSystem.theme.prefix ? `${options.designSystem.theme.prefix}:` : ''

  for (let candidate of candidates) {
    let variants = segment(candidate, ':')
    let utility = variants.pop()!

    let important = utility.endsWith('!')
    if (important) {
      utility = utility.slice(0, -1)
    }

    let before = variants.length > 0 ? `${variants.join(':')}:` : ''
    let after = important ? '!' : ''

    // Group by variants and important flag
    groups.get(before).get(after).add(`${prefix}${utility}`)
  }

  let result = new Set<string>()
  for (let [before, group] of groups.entries()) {
    for (let [after, candidates] of group.entries()) {
      for (let candidate of collapseGroup(Array.from(candidates))) {
        // Drop the prefix if we had one, because the prefix is already there as
        // part of the variants.
        if (prefix && candidate.startsWith(prefix)) {
          candidate = candidate.slice(prefix.length)
        }

        result.add(`${before}${candidate}${after}`)
      }
    }
  }

  return Array.from(result)

  function collapseGroup(candidates: string[]) {
    let signatureOptions = options.signatureOptions
    let computeUtilitiesPropertiesLookup =
      designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions)
    let staticUtilities = designSystem.storage[STATIC_UTILITIES_KEY].get(signatureOptions)

    // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem`
    //
    // NOTE: Currently assuming we are dealing with static utilities only. This
    // will change the moment we have `@utility` for most built-ins.
    let candidatePropertiesValues = candidates.map((candidate) =>
      computeUtilitiesPropertiesLookup.get(candidate),
    )

    // Hard-coded optimization: if any candidate sets `line-height` and another
    // candidate sets `font-size`, we pre-compute the `text-*` utilities with
    // this line-height to try and collapse to those combined values.
    if (candidatePropertiesValues.some((x) => x.has('line-height'))) {
      let fontSizeNames = designSystem.theme.keysInNamespaces(['--text'])
      if (fontSizeNames.length > 0) {
        let interestingLineHeights = new Set<string | number>()
        let seenLineHeights = new Set<string>()
        for (let pairs of candidatePropertiesValues) {
          for (let lineHeight of pairs.get('line-height')) {
            if (seenLineHeights.has(lineHeight)) continue
            seenLineHeights.add(lineHeight)

            let bareValue = designSystem.storage[SPACING_KEY]?.get(lineHeight) ?? null
            if (bareValue !== null) {
              if (isValidSpacingMultiplier(bareValue)) {
                interestingLineHeights.add(bareValue)

                for (let name of fontSizeNames) {
                  computeUtilitiesPropertiesLookup.get(`text-${name}/${bareValue}`)

Subdomains

Frequently Asked Questions

What does collapseCandidates() do?
collapseCandidates() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is collapseCandidates() defined?
collapseCandidates() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 200.
What does collapseCandidates() call?
collapseCandidates() calls 8 function(s): add, combinations, entries, get, intersection, isValidSpacingMultiplier, keysInNamespaces, segment.
What calls collapseCandidates()?
collapseCandidates() is called by 1 function(s): canonicalizeCandidates.

Analyze Your Own Codebase

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

Try Supermodel Free