( theme: Theme, path: string[], )
| 114 | } |
| 115 | |
| 116 | function readFromCss( |
| 117 | theme: Theme, |
| 118 | path: string[], |
| 119 | ): |
| 120 | | [value: string | null | Record<string, unknown>, options: number] |
| 121 | | [value: Record<string, unknown>, options: Record<string, number>] { |
| 122 | // `--color-red-500` should resolve to the theme variable directly, no look up |
| 123 | // and handling of nested objects is required. |
| 124 | if (path.length === 1 && path[0].startsWith('--')) { |
| 125 | return [theme.get([path[0] as ThemeKey]), theme.getOptions(path[0])] as const |
| 126 | } |
| 127 | |
| 128 | type ThemeValue = |
| 129 | // A normal string value |
| 130 | | string |
| 131 | |
| 132 | // A nested tuple with additional data |
| 133 | | [main: string, extra: Record<string, string>] |
| 134 | |
| 135 | let themeKey = keyPathToCssProperty(path) |
| 136 | |
| 137 | let map = new Map<string | null, ThemeValue>() |
| 138 | let nested = new DefaultMap<string | null, Map<string, [value: string, options: number]>>( |
| 139 | () => new Map(), |
| 140 | ) |
| 141 | |
| 142 | let ns = theme.namespace(`--${themeKey}`) |
| 143 | if (ns.size === 0) { |
| 144 | return [null, ThemeOptions.NONE] |
| 145 | } |
| 146 | |
| 147 | let options = new Map() |
| 148 | |
| 149 | for (let [key, value] of ns) { |
| 150 | // Non-nested values can be set directly |
| 151 | if (!key || !key.includes('--')) { |
| 152 | map.set(key, value) |
| 153 | options.set(key, theme.getOptions(!key ? `--${themeKey}` : `--${themeKey}-${key}`)) |
| 154 | continue |
| 155 | } |
| 156 | |
| 157 | // Nested values are stored separately |
| 158 | let nestedIndex = key.indexOf('--') |
| 159 | |
| 160 | let mainKey = key.slice(0, nestedIndex) |
| 161 | let nestedKey = key.slice(nestedIndex + 2) |
| 162 | |
| 163 | // Make `nestedKey` camel case: |
| 164 | nestedKey = nestedKey.replace(/-([a-z])/g, (_, a) => a.toUpperCase()) |
| 165 | |
| 166 | nested |
| 167 | .get(mainKey === '' ? null : mainKey) |
| 168 | .set(nestedKey, [value, theme.getOptions(`--${themeKey}${key}`)]) |
| 169 | } |
| 170 | |
| 171 | let baseOptions = theme.getOptions(`--${themeKey}`) |
| 172 | for (let [key, extra] of nested) { |
| 173 | let value = map.get(key) |
no test coverage detected