Home / Function/ createCssUtility() — tailwindcss Function Reference

createCssUtility() — tailwindcss Function Reference

Architecture documentation for the createCssUtility() function in utilities.ts from the tailwindcss codebase.

Function typescript OxideEngine Extractor calls 15 called by 1

Entity Profile

Dependency Diagram

graph TD
  75cdf0b0_3569_52fd_7186_577645fd4872["createCssUtility()"]
  2bc6f8eb_6339_d09c_79df_e9025a479c97["utilities.ts"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|defined in| 2bc6f8eb_6339_d09c_79df_e9025a479c97
  3970218d_3d6c_e455_87cc_45b4a094f0e9["parseCss()"]
  3970218d_3d6c_e455_87cc_45b4a094f0e9 -->|calls| 75cdf0b0_3569_52fd_7186_577645fd4872
  1edbc988_3b60_5ad0_3e4c_89aca8f06008["isValidFunctionalUtilityName()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 1edbc988_3b60_5ad0_3e4c_89aca8f06008
  be813be6_4d88_f498_92f9_713a0891c048["value()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| be813be6_4d88_f498_92f9_713a0891c048
  ed78da58_8727_ad98_120c_61f35cea357a["walk()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| ed78da58_8727_ad98_120c_61f35cea357a
  2d6c8361_96d8_df0d_ca51_c62f179fdc73["parse()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 2d6c8361_96d8_df0d_ca51_c62f179fdc73
  f712ed47_45d4_4e5a_dd73_fdefa1da71da["segment()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| f712ed47_45d4_4e5a_dd73_fdefa1da71da
  8a1ac231_1225_a863_301e_a2e539358b72["entries()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 8a1ac231_1225_a863_301e_a2e539358b72
  06ed9408_12cf_7ddd_a435_8cdd942de1d4["add()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 06ed9408_12cf_7ddd_a435_8cdd942de1d4
  2a767b71_a71c_597d_598f_9de772a6094a["functional()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 2a767b71_a71c_597d_598f_9de772a6094a
  88bcab2f_f837_9e57_5b6a_fda72a4c3315["cloneAstNode()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 88bcab2f_f837_9e57_5b6a_fda72a4c3315
  3df007ff_869c_dbfa_d21c_be64d5ce3eec["resolveValueFunction()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 3df007ff_869c_dbfa_d21c_be64d5ce3eec
  3bf47f1b_2209_a8a0_ee89_625a80aa3b7a["suggest()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 3bf47f1b_2209_a8a0_ee89_625a80aa3b7a
  4e39f1e7_ebcb_7f62_4a59_b645e71d50fd["isPositiveInteger()"]
  75cdf0b0_3569_52fd_7186_577645fd4872 -->|calls| 4e39f1e7_ebcb_7f62_4a59_b645e71d50fd
  style 75cdf0b0_3569_52fd_7186_577645fd4872 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/utilities.ts lines 5941–6301

export function createCssUtility(node: AtRule) {
  let name = node.params

  // Functional utilities. E.g.: `tab-size-*`
  if (isValidFunctionalUtilityName(name)) {
    // API:
    //
    // - `--value('literal')`         resolves a literal named value
    // - `--value(number)`            resolves a bare value of type number
    // - `--value([number])`          resolves an arbitrary value of type number
    // - `--value(--color)`           resolves a theme value in the `color` namespace
    // - `--value(number, [number])`  resolves a bare value of type number or an
    //                                arbitrary value of type number in order.
    //
    // Rules:
    //
    // - If `--value(…)` does not resolve to a valid value, the declaration is
    //   removed.
    // - If a `--value(ratio)` resolves, the `--modifier(…)` cannot be used.
    // - If a candidate looks like `foo-2/3`, then the `--value(ratio)` should
    //   be used OR the `--value(…)` and `--modifier(…)` must be used. But not
    //   both.
    // - All parts of the candidate must resolve, otherwise it's not a valid
    //   utility. E.g.:`
    //   ```
    //   @utility foo-* {
    //     test: --value(number)
    //   }
    //   ```
    //   If you then use `foo-1/2`, this is invalid, because the modifier is not used.

    return (designSystem: DesignSystem) => {
      let storage = {
        '--value': {
          usedSpacingInteger: false,
          usedSpacingNumber: false,
          themeKeys: new Set<`--${string}`>(),
          literals: new Set<string>(),
        },
        '--modifier': {
          usedSpacingInteger: false,
          usedSpacingNumber: false,
          themeKeys: new Set<`--${string}`>(),
          literals: new Set<string>(),
        },
      }

      // Pre-process the AST to make it easier to work with.
      //
      // - Normalize theme values used in `--value(…)` and `--modifier(…)`
      //   functions.
      // - Track information for suggestions
      walk(node.nodes, (child) => {
        if (child.kind !== 'declaration') return
        if (!child.value) return
        if (!child.value.includes('--value(') && !child.value.includes('--modifier(')) return

        let declarationValueAst = ValueParser.parse(child.value)

        // Required manipulations:
        //
        // - `--value(--spacing)`                 -> `--value(--spacing-*)`
        // - `--value(--spacing- *)`              -> `--value(--spacing-*)`
        // - `--value(--text- * --line-height)`   -> `--value(--text-*--line-height)`
        // - `--value(--text --line-height)`      -> `--value(--text-*--line-height)`
        // - `--value(--text-\\* --line-height)`  -> `--value(--text-*--line-height)`
        // - `--value([ *])`                      -> `--value([*])`
        //
        // Once Prettier / Biome handle these better (e.g.: not crashing without
        // `\\*` or not inserting whitespace) then most of these can go away.
        walk(declarationValueAst, (fn) => {
          if (fn.kind !== 'function') return

          // Track usage of `--spacing(…)`
          if (
            fn.value === '--spacing' &&
            // Quick bail check if we already know that `--value` and `--modifier` are
            // using the full `--spacing` theme scale.
            !(storage['--modifier'].usedSpacingNumber && storage['--value'].usedSpacingNumber)
          ) {
            walk(fn.nodes, (node) => {

Domain

Subdomains

Called By

Frequently Asked Questions

What does createCssUtility() do?
createCssUtility() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/utilities.ts.
Where is createCssUtility() defined?
createCssUtility() is defined in packages/tailwindcss/src/utilities.ts at line 5941.
What does createCssUtility() call?
createCssUtility() calls 15 function(s): add, cloneAstNode, entries, functional, isPositiveInteger, isValidFunctionalUtilityName, isValidStaticUtilityName, keysInNamespaces, and 7 more.
What calls createCssUtility()?
createCssUtility() is called by 1 function(s): parseCss.

Analyze Your Own Codebase

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

Try Supermodel Free