MCPcopy
hub / github.com/tw-in-js/twind / theme

Function theme

packages/core/src/internal/theme.ts:45–102  ·  view source on GitHub ↗
(
    sectionKey?: string,
    key?: string,
    defaultValue?: any,
    opacityValue?: string | undefined,
  )

Source from the content-addressed store, hash-verified

43 return theme as ThemeFunction<Theme>
44
45 function theme(
46 sectionKey?: string,
47 key?: string,
48 defaultValue?: any,
49 opacityValue?: string | undefined,
50 ): any {
51 if (sectionKey) {
52 ;({ 1: sectionKey, 2: opacityValue } =
53 // eslint-disable-next-line no-sparse-arrays
54 /^(\S+?)(?:\s*\/\s*([^/]+))?$/.exec(sectionKey) || ([, sectionKey] as [undefined, string]))
55
56 if (/[.[]/.test(sectionKey)) {
57 const path: string[] = []
58
59 // dotted deep access: colors.gray.500 or spacing[2.5]
60 sectionKey.replace(
61 /\[([^\]]+)\]|([^.[]+)/g,
62 (_, $1, $2 = $1) => path.push($2) as unknown as string,
63 )
64
65 sectionKey = path.shift() as string
66 defaultValue = key
67 key = path.join('-')
68 }
69
70 const section =
71 resolved[sectionKey] ||
72 // two-step deref to allow extend section to reference base section
73 Object.assign(
74 Object.assign(
75 // Make sure to not get into recursive calls
76 (resolved[sectionKey] = {}),
77 deref(base, sectionKey),
78 ),
79 deref(extend, sectionKey),
80 )
81
82 if (key == null) return section
83
84 key ||= 'DEFAULT'
85
86 const value =
87 section[key] ?? key.split('-').reduce((obj, prop) => obj?.[prop], section) ?? defaultValue
88
89 return opacityValue
90 ? toColorValue(value, { opacityValue: resolveThemeFunction(opacityValue, theme) })
91 : value
92 }
93
94 // Collect the whole theme
95 const result = {} as Record<string, any>
96
97 for (const section of [...Object.keys(base), ...Object.keys(extend)]) {
98 result[section] = theme(section)
99 }
100
101 return result
102 }

Callers 5

colorsFunction · 0.70
resolveThemeFunctionFunction · 0.70
theme.test.tsFile · 0.70
autoDarkColorFunction · 0.50
theme.test.tsFile · 0.50

Calls 3

toColorValueFunction · 0.90
resolveThemeFunctionFunction · 0.90
derefFunction · 0.85

Tested by

no test coverage detected