-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathindex.ts
More file actions
52 lines (42 loc) · 1.54 KB
/
index.ts
File metadata and controls
52 lines (42 loc) · 1.54 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pluralize from 'pluralize'
import { Theme } from '@theme-ui/css'
// Simplified validator based on spec https://www.w3.org/TR/CSS22/syndata.html#value-def-identifier
// Does not check for "cannot start with a digit, two hyphens, or a hyphen followed by a digit"
const keyValidator = /^[A-z0-9][\w-]*$/
interface CustomProperties {
[key: string]: string | number
}
export default function makeCustomProperties(theme: Theme, prefix?: string) {
const customProperties: CustomProperties = {}
const generateProperties = (object: object, previousKey?: string) => {
Object.entries(object).forEach(([key, value]) => {
let formattedKey = pluralize(key, 1) || key
if (
process.env.NODE_ENV !== 'production' &&
!keyValidator.test(formattedKey)
) {
console.warn(
`[theme-ui] Theme key "${value}" found will produce an invalid CSS custom property. ` +
'Keys must only contain the following: A-Z, a-z, 0-9, hyphen, underscore.'
)
}
if (prefix && !previousKey) {
formattedKey = `${prefix}-${formattedKey}`
}
const newKey = previousKey
? previousKey + '-' + formattedKey
: formattedKey
if (Array.isArray(value)) {
value.forEach((item, i) => {
customProperties[`--${newKey}-${i}`] = item
})
} else if (Object(value) === value) {
generateProperties(value, newKey)
} else {
customProperties[`--${newKey}`] = value
}
})
}
generateProperties(theme)
return customProperties
}