-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
create-instance.js
161 lines (144 loc) Β· 3.67 KB
/
create-instance.js
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
// @flow
import createCache from '@emotion/cache'
import { serializeStyles } from '@emotion/serialize'
import {
insertStyles,
getRegisteredStyles,
type EmotionCache,
type SerializedStyles
} from '@emotion/utils'
function insertWithoutScoping(cache, serialized: SerializedStyles) {
if (cache.inserted[serialized.name] === undefined) {
return cache.insert('', serialized, cache.sheet, true)
}
}
function merge(registered: Object, css: (*) => string, className: string) {
const registeredStyles = []
const rawClassName = getRegisteredStyles(
registered,
registeredStyles,
className
)
if (registeredStyles.length < 2) {
return className
}
return rawClassName + css(registeredStyles)
}
export type Interpolation = any
export type Interpolations = Array<Interpolation>
type CreateStyles<ReturnValue> = (...args: Interpolations) => ReturnValue
type ClassNameArg =
| string
| boolean
| { [key: string]: boolean | void | null }
| Array<ClassNameArg>
| void
| null
declare class StyleSheet {
insert(rule: string): void;
flush(): void;
speedy(newVal: boolean): void;
tags: Array<HTMLStyleElement>;
isSpeedy: number;
ctr: number;
}
export type Emotion = {
css: CreateStyles<string>,
cx: (...classNames: Array<ClassNameArg>) => string,
flush: () => void,
hydrate: (ids: Array<string>) => void,
injectGlobal: CreateStyles<void>,
keyframes: CreateStyles<string>,
sheet: StyleSheet,
cache: EmotionCache,
merge: *,
getRegisteredStyles: *
}
let createEmotion = (options: *): Emotion => {
let cache = createCache(options)
// $FlowFixMe
cache.sheet.speedy = function (value: boolean) {
if (process.env.NODE_ENV !== 'production' && this.ctr !== 0) {
throw new Error('speedy must be changed before any rules are inserted')
}
this.isSpeedy = value
}
cache.compat = true
let css = (...args) => {
let serialized = serializeStyles(args, cache.registered, undefined)
insertStyles(cache, serialized, false)
return `${cache.key}-${serialized.name}`
}
let keyframes = (...args) => {
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 = (...args) => {
let serialized = serializeStyles(args, cache.registered)
insertWithoutScoping(cache, serialized)
}
let cx = (...args) => {
return merge(cache.registered, css, classnames(args))
}
return {
css,
cx,
injectGlobal,
keyframes,
hydrate(ids: Array<string>) {
ids.forEach(key => {
cache.inserted[key] = true
})
},
flush() {
cache.registered = {}
cache.inserted = {}
cache.sheet.flush()
},
// $FlowFixMe
sheet: cache.sheet,
cache,
getRegisteredStyles: getRegisteredStyles.bind(null, cache.registered),
merge: merge.bind(null, cache.registered, css)
}
}
let classnames = args => {
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