Home / Function/ createThemeFn() — tailwindcss Function Reference

createThemeFn() — tailwindcss Function Reference

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

Function typescript Oxide PreProcessors calls 8 called by 2

Entity Profile

Dependency Diagram

graph TD
  dabb13bc_6018_934b_19c4_595b7bea686d["createThemeFn()"]
  3d2f062b_d3ec_6b3a_dc32_9d2e4732e20e["plugin-functions.ts"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|defined in| 3d2f062b_d3ec_6b3a_dc32_9d2e4732e20e
  e544c002_a540_ba66_527d_bb3fc84f4e10["mergeTheme()"]
  e544c002_a540_ba66_527d_bb3fc84f4e10 -->|calls| dabb13bc_6018_934b_19c4_595b7bea686d
  31653e23_464a_3652_4a48_0c82332a92c4["buildPluginApi()"]
  31653e23_464a_3652_4a48_0c82332a92c4 -->|calls| dabb13bc_6018_934b_19c4_595b7bea686d
  05d44181_6631_c1fa_4a63_96653e28f99c["toKeyPath()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| 05d44181_6631_c1fa_4a63_96653e28f99c
  bdec5541_c088_b3c7_4457_73a4592c6631["readFromCss()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| bdec5541_c088_b3c7_4457_73a4592c6631
  d238f217_f342_fe65_082c_b17252edf6de["resolveValue()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| d238f217_f342_fe65_082c_b17252edf6de
  1b59ba6b_4432_276c_dd0d_7664aec4893a["get()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| 1b59ba6b_4432_276c_dd0d_7664aec4893a
  e135ce03_2957_5c11_2b7b_695da98ab45f["deepMerge()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| e135ce03_2957_5c11_2b7b_695da98ab45f
  87a17ea2_6c8d_3d38_bd46_25d9267eeac0["unescape()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| 87a17ea2_6c8d_3d38_bd46_25d9267eeac0
  24bbf7d9_ff21_752d_5669_1a8f911d9ad1["keys()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| 24bbf7d9_ff21_752d_5669_1a8f911d9ad1
  2e8f6051_b6e8_4985_14e1_e11717a8d2ab["withAlpha()"]
  dabb13bc_6018_934b_19c4_595b7bea686d -->|calls| 2e8f6051_b6e8_4985_14e1_e11717a8d2ab
  style dabb13bc_6018_934b_19c4_595b7bea686d 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