Home / Function/ createThemeFn() — tailwindcss Function Reference

createThemeFn() — tailwindcss Function Reference

Architecture documentation for the createThemeFn() function in plugin-functions.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  d30151e4_eee8_a868_f516_c653088f4a03["createThemeFn()"]
  20b59de8_11c6_2432_2a83_24f6f6e741a7["plugin-functions.ts"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|defined in| 20b59de8_11c6_2432_2a83_24f6f6e741a7
  63f5d3ac_b848_6ab0_af8e_5d1ec324c0d5["mergeTheme()"]
  63f5d3ac_b848_6ab0_af8e_5d1ec324c0d5 -->|calls| d30151e4_eee8_a868_f516_c653088f4a03
  2efa0a66_c375_c031_24ad_1f7509bb9b14["buildPluginApi()"]
  2efa0a66_c375_c031_24ad_1f7509bb9b14 -->|calls| d30151e4_eee8_a868_f516_c653088f4a03
  1b250eae_0bea_d404_ca9e_42da26c56b45["toKeyPath()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| 1b250eae_0bea_d404_ca9e_42da26c56b45
  00a6c74c_0751_dcea_d32a_e486a30355a4["readFromCss()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| 00a6c74c_0751_dcea_d32a_e486a30355a4
  c16f096b_35b2_0695_2909_f85c627abd1e["resolveValue()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| c16f096b_35b2_0695_2909_f85c627abd1e
  760be792_5eb1_da6f_e22b_2c4b123c6248["get()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| 760be792_5eb1_da6f_e22b_2c4b123c6248
  a21213f9_9467_d282_d4f1_fcb2cf2ae2fc["deepMerge()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| a21213f9_9467_d282_d4f1_fcb2cf2ae2fc
  dfc91ad5_bcf3_d363_efd7_01a5223f7b74["unescape()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| dfc91ad5_bcf3_d363_efd7_01a5223f7b74
  0dfe396e_290f_02ff_5c72_321290ae550b["keys()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| 0dfe396e_290f_02ff_5c72_321290ae550b
  b71e300f_d366_e92d_f71f_413f7ee24e95["withAlpha()"]
  d30151e4_eee8_a868_f516_c653088f4a03 -->|calls| b71e300f_d366_e92d_f71f_413f7ee24e95
  style d30151e4_eee8_a868_f516_c653088f4a03 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/compat/plugin-functions.ts lines 11–114

export function createThemeFn(
  designSystem: DesignSystem,
  configTheme: () => UserConfig['theme'],
  resolveValue: (value: any) => any,
) {
  return function theme(path: string, defaultValue?: unknown) {
    // Extract an eventual modifier from the path. e.g.:
    // - "colors.red.500 / 50%" -> "50%"
    // - "foo/bar/baz/50%"      -> "50%"
    let lastSlash = path.lastIndexOf('/')
    let modifier: string | null = null
    if (lastSlash !== -1) {
      modifier = path.slice(lastSlash + 1).trim()
      path = path.slice(0, lastSlash).trim()
    }

    let resolvedValue = (() => {
      let keypath = toKeyPath(path)
      let [cssValue, options] = readFromCss(designSystem.theme, keypath)

      let configValue = resolveValue(get(configTheme() ?? {}, keypath) ?? null)

      if (typeof configValue === 'string') {
        configValue = configValue.replace('<alpha-value>', '1')
      }

      // Resolved to a primitive value.
      if (typeof cssValue !== 'object') {
        if (typeof options !== 'object' && options & ThemeOptions.DEFAULT) {
          return configValue ?? cssValue
        }

        return cssValue
      }

      if (configValue !== null && typeof configValue === 'object' && !Array.isArray(configValue)) {
        let configValueCopy: Record<string, unknown> & { __CSS_VALUES__?: Record<string, number> } =
          // We want to make sure that we don't mutate the original config
          // value. Ideally we use `structuredClone` here, but it's not possible
          // because it can contain functions.
          deepMerge({}, [configValue], (_, b) => b)

        // There is no `cssValue`, which means we can back-fill it with values
        // from the `configValue`.
        if (cssValue === null && Object.hasOwn(configValue, '__CSS_VALUES__')) {
          let localCssValue: Record<string, unknown> = {}
          for (let key in configValue.__CSS_VALUES__) {
            localCssValue[key] = configValue[key]
            delete configValueCopy[key]
          }
          cssValue = localCssValue
        }

        for (let key in cssValue) {
          if (key === '__CSS_VALUES__') continue

          // If the value is coming from a default source (`@theme default`),
          // then we keep the value from the JS config (which is also a
          // default source, but wins over the built-in defaults).
          if (
            configValue?.__CSS_VALUES__?.[key] & ThemeOptions.DEFAULT &&
            get(configValueCopy, key.split('-')) !== undefined
          ) {
            continue
          }

          // CSS values from `@theme` win over values from the config
          configValueCopy[unescape(key)] = cssValue[key]
        }

        return configValueCopy
      }

      // Handle the tuple case
      if (Array.isArray(cssValue) && Array.isArray(options) && Array.isArray(configValue)) {
        let base = cssValue[0]
        let extra = cssValue[1]

        // Values from the config overwrite any default values from the CSS theme
        if (options[0] & ThemeOptions.DEFAULT) {
          base = configValue[0] ?? base

Domain

Subdomains

Frequently Asked Questions

What does createThemeFn() do?
createThemeFn() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/compat/plugin-functions.ts.
Where is createThemeFn() defined?
createThemeFn() is defined in packages/tailwindcss/src/compat/plugin-functions.ts at line 11.
What does createThemeFn() call?
createThemeFn() calls 8 function(s): deepMerge, get, keys, readFromCss, resolveValue, toKeyPath, unescape, withAlpha.
What calls createThemeFn()?
createThemeFn() is called by 2 function(s): buildPluginApi, mergeTheme.

Analyze Your Own Codebase

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

Try Supermodel Free