-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
mergeStyleSets.ts
142 lines (124 loc) · 5.69 KB
/
mergeStyleSets.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
import { extractStyleParts } from './extractStyleParts';
import { concatStyleSets } from './concatStyleSets';
import { IStyle } from './IStyle';
import { styleToRegistration, applyRegistration } from './styleToClassName';
import { IStyleSet, IProcessedStyleSet, IConcatenatedStyleSet } from './IStyleSet';
/**
* Takes in one or more style set objects, each consisting of a set of areas,
* each which will produce a class name. Using this is analogous to calling
* `mergeStyles` for each property in the object, but ensures we maintain the
* set ordering when multiple style sets are merged.
*
* @param styleSet - The first style set to be merged and reigstered.
*/
export function mergeStyleSets<TStyleSet extends IStyleSet<TStyleSet>>(
styleSet: TStyleSet | false | null | undefined
): IProcessedStyleSet<TStyleSet>;
/**
* Takes in one or more style set objects, each consisting of a set of areas,
* each which will produce a class name. Using this is analogous to calling
* `mergeStyles` for each property in the object, but ensures we maintain the
* set ordering when multiple style sets are merged.
*
* @param styleSet1 - The first style set to be merged.
* @param styleSet2 - The second style set to be merged.
*/
export function mergeStyleSets<TStyleSet1 extends IStyleSet<TStyleSet1>, TStyleSet2 extends IStyleSet<TStyleSet2>>(
styleSet1: TStyleSet1 | false | null | undefined,
styleSet2: TStyleSet2 | false | null | undefined
): IProcessedStyleSet<TStyleSet1 & TStyleSet2>;
/**
* Takes in one or more style set objects, each consisting of a set of areas,
* each which will produce a class name. Using this is analogous to calling
* `mergeStyles` for each property in the object, but ensures we maintain the
* set ordering when multiple style sets are merged.
*
* @param styleSet1 - The first style set to be merged.
* @param styleSet2 - The second style set to be merged.
* @param styleSet3 - The third style set to be merged.
*/
export function mergeStyleSets<
TStyleSet1 extends IStyleSet<TStyleSet1>,
TStyleSet2 extends IStyleSet<TStyleSet2>,
TStyleSet3 extends IStyleSet<TStyleSet3>
>(
styleSet1: TStyleSet1 | false | null | undefined,
styleSet2: TStyleSet2 | false | null | undefined,
styleSet3: TStyleSet3 | false | null | undefined
): IProcessedStyleSet<TStyleSet1 & TStyleSet2 & TStyleSet3>;
/**
* Takes in one or more style set objects, each consisting of a set of areas,
* each which will produce a class name. Using this is analogous to calling
* `mergeStyles` for each property in the object, but ensures we maintain the
* set ordering when multiple style sets are merged.
*
* @param styleSet1 - The first style set to be merged.
* @param styleSet2 - The second style set to be merged.
* @param styleSet3 - The third style set to be merged.
* @param styleSet4 - The fourth style set to be merged.
*/
export function mergeStyleSets<
TStyleSet1 extends IStyleSet<TStyleSet1>,
TStyleSet2 extends IStyleSet<TStyleSet2>,
TStyleSet3 extends IStyleSet<TStyleSet3>,
TStyleSet4 extends IStyleSet<TStyleSet4>
>(
styleSet1: TStyleSet1 | false | null | undefined,
styleSet2: TStyleSet2 | false | null | undefined,
styleSet3: TStyleSet3 | false | null | undefined,
styleSet4: TStyleSet4 | false | null | undefined
): IProcessedStyleSet<TStyleSet1 & TStyleSet2 & TStyleSet3 & TStyleSet4>;
/**
* Takes in one or more style set objects, each consisting of a set of areas,
* each which will produce a class name. Using this is analogous to calling
* `mergeStyles` for each property in the object, but ensures we maintain the
* set ordering when multiple style sets are merged.
*
* @param styleSets - One or more style sets to be merged.
*/
export function mergeStyleSets(...styleSets: Array<IStyleSet<any> | undefined | false | null>): IProcessedStyleSet<any>;
/**
* Takes in one or more style set objects, each consisting of a set of areas,
* each which will produce a class name. Using this is analogous to calling
* `mergeStyles` for each property in the object, but ensures we maintain the
* set ordering when multiple style sets are merged.
*
* @param styleSets - One or more style sets to be merged.
*/
export function mergeStyleSets(...styleSets: Array<IStyleSet<any> | undefined | false | null>): IProcessedStyleSet<any> {
// tslint:disable-next-line:no-any
const classNameSet: IProcessedStyleSet<any> = { subComponentStyles: {} };
const classMap: { [key: string]: string } = {};
const styleSet = styleSets[0];
if (!styleSet && styleSets.length <= 1) {
return { subComponentStyles: {} };
}
let concatenatedStyleSet: IConcatenatedStyleSet<any> | IStyleSet<any> =
// we have guarded against falsey values just above.
styleSet!;
concatenatedStyleSet = concatStyleSets(...styleSets);
const registrations = [];
for (const styleSetArea in concatenatedStyleSet) {
if (concatenatedStyleSet.hasOwnProperty(styleSetArea)) {
if (styleSetArea === 'subComponentStyles') {
classNameSet.subComponentStyles = (concatenatedStyleSet as IConcatenatedStyleSet<any>).subComponentStyles || {};
continue;
}
const styles: IStyle = (concatenatedStyleSet as any)[styleSetArea];
const { classes, objects } = extractStyleParts(styles);
const registration = styleToRegistration({ displayName: styleSetArea }, objects);
registrations.push(registration);
if (registration) {
classMap[styleSetArea] = registration.className;
// as any cast not needed in ts >=2.9
(classNameSet as any)[styleSetArea] = classes.concat([registration.className]).join(' ');
}
}
}
for (const registration of registrations) {
if (registration) {
applyRegistration(registration, classMap);
}
}
return classNameSet;
}