-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
create-instance.ts
191 lines (173 loc) Β· 4.53 KB
/
create-instance.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
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
import createCache from '@emotion/cache'
import {
serializeStyles,
CSSInterpolation,
Interpolation
} from '@emotion/serialize'
import {
insertStyles,
getRegisteredStyles,
SerializedStyles,
RegisteredCache
} from '@emotion/utils'
import { EmotionCache, Options } from '@emotion/cache'
import { StyleSheet } from '@emotion/sheet'
import isDevelopment from '#is-development'
export type {
CSSInterpolation,
ArrayCSSInterpolation,
ComponentSelector,
CSSObject
} from '@emotion/serialize'
function insertWithoutScoping(
cache: EmotionCache,
serialized: SerializedStyles
) {
if (cache.inserted[serialized.name] === undefined) {
return cache.insert('', serialized, cache.sheet, true)
}
}
export type { EmotionCache, Options }
export interface ArrayClassNamesArg extends Array<ClassNamesArg> {}
export type ClassNamesArg =
| undefined
| null
| string
| boolean
| { [className: string]: boolean | null | undefined }
| ArrayClassNamesArg
export interface CSSStyleSheet extends StyleSheet {
speedy(value: boolean): void
}
export interface Emotion {
css(template: TemplateStringsArray, ...args: Array<CSSInterpolation>): string
css(...args: Array<CSSInterpolation>): string
cx(...classNames: Array<ClassNamesArg>): string
flush(): void
hydrate(ids: Array<string>): void
injectGlobal(
template: TemplateStringsArray,
...args: Array<CSSInterpolation>
): void
injectGlobal(...args: Array<CSSInterpolation>): void
keyframes(
template: TemplateStringsArray,
...args: Array<CSSInterpolation>
): string
keyframes(...args: Array<CSSInterpolation>): string
sheet: CSSStyleSheet
cache: EmotionCache
merge(className: string): string
getRegisteredStyles(
registeredStyles: Array<string>,
className: string
): string
}
function merge(
registered: RegisteredCache,
css: Emotion['css'],
className: string
) {
const registeredStyles: string[] = []
const rawClassName = getRegisteredStyles(
registered,
registeredStyles,
className
)
if (registeredStyles.length < 2) {
return className
}
return rawClassName + css(registeredStyles)
}
let createEmotion = (options: Options): Emotion => {
let cache = createCache(options)
;(cache.sheet as CSSStyleSheet).speedy = function (value: boolean) {
if (isDevelopment && this.ctr !== 0) {
throw new Error('speedy must be changed before any rules are inserted')
}
this.isSpeedy = value
}
cache.compat = true
let css: Emotion['css'] = (
...args: (TemplateStringsArray | Interpolation<unknown>)[]
) => {
let serialized = serializeStyles(args, cache.registered, undefined)
insertStyles(cache, serialized, false)
return `${cache.key}-${serialized.name}`
}
let keyframes: Emotion['keyframes'] = (
...args: (TemplateStringsArray | Interpolation<unknown>)[]
) => {
let serialized = serializeStyles(args, cache.registered)
let animation = `animation-${serialized.name}`
insertWithoutScoping(cache, {
name: serialized.name,
styles: `@keyframes ${animation}{${serialized.styles}}`
})
return animation
}
let injectGlobal: Emotion['injectGlobal'] = (
...args: (TemplateStringsArray | Interpolation<unknown>)[]
) => {
let serialized = serializeStyles(args, cache.registered)
insertWithoutScoping(cache, serialized)
}
let cx: Emotion['cx'] = (...args) => {
return merge(cache.registered, css, classnames(args))
}
return {
css,
cx,
injectGlobal,
keyframes,
hydrate(ids) {
ids.forEach(key => {
cache.inserted[key] = true
})
},
flush() {
cache.registered = {}
cache.inserted = {}
cache.sheet.flush()
},
sheet: cache.sheet as CSSStyleSheet,
cache,
getRegisteredStyles: getRegisteredStyles.bind(null, cache.registered),
merge: merge.bind(null, cache.registered, css)
}
}
let classnames = (args: ClassNamesArg[]) => {
let cls = ''
for (let i = 0; i < args.length; i++) {
let arg = args[i]
if (arg == null) continue
let toAdd
switch (typeof arg) {
case 'boolean':
break
case 'object': {
if (Array.isArray(arg)) {
toAdd = classnames(arg)
} else {
toAdd = ''
for (const k in arg) {
if (arg[k] && k) {
toAdd && (toAdd += ' ')
toAdd += k
}
}
}
break
}
default: {
toAdd = arg
}
}
if (toAdd) {
cls && (cls += ' ')
cls += toAdd
}
}
return cls
}
export default createEmotion