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
  1ea8fd4a_634f_c2f0_5465_01d387d5207b["createThemeFn()"]
  9c6a3a4b_953d_4928_2f26_12c610c5c0e4["mergeTheme()"]
  9c6a3a4b_953d_4928_2f26_12c610c5c0e4 -->|calls| 1ea8fd4a_634f_c2f0_5465_01d387d5207b
  ad196438_55f7_af7b_1604_1d75c1c27d8e["buildPluginApi()"]
  ad196438_55f7_af7b_1604_1d75c1c27d8e -->|calls| 1ea8fd4a_634f_c2f0_5465_01d387d5207b
  f1189154_e89f_8988_3e83_53458fbcad21["toKeyPath()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| f1189154_e89f_8988_3e83_53458fbcad21
  1176a137_5b06_095c_fa3b_a8db365b8b18["readFromCss()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| 1176a137_5b06_095c_fa3b_a8db365b8b18
  fedba2ef_0b11_0dd0_fc72_1873b2b9e509["resolveValue()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| fedba2ef_0b11_0dd0_fc72_1873b2b9e509
  a0c16668_c872_21fc_296d_76c8221fee87["get()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| a0c16668_c872_21fc_296d_76c8221fee87
  982d7e72_5afc_4832_dfcb_f221e9d52c64["deepMerge()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| 982d7e72_5afc_4832_dfcb_f221e9d52c64
  03501eb1_e642_df92_15b1_af590e3332fb["unescape()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| 03501eb1_e642_df92_15b1_af590e3332fb
  318e504e_be93_f95b_211e_84e3eabd9029["keys()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| 318e504e_be93_f95b_211e_84e3eabd9029
  bf187488_02a1_3fac_d926_9593a941af2d["withAlpha()"]
  1ea8fd4a_634f_c2f0_5465_01d387d5207b -->|calls| bf187488_02a1_3fac_d926_9593a941af2d
  style 1ea8fd4a_634f_c2f0_5465_01d387d5207b 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
        }

        for (let key of Object.keys(extra)) {
          if (options[1][key] & ThemeOptions.DEFAULT) {
            extra[key] = configValue[1][key] ?? extra[key]
          }
        }

        return [base, extra]
      }

      // Values from CSS take precedence over values from the config
      return cssValue ?? configValue
    })()

    // Apply the opacity modifier if present
    if (modifier && typeof resolvedValue === 'string') {
      resolvedValue = withAlpha(resolvedValue, modifier)
    }

    return resolvedValue ?? defaultValue
  }
}

Subdomains

Frequently Asked Questions

What does createThemeFn() do?
createThemeFn() is a function in the tailwindcss codebase.
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