-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcss.ts
More file actions
276 lines (261 loc) · 8.19 KB
/
css.ts
File metadata and controls
276 lines (261 loc) · 8.19 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { Config } from './config'
import { BORDER_KEY, CAMEL_TO_SNAKE, COLOR_KEYS, COMMA_JOINED, FALSE_VALUES, SHORTHANDS, TRANSFORM_KEYS_MAP, unitlessNumberProperties } from './constants'
import { boxShadowItem, boxShadowSyntax } from './cssPropertySet'
import { px, stringHash } from './helpers'
// exports
export { GlossPropertySet } from './cssPropertySet'
export { configureCSS } from './config'
export { SNAKE_TO_CAMEL, CAMEL_TO_SNAKE, SHORTHANDS, validCSSAttr, cssAttributeAbbreviations } from './constants'
export {
CSSPropertySet,
CSSPropertySetResolved,
CSSPropertySetStrict,
CSSPropertySetLoose,
CSSPropertyValThemeFn,
} from './cssPropertySet'
export * from './helpers'
export { camelToSnake, snakeToCamel } from './helpers'
export { ThemeObject, ThemeValueLike, ThemeSet, ThemeCoats } from './ThemeObject'
export type CSSConfig = {
errorMessage?: string
snakeCase?: boolean
ignoreCSSVariables?: boolean
resolveFunctionValue?: ((val: any) => any)
}
const emptyObject = Object.freeze({})
export function cssString(styles: Object, opts?: CSSConfig): string {
if (!styles) return ''
const shouldSnake = !opts || opts.snakeCase !== false
let style = ''
for (let key in styles) {
let value = cssValue(key, styles[key], false, opts)
// shorthands
if (value !== undefined) {
if (SHORTHANDS[key]) {
for (let k of SHORTHANDS[key]) {
k = shouldSnake ? CAMEL_TO_SNAKE[k] || k : k
style += `${k}:${px(value)};`
}
} else {
style += `${(shouldSnake && CAMEL_TO_SNAKE[key]) || key}:${value};`
}
}
}
return style
}
/**
* Because css objects are somewhat predictable, we can hash while looping them
* which saves us a full extra loop + some work of hashing the keys
*/
const keyHashes = {}
export function cssStringWithHash(styles: Object, opts?: CSSConfig): [number, string] {
if (!styles) return [0, '']
const shouldSnake = !opts || opts.snakeCase !== false
let style = ''
let hash = 0
for (let key in styles) {
const rawVal = styles[key]
let value = cssValue(key, rawVal, false, opts)
// shorthands
if (value !== undefined) {
// we want to return 0 if object is empty so we can check later by !hash
if (hash == 0) hash = 5381
// start key hash: keys are always the keys from css, cacheable
let keyHash = keyHashes[key]
if (!keyHash) {
keyHash = keyHashes[key] = stringHash(key)
}
hash = (hash * 33) ^ keyHash
// end key hash
if (typeof rawVal === 'number') {
hash = (hash * 33) ^ ((Math.abs(rawVal) + 250) + (rawVal < 0 ? 100 : 0))
} else {
hash = (hash * 33) ^ stringHash(value)
}
if (SHORTHANDS[key]) {
for (let k of SHORTHANDS[key]) {
k = shouldSnake ? CAMEL_TO_SNAKE[k] || k : k
style += `${k}:${px(value)};`
}
} else {
style += `${(shouldSnake && CAMEL_TO_SNAKE[key]) || key}:${value};`
}
}
}
return [Math.abs(hash), style]
}
export function css(styles: Object, opts?: CSSConfig): Object {
if (!styles) return emptyObject
const shouldSnake = !opts || opts.snakeCase !== false
const toReturn = {}
for (let key in styles) {
let value = cssValue(key, styles[key], true, opts)
// shorthands
if (value !== undefined) {
if (SHORTHANDS[key]) {
for (let k of SHORTHANDS[key]) {
k = shouldSnake ? CAMEL_TO_SNAKE[k] || k : k
toReturn[k] = px(value)
}
} else {
toReturn[(shouldSnake && CAMEL_TO_SNAKE[key]) || key] = value
}
}
}
return toReturn
}
export function cssValue(key: string, value: any, recurse = false, options?: CSSConfig) {
// function first so it can go through other transforms
if (options?.resolveFunctionValue && typeof value === 'function') {
value = options?.resolveFunctionValue(value)
}
// get falsy values
if (value === false) {
value === FALSE_VALUES[key]
}
// remove nullish
if (value === undefined || value === null || value === false) {
return
}
if (value && value.cssVariable && (!options || !options.ignoreCSSVariables)) {
if (value.toCSS) {
return value.toCSS()
}
return `var(--${value.cssVariable})`
}
const valueType = typeof value
if (valueType === 'string' || valueType === 'number') {
if (valueType === 'number' && !unitlessNumberProperties.has(key)) {
value += 'px'
}
return value
} else if (COLOR_KEYS.has(key) && Config.isColor(value)) {
return Config.toColor(value)
} else if (Array.isArray(value)) {
return processArray(key, value, 0, options)
} else if (valueType === 'object') {
if (value.getCSSValue) {
return value.getCSSValue()
}
// remove any weird looking objects (non-plain)
if (value.constructor.name !== 'Object') {
return
}
const res = processObject(key, value, options)
if (res !== undefined) {
return res
}
} else if (key === 'isolate') {
return value
} else {
if (recurse) {
const firstChar = key[0]
if (
// recurse into psuedo or media query
firstChar === '&' ||
firstChar === '@' ||
key === 'from' ||
key === 'to'
) {
return css(value, options)
}
}
}
if (__DEV__) {
console.error(`Invalid style value for ${key}`, value, options)
}
}
export function styleToClassName(style: string): string {
return `g${stringHash(style)}`
}
const objectToCSS = {
textShadow: ({ x, y, blur, color }) =>
`${px(x || 0)} ${px(y || 0)} ${px(blur || 0)} ${Config.toColor(color)}`,
boxShadow: (v: boxShadowSyntax) =>
`${v.inset ? 'inset ' : ''}${px(v.x || 0)} ${px(v.y || 0)} ${px(v.blur || 0)} ${px(
v.spread || 0,
)} ${Config.isColor(v.color) ? Config.toColor(v.color) : v.color || ''}`,
background: v =>
Config.isColor(v)
? Config.toColor(v)
: `${Config.toColor(v.color)} ${v.image || ''} ${(v.position
? v.position.join(' ')
: v.position) || ''} ${v.repeat || ''}`,
}
function processArrayItem(key: string, val: any, level: number = 0, options?: CSSConfig) {
// recurse
if (Array.isArray(val)) {
return processArray(key, val, level + 1, options)
}
return cssValue(key, val, false, options)
}
export function processArray(key: string, value: any[], level: number = 0, options?: CSSConfig): string {
// solid default option for borders
if (BORDER_KEY.has(key) && value.length === 2) {
value.push('solid')
}
if (key === 'fontFamily') {
return value.map(x => (x.includes(' ') ? `"${x}"` : x)).join(', ')
}
if (key === 'boxShadow') {
value = value.filter(Boolean).map(x => processBoxShadow(x, options))
} else {
value = value.map(val => processArrayItem(key, val, level, options))
}
return value.join(level === 0 && COMMA_JOINED.has(key) ? ', ' : ' ')
}
function processBoxShadow(val: boxShadowItem, options?: CSSConfig) {
if (Array.isArray(val)) {
return val
.map(x => {
return cssValue(Config.isColor(x) ? 'color' : '', x, false, options)
})
.join(' ')
}
if (val && typeof val === 'object') {
return objectToCSS.boxShadow(val)
}
return val
}
function objectValue(key: string, value: any) {
if (objectToCSS[key]) {
return objectToCSS[key](value)
}
if (
key === 'scale' ||
key === 'scaleX' ||
key === 'scaleY' ||
key === 'grayscale' ||
key === 'brightness'
) {
return value
}
if (typeof value === 'number') {
return `${value}px`
}
return value
}
export function processObject(key: string, object: any, options?: CSSConfig): string | undefined {
if (COLOR_KEYS.has(key)) {
if (Config.isColor(object)) {
return Config.toColor(object)
}
}
// plain object only
if (!object.constructor || Object.prototype.toString.call(object) === '[object Object]') {
const toReturn: string[] = []
for (const subKey in object) {
if (!Object.hasOwnProperty.call(object, subKey)) {
continue
}
let value = object[subKey]
if (Array.isArray(value)) {
value = processArray(key, value, 0, options)
} else {
value = objectValue(subKey, value)
}
toReturn.push(`${TRANSFORM_KEYS_MAP[subKey] || subKey}(${value})`)
}
return toReturn.join(' ')
}
}