/
get-class-name.ts
39 lines (36 loc) · 1.09 KB
/
get-class-name.ts
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
import hash from '@emotion/hash'
import getSafeValue from './get-safe-value'
export interface PropertyInfo {
className?: string
safeValue?: boolean
complexValue?: boolean
jsName?: string
cssName?: string
defaultUnit?: string
isPrefixed?: boolean
}
/**
* Generates the class name.
*/
export default function getClassName(propertyInfo: PropertyInfo, value: string) {
const {
className,
safeValue = false, // Value never contains unsafe characters. e.g: 10, hidden, border-box
complexValue = false // Complex values that are best hashed. e.g: background-image
} = propertyInfo
let valueKey: string
// Shortcut the global keywords
if (value === 'inherit' || value === 'initial' || value === 'unset') {
valueKey = value
/* Always hash values that contain a calc() because the operators get
stripped which can result in class name collisions
*/
} else if (complexValue || value.includes('calc(')) {
valueKey = hash(value)
} else if (safeValue) {
valueKey = value
} else {
valueKey = getSafeValue(value)
}
return `ub-${className}_${valueKey}`
}