Spaces:
Build error

File size: 934 Bytes
3459571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export type JsonObject = {
  [key: string]: string | number | JsonObject
}

export default function cssVars(variables: JsonObject | string) {
  const variablesObject: JsonObject =
    typeof variables === 'string' ? JSON.parse(variables) : variables
  return generateVariables('', variablesObject)
}

function generateVariables(path: string, object: JsonObject): string {
  let styles = ''
  Object.entries(object).forEach(([key, value]) => {
    if (typeof value === 'object') {
      styles += generateVariables(`${join(path, key)}`, value)
    } else {
      styles += `--${join(path, key)}: ${value};`
    }
  })
  return styles
}

function join(path: string, key: string): string {
  if (!path) return key
  // ignore __default property and use parent key instead
  // e.g. blue: { default: "blue", light: "lightblue" } => --blue: blue; --blue-light: lightblue;
  if (key === '__default') return path
  return `${path}-${key}`
}