-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstyles.ts
More file actions
374 lines (341 loc) · 11.2 KB
/
styles.ts
File metadata and controls
374 lines (341 loc) · 11.2 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import type { CSSDirective } from "@microsoft/fast-element";
import type { CSSDesignToken } from "@microsoft/fast-foundation";
import { InteractiveColorRecipe, InteractiveColorRecipeBySet } from "../color/recipe.js";
import { Swatch } from "../color/swatch.js";
import { TypedCSSDesignToken, TypedDesignToken } from "../adaptive-design-tokens.js";
import { InteractiveSet, InteractiveTokenGroup } from "../types.js";
import { createForegroundSet, createForegroundSetBySet } from "../token-helpers-color.js";
import { StyleModuleTarget, StyleProperty, StylePropertyCss } from "./types.js";
/**
* Supported values for a style property.
*
* @public
*/
export type StyleValue = CSSDesignToken<any> | InteractiveSet<any | null> | CSSDirective | string | number;
/**
* An object of style definitions, where the key is the {@link (StylePropertyCss:type)} and the value is the token or final value.
*
* @remarks
* The `Record` format is a convenience for manual authoring of style modules (instead of a `Map`).
*
* @public
*/
export type StyleProperties = Partial<Record<StylePropertyCss, StyleValue>>;
/**
* A `Map` of style definitions, where the key is the {@link (StylePropertyCss:type)} and the value is the token or final value.
*
* @public
*/
export type StylePropertiesMap = Map<StylePropertyCss, StyleValue>;
/**
* The properties and values for a {@link StyleRule} definition.
*
* A declaration should have `styles` and/or `properties` - `styles` are applied before `properties`.
*
* @public
*/
export type StyleDeclaration = {
/**
* The {@link Styles} for this rule.
*
* @remarks
* Optional. If not applicable, provide `properties`.
*/
styles?: Styles | Iterable<Styles>;
/**
* A collection of properties to define a new {@link Styles} or augment those provided as `styles`.
*
* @remarks
* Optional. If not applicable, provide `styles`.
*/
properties?: StyleProperties;
};
/**
* Definition for a single Adaptive UI style rule, which maps to a rule in a normal CSS style sheet.
*
* @public
*/
export type StyleRule = {
/**
* The target for the style rule, used to build the CSS selector.
*
* @remarks
* Optional. If not supplied defaults to the host element for web components.
*/
target?: StyleModuleTarget;
} & StyleDeclaration;
/**
* @public
*/
export const Fill = {
backgroundAndForeground: function(
background: InteractiveTokenGroup<Swatch>,
foregroundRecipe: TypedDesignToken<InteractiveColorRecipe>
): StyleProperties {
return {
backgroundFill: background,
foregroundFill: createForegroundSet(foregroundRecipe, background),
};
},
backgroundAndForegroundBySet: function(
background: InteractiveTokenGroup<Swatch>,
foregroundRecipe: TypedDesignToken<InteractiveColorRecipeBySet>
): StyleProperties {
return {
backgroundFill: background,
foregroundFill: createForegroundSetBySet(foregroundRecipe, background),
};
},
foregroundNonInteractiveWithDisabled: function(
foreground: TypedCSSDesignToken<Swatch>,
disabled: TypedCSSDesignToken<Swatch>,
): StyleProperties {
return {
foregroundFill: {
name: `${foreground.name}-with-disabled-value`,
rest: foreground,
hover: foreground,
active: foreground,
focus: foreground,
disabled,
} as InteractiveTokenGroup<Swatch>,
}
}
}
/**
* @public
*/
export const BorderFill = {
all: function(value: StyleValue): StyleProperties {
return {
borderFillTop: value,
borderFillRight: value,
borderFillBottom: value,
borderFillLeft: value,
};
},
}
/**
* @public
*/
export const BorderThickness = {
all: function(value: StyleValue): StyleProperties {
return {
borderThicknessTop: value,
borderThicknessRight: value,
borderThicknessBottom: value,
borderThicknessLeft: value,
};
},
}
/**
* @public
*/
export const BorderStyle = {
all: function(value: StyleValue): StyleProperties {
return {
borderStyleTop: value,
borderStyleRight: value,
borderStyleBottom: value,
borderStyleLeft: value,
};
},
}
/**
* @public
*/
export const CornerRadius = {
all: function(value: StyleValue): StyleProperties {
return {
cornerRadiusTopLeft: value,
cornerRadiusTopRight: value,
cornerRadiusBottomRight: value,
cornerRadiusBottomLeft: value,
};
},
}
/**
* @public
*/
export const Padding = {
all: function(value: StyleValue): StyleProperties {
return {
paddingTop: value,
paddingRight: value,
paddingBottom: value,
paddingLeft: value,
};
},
verticalHorizontal: function(valueVertical: StyleValue, valueHorizontal: StyleValue): StyleProperties {
return {
paddingTop: valueVertical,
paddingRight: valueHorizontal,
paddingBottom: valueVertical,
paddingLeft: valueHorizontal,
};
},
}
/**
* A modular definition of style properties, either an alias to another style module or a collection of style properties.
*
* @public
*/
export class Styles {
// An array of composed styles
private _composed?: Styles[];
// Individual properties, or additional to composed style properties
private _properties?: StylePropertiesMap;
// Effective properties from composed styles and additional properties
private _composedProperties?: StylePropertiesMap;
// Style overrides for a media query
private _mediaQueryVariations: Map<string, Styles>;
private constructor(
/**
* The style module name.
*/
public readonly name: string | undefined,
propertiesOrStyles: StyleProperties | Styles[]
) {
if (name && Styles.Shared.has(name)) {
throw `Style '${name}' already created. Update it instead.`;
}
if (Array.isArray(propertiesOrStyles)) {
this._composed = propertiesOrStyles;
this.createEffectiveProperties();
} else {
this._properties = new Map();
for (const k in propertiesOrStyles) {
const key: keyof StyleProperties = k as keyof StyleProperties;
const value = propertiesOrStyles[key];
if (value) {
this._properties.set(key, value);
}
}
}
if (name) {
Styles.Shared.set(name, this);
}
}
/**
* Gets the array of composed styles.
*/
public get composed(): Styles[] | undefined {
return this._composed;
}
/**
* Clears the array of composed styles.
*/
public clearComposed(): void {
this._composed = undefined;
this._composedProperties = undefined;
}
public appendComposed(styles: Styles): void {
this._composed?.push(styles);
this.createEffectiveProperties();
}
/**
* The local properties or composition overrides.
*/
public get properties(): StylePropertiesMap | undefined {
return this._properties;
}
public set properties(properties: StylePropertiesMap | undefined) {
this._properties = properties;
this.createEffectiveProperties();
}
/**
* Adds a style variation for a media query like `forced-colors`.
*
* @param query - The media query, see {@link MediaQuery}.
* @param styles - The styles to apply for the provided media query
* @returns The `Styles` definition with media query variation.
*/
public withMediaQuery(query: string, styles: Styles): this {
if (!this._mediaQueryVariations) {
this._mediaQueryVariations = new Map();
}
this._mediaQueryVariations.set(query, styles);
return this;
}
/**
* Gets the media query variations for this style.
*
* @returns The defined media query variations.
*/
public getMediaQueryStyles(): ReadonlyMap<string, Styles> | undefined {
return this._mediaQueryVariations;
}
/**
* Gets the full effective set of properties, from composed styles and local properties as applicable.
*/
public get effectiveProperties(): StylePropertiesMap {
if (this._composedProperties) {
return this._composedProperties;
} else if (this._properties) {
return this._properties;
} else {
return new Map();
}
}
/**
* Gets the set of effective properties that support Adaptive UI design-to-code.
*/
public get effectiveAdaptiveProperties(): Map<StyleProperty, StyleValue> {
const entries = [...this.effectiveProperties];
const filtered = entries.filter(([styleProperty]) => { return styleProperty in StyleProperty}) as [StyleProperty, StyleValue][];
return new Map(filtered);
}
private createEffectiveProperties() {
if (this._composed) {
const map: StylePropertiesMap = new Map();
this._composed.forEach((styles: Styles) => {
styles.effectiveProperties.forEach((value, target) => {
map.set(target, value);
});
});
this._properties?.forEach((value, target) => {
map.set(target, value);
});
this._composedProperties = map;
}
}
public static Shared: Map<string, Styles> = new Map();
/**
* Creates a new Styles object for the composed styles.
*
* @param styles - An array of styles to compose.
* @param properties - Individual properties to append to the styles.
* @param name - A name for the styles used for lookup.
* @returns A new Styles object representing the composed styles.
*/
public static compose(styles: Styles[], properties?: StyleProperties, name?: string): Styles {
if (properties) {
styles.push(Styles.fromProperties(properties));
}
return new Styles(name, styles);
}
/**
* Creates a new Styles object for the individual properties.
*
* @param properties - Individual properties for the new style module.
* @param name - A name for the styles used for lookup.
* @returns A new Styles object representing the properties.
*/
public static fromProperties(properties: StyleProperties, name?: string): Styles {
return new Styles(name, properties);
}
/**
* Creates a new Styles object for the declared styles.
*
* @param declaration - The style declaration
* @param name - A name for the styles used for lookup.
* @returns A new Styles object representing the declared styles.
*/
public static fromDeclaration(declaration: StyleDeclaration, name?: string) {
const styles: Array<Styles> = declaration.styles ?
(Array.isArray(declaration.styles) ? declaration.styles : [declaration.styles]) :
[];
const composed = Styles.compose(styles, declaration.properties, name);
return composed;
}
}