Home / File/ create-style-map.ts — ui Source File

create-style-map.ts — ui Source File

Architecture documentation for create-style-map.ts, a typescript file in the ui codebase. 3 imports, 4 dependents.

File typescript FrameworkTooling CLICore 3 imports 4 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  20ad6f91_6d0b_ef0e_6867_01a114c31d1d["create-style-map.ts"]
  b71db677_2c3f_c5cd_61c3_36109697225a["postcss"]
  20ad6f91_6d0b_ef0e_6867_01a114c31d1d --> b71db677_2c3f_c5cd_61c3_36109697225a
  1f073b7b_06bb_77ca_889f_a1e7ce082531["postcss-selector-parser"]
  20ad6f91_6d0b_ef0e_6867_01a114c31d1d --> 1f073b7b_06bb_77ca_889f_a1e7ce082531
  6802ce19_522d_e5fb_e458_8826d9f6952e["zod"]
  20ad6f91_6d0b_ef0e_6867_01a114c31d1d --> 6802ce19_522d_e5fb_e458_8826d9f6952e
  41e4e400_ae1e_8f4f_8458_fae7ce5b1dab["create-style-map.test.ts"]
  41e4e400_ae1e_8f4f_8458_fae7ce5b1dab --> 20ad6f91_6d0b_ef0e_6867_01a114c31d1d
  b6929276_0c11_78e4_0d6e_5cbe870a5cec["transform-style-map.test.ts"]
  b6929276_0c11_78e4_0d6e_5cbe870a5cec --> 20ad6f91_6d0b_ef0e_6867_01a114c31d1d
  cef4e429_7112_fef5_ae0b_a838a57ccf71["transform-style-map.ts"]
  cef4e429_7112_fef5_ae0b_a838a57ccf71 --> 20ad6f91_6d0b_ef0e_6867_01a114c31d1d
  b35e7ac9_4f0f_f678_f1a6_1e42325d5108["transform.ts"]
  b35e7ac9_4f0f_f678_f1a6_1e42325d5108 --> 20ad6f91_6d0b_ef0e_6867_01a114c31d1d
  style 20ad6f91_6d0b_ef0e_6867_01a114c31d1d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import postcss from "postcss"
import selectorParser, {
  type ClassName,
  type Selector as SelectorNodeRoot,
} from "postcss-selector-parser"
import { z } from "zod"

const CN_PREFIX = "cn-"

export const styleMapSchema = z.record(
  z.string().startsWith(CN_PREFIX),
  z.string()
)

export type StyleMap = z.infer<typeof styleMapSchema>

export function createStyleMap(input: string) {
  const root = postcss.parse(input)

  const result: Record<string, string> = {}

  root.walkRules((rule) => {
    const selectors = rule.selectors ?? []

    if (selectors.length === 0) {
      return
    }

    const tailwindClasses = extractTailwindClasses(rule)

    if (!tailwindClasses) {
      return
    }

    for (const selector of selectors) {
      const normalizedSelector = normalizeSelector(selector)

      selectorParser((selectorsRoot) => {
        selectorsRoot.each((sel) => {
          const targetClass = findSubjectClass(sel)

          if (!targetClass) {
            return
          }

          const className = targetClass.value

          if (!className.startsWith(CN_PREFIX)) {
            return
          }

          result[className] = result[className]
            ? `${tailwindClasses} ${result[className]}`
            : tailwindClasses
        })
      }).processSync(normalizedSelector)
    }
  })

  return styleMapSchema.parse(result)
}

function normalizeSelector(selector: string) {
  return selector.replace(/\s*&\s*/g, "").trim()
}

function extractTailwindClasses(rule: postcss.Rule) {
  const classes: string[] = []

  for (const node of rule.nodes || []) {
    if (node.type === "atrule" && node.name === "apply") {
      const value = node.params.trim()
      if (value) {
        classes.push(value)
      }
    }
  }

  if (classes.length === 0) {
    return null
  }

  return classes.join(" ")
}

function findSubjectClass(selector: SelectorNodeRoot) {
  const classNodes: ClassName[] = []

  selector.walkClasses((classNode) => {
    if (classNode.value.startsWith(CN_PREFIX)) {
      classNodes.push(classNode)
    }
  })

  if (classNodes.length === 0) {
    return null
  }

  return classNodes[classNodes.length - 1]
}

Subdomains

Types

Dependencies

  • postcss
  • postcss-selector-parser
  • zod

Frequently Asked Questions

What does create-style-map.ts do?
create-style-map.ts is a source file in the ui codebase, written in typescript. It belongs to the FrameworkTooling domain, CLICore subdomain.
What functions are defined in create-style-map.ts?
create-style-map.ts defines 4 function(s): createStyleMap, extractTailwindClasses, findSubjectClass, normalizeSelector.
What does create-style-map.ts depend on?
create-style-map.ts imports 3 module(s): postcss, postcss-selector-parser, zod.
What files import create-style-map.ts?
create-style-map.ts is imported by 4 file(s): create-style-map.test.ts, transform-style-map.test.ts, transform-style-map.ts, transform.ts.
Where is create-style-map.ts in the architecture?
create-style-map.ts is located at packages/shadcn/src/styles/create-style-map.ts (domain: FrameworkTooling, subdomain: CLICore, directory: packages/shadcn/src/styles).

Analyze Your Own Codebase

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

Try Supermodel Free