Home / Function/ keyPathToCssProperty() — tailwindcss Function Reference

keyPathToCssProperty() — tailwindcss Function Reference

Architecture documentation for the keyPathToCssProperty() function in apply-config-to-theme.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  ae20c4fb_29d7_ca79_3c49_ca02c45d5369["keyPathToCssProperty()"]
  245c850a_c551_a2cf_854e_bba95b5a1339["apply-config-to-theme.ts"]
  ae20c4fb_29d7_ca79_3c49_ca02c45d5369 -->|defined in| 245c850a_c551_a2cf_854e_bba95b5a1339
  e26bfad3_530e_6523_7463_161205321110["migrateTheme()"]
  e26bfad3_530e_6523_7463_161205321110 -->|calls| ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  904b969f_df9e_eb9f_fedd_18eea0cfe028["migratePreflight()"]
  904b969f_df9e_eb9f_fedd_18eea0cfe028 -->|calls| ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  dc9585e4_0c18_e5de_1302_9b707f3ab6ed["createConverter()"]
  dc9585e4_0c18_e5de_1302_9b707f3ab6ed -->|calls| ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  77acef4a_feff_6e9f_7c6b_2b6942c9ad63["createConverterCache()"]
  77acef4a_feff_6e9f_7c6b_2b6942c9ad63 -->|calls| ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  9fb2cb3b_46e1_4148_c220_0e7f4519db6d["applyConfigToTheme()"]
  9fb2cb3b_46e1_4148_c220_0e7f4519db6d -->|calls| ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  00a6c74c_0751_dcea_d32a_e486a30355a4["readFromCss()"]
  00a6c74c_0751_dcea_d32a_e486a30355a4 -->|calls| ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  style ae20c4fb_29d7_ca79_3c49_ca02c45d5369 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/compat/apply-config-to-theme.ts lines 177–240

export function keyPathToCssProperty(path: string[]) {
  // In some special cases the `DEFAULT` key did not map to a "default" utility
  // e.g. `ringColor.DEFAULT` wasn't *just* used for `ring`. It was used for
  // all ring utilities as the color when one wasn't specified.
  //
  // We place these specialty values under the `--default-*` namespace to signal
  // that they are defaults used by (potentially) multiple utilities.
  let specialDefault = SPECIAL_DEFAULT_KEYS[path[0]]
  if (specialDefault && path[1] === 'DEFAULT') return `default-${specialDefault}`

  // The legacy container component config should not be included in the Theme
  if (path[0] === 'container') return null

  for (let part of path) {
    if (!IS_VALID_KEY.test(part)) return null
  }

  // Map old v3 namespaces to new theme namespaces
  let ns = OLD_TO_NEW_NAMESPACE[path[0]]
  if (ns) {
    path = path.slice()
    path[0] = ns
  }

  return (
    path
      // [1] should move into the nested object tuple. To create the CSS variable
      // name for this, we replace it with an empty string that will result in two
      // subsequent dashes when joined.
      //
      // E.g.:
      // - `fontSize.xs.1.lineHeight` -> `font-size-xs--line-height`
      // - `spacing.1` -> `--spacing-1`
      .map((path, idx, all) => (path === '1' && idx !== all.length - 1 ? '' : path))

      // Resolve the key path to a CSS variable segment
      .map((part, idx) => {
        part = part.replaceAll('.', '_')

        let shouldConvert =
          // The first "namespace" part should be converted to kebab-case
          // This converts things like backgroundColor to `background-color`
          idx === 0 ||
          // Any tuple nested key should be converted to kebab-case
          // These are identified with a leading `-`
          // e.g. `fontSize.xs.1.lineHeight` -> `font-size-xs--line-height`
          part.startsWith('-') ||
          // `lineHeight` is a bit of a special case in which it does not
          // always begin with a leading `-` even when as a nested tuple key
          part === 'lineHeight'

        if (shouldConvert) {
          part = part.replace(/([a-z])([A-Z])/g, (_, a, b) => `${a}-${b.toLowerCase()}`)
        }

        return part
      })

      // Remove the `DEFAULT` key at the end of a path
      // We're reading from CSS anyway so it'll be a string
      .filter((part, index) => part !== 'DEFAULT' || index !== path.length - 1)
      .join('-')
  )
}

Subdomains

Frequently Asked Questions

What does keyPathToCssProperty() do?
keyPathToCssProperty() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/compat/apply-config-to-theme.ts.
Where is keyPathToCssProperty() defined?
keyPathToCssProperty() is defined in packages/tailwindcss/src/compat/apply-config-to-theme.ts at line 177.
What calls keyPathToCssProperty()?
keyPathToCssProperty() is called by 6 function(s): applyConfigToTheme, createConverter, createConverterCache, migratePreflight, migrateTheme, readFromCss.

Analyze Your Own Codebase

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

Try Supermodel Free