-
Notifications
You must be signed in to change notification settings - Fork 42
/
utils.ts
458 lines (390 loc) · 17.6 KB
/
utils.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import {
AnimatableElement,
AnimationOptions,
AnimationStatus,
AnimationKeyframes,
CLARITY_MOTION_ENTER_LEAVE_PROPERTY,
CLARITY_MOTION_FALLBACK_DURATION_IN_MS,
CLARITY_MOTION_FALLBACK_EASING,
CLARITY_MOTION_REVERSE_ANIMATION_LABEL,
CLARITY_MOTION_REVERSE_ANIMATION_SUFFIX,
PropertyDrivenAnimation,
TargetedAnimation,
TargetedAnimationAsPropertyTuple,
} from './interfaces.js';
import { LogService } from '../services/log.service.js';
import { ClarityMotion } from './motion.service.js';
import clone from 'ramda/es/clone.js';
import { getCssPropertyValue, isCssPropertyName } from '../utils/css.js';
import { isPrefixedBy, isSuffixedBy, getNumericValueFromCssSecondsStyleValue, removePrefix } from '../utils/string.js';
import { queryChildFromLightOrShadowDom } from '../utils/dom.js';
import { allPropertiesPass } from '../utils/identity.js';
import { getMillisecondsFromSeconds } from '../utils/math.js';
import { PropertyValues } from 'lit';
/**
* runPropertyAnimations() is a utility function called by the @animate(). It is a single point of entry
* to execute the property-driven animations found in components like the accordion and overlay.
*
* Other utilities here are exported to break up the unit tests covering the animation/motion
* library. While they could be called or used in isolation, it is not recommended for most use cases.
*
* runPropertyAnimations returns a boolean so that we know if it ran successfully or not. This is
* mostly to make testing the function easier.
*
* @internal
*/
/* c8 ignore next */
export async function runPropertyAnimations(props: PropertyValues<any>, hostEl: AnimatableElement): Promise<boolean> {
if (!hostEl._animations) {
LogService.warn(`${hostEl.tagName.toLocaleLowerCase()} is trying to animate but no animations are defined.`);
return false;
}
// this runs through animations for each property sequentially
// animations passed into getPropertyAnimations are the Property-Driven Animations
// passed into the @animate decorator
// { hidden: { true: 'cds-modal-enter-reverse', false: 'enter' }, status: { error: 'cds-component-shake' } }
// getPropertyAnimations transforms the @animate decorator config into an array of
// tuples so they are easier to work with.
// ['hidden', { true: 'cds-modal-enter-reverse', false: 'enter' }], ['status', { error: 'cds-component-shake' }]
// * inactive animations are filtered out
const propertyAnimations = getPropertyAnimations(hostEl._animations, props);
const animationsRan = await Promise.all(
propertyAnimations.map((propNameAnimationTuple: TargetedAnimationAsPropertyTuple) => {
const [propname, propertyAnimationsByValue] = propNameAnimationTuple;
if (props.get(propname) === (hostEl as any)[propname]) {
// a weird/unlikely state where an update is sent but the property value didn't actually change
return false;
}
// gets animations to run based on the property's value
const animatedPropertyValueAsString = propertyAnimationsByValue[(hostEl as any)[propname].toString()];
const cdsMotionValue = hostEl.cdsMotion;
// looping through each tuple value in order, getAnimationConfigForPropertyValue()
// returns a tuple with the animation name from Clarity Motion and the TargetedAnimation
// config (also returned from the Clarity Motion service)
// ['enter-reverse', [ { target: '.private-host', animation: [ { opacity: 0 }, { opacity: 1 } ], options: { duration: 300 }}]
const [motionName, returnedMotion] = getAnimationConfigForPropertyValue(
animatedPropertyValueAsString,
getInlineOverride(cdsMotionValue, propname, (hostEl as any)[propname].toString())
);
let motionForMyValue = clone(returnedMotion); // have to jump through this hoop to keep typescript happy
// jumps out if there are no animation routines
if (motionForMyValue.length < 1) {
return false;
}
motionForMyValue = setAnimationConfigOptions(motionName, motionForMyValue, hostEl);
// loops through animation config and runs them; assigns the promises to an array we can Promise.all() at the end
const animations = getAnimationPromiseInstructions(animatedPropertyValueAsString, motionForMyValue, hostEl);
return Promise.all(animations).then(() => {
resolveAnimationEndStatus(animatedPropertyValueAsString, hostEl);
return true;
});
})
)
.then(result => {
return result.indexOf(true) > -1;
})
.catch(() => {
return false;
});
return animationsRan;
}
// --- CONTROL FLOW UTILS ---
/* c8 ignore next */
export function setAnimationStartStatus(animatedPropertyValueAsString: string, hostEl: AnimatableElement) {
// accesses and manipulates the private animation status attr;
if (hostEl.getAttribute('_cds-animation-status') !== AnimationStatus.active) {
hostEl.setAttribute('_cds-animation-status', AnimationStatus.active);
// this is here so we only emit the start event once, right when the whole animation starts...
hostEl.cdsMotionChange.emit(`${animatedPropertyValueAsString} animation ${AnimationStatus.start}`);
}
}
/* c8 ignore next */
export function resolveAnimationEndStatus(animatedPropertyValueAsString: string, hostEl: AnimatableElement) {
// sets super secret animation attr back to 'ready'
hostEl.setAttribute('_cds-animation-status', AnimationStatus.ready); // A
// emits the name of the animation and that it ended
hostEl.cdsMotionChange.emit(`${animatedPropertyValueAsString} animation ${AnimationStatus.end}`); // A
}
/* c8 ignore next */
export function setAnimationConfigOptions(
motionName: string,
motionForMyValue: TargetedAnimation[],
hostEl: AnimatableElement
): TargetedAnimation[] {
const cdsMotionValue = hostEl.cdsMotion;
const animationsAreTurnedOff = !cdsMotionValue || cdsMotionValue === 'off';
// the following set of functions – reverseAnimationConfig(), zeroOutAnimationConfig(),
// setAnimationDuration(), setAnimationEasing() – modify the "options" portion of the animation routine
// in the tuple above that is returned from getAnimationConfigForPropertyValue
if (animationIsReversed(motionName)) {
motionForMyValue = reverseAnimationConfig(motionForMyValue);
}
if (animationsAreTurnedOff) {
// cdsMotion is turned off, so zero out the duration
motionForMyValue = zeroOutAnimationConfig(motionForMyValue);
} else {
// sets duration and easing based on CSS properties, passed values, or a global fallback
motionForMyValue = setAnimationDuration(motionForMyValue, hostEl);
motionForMyValue = setAnimationEasing(motionForMyValue, hostEl);
}
return motionForMyValue;
}
/* c8 ignore next */
export function getAnimationPromiseInstructions(
animatedPropertyValueAsString: string,
motionConfigs: TargetedAnimation[],
hostEl: AnimatableElement
): Promise<unknown>[] {
return motionConfigs
.filter(config => !config.onlyIf || allPropertiesPass(hostEl, config.onlyIf))
.map(config => {
setAnimationStartStatus(animatedPropertyValueAsString, hostEl);
return new Promise(resolve => {
const animationPlayer = getAnimationTarget(hostEl, config.target).animate(
getAnimationKeyframesOrPropertyIndexedFrames(config.animation, hostEl),
config.options || {}
);
const listener = () => {
resolve('animation finished');
animationPlayer.removeEventListener('finish', listener);
};
animationPlayer.addEventListener('finish', listener);
});
});
}
/* c8 ignore next */
export function getAnimationTarget(hostEl: HTMLElement, targetSelector?: string): Element {
return queryChildFromLightOrShadowDom(hostEl, targetSelector) || hostEl;
}
/* c8 ignore next */
export function getAnimationKeyframesOrPropertyIndexedFrames(
animationConfig: AnimationKeyframes | PropertyIndexedKeyframes,
hostEl: HTMLElement
): AnimationKeyframes | PropertyIndexedKeyframes {
return !Array.isArray(animationConfig) ? animationConfig : sizeDimensionKeyframes(animationConfig, hostEl);
}
// --- REVERSING UTILS ---
// string -> boolean
/* c8 ignore next */
export function animationIsReversed(nameOfAnimation: string) {
return isSuffixedBy(nameOfAnimation, CLARITY_MOTION_REVERSE_ANIMATION_SUFFIX);
}
// { target: '.my-selector', animation: [<keyframes>], options: { duration: 500 }} ->
// { target: '.my-selector', animation: [<keyframes>], options: { duration: 500, direction: 'reverse' }}
/* c8 ignore next */
export function reverseAnimationConfig(config: TargetedAnimation[]): TargetedAnimation[] {
return config.map((anim: TargetedAnimation) => {
if (anim.options) {
anim.options.direction = CLARITY_MOTION_REVERSE_ANIMATION_LABEL;
} else {
anim.options = { direction: CLARITY_MOTION_REVERSE_ANIMATION_LABEL };
}
return anim;
});
}
// 'my-animation' -> 'my-animation-reverse'
/* c8 ignore next */
export function reverseAnimation(animationName: string) {
return [animationName, '-', CLARITY_MOTION_REVERSE_ANIMATION_LABEL].join('');
}
// --- ANIMATION CONFIG RETRIEVAL ---
// returns an animation name from a stringified @animate decorator config passed into the
// cds-motion attr. looks it up by property first and value second. returns empty string
// if no animation name matches the property and property value.
/* c8 ignore next */
export function getInlineOverride(cdsMotionValue: string, propName: string, propValue: string): string {
if (!cdsMotionValue || !propName || typeof propValue === 'undefined') {
return '';
}
if (cdsMotionValue === 'on' || cdsMotionValue === 'off') {
return '';
}
let inlineOverride: any | null;
try {
inlineOverride = JSON.parse(cdsMotionValue);
} catch (e) {
return '';
}
const overrideValue = inlineOverride[propName] && inlineOverride[propName][propValue];
return overrideValue ? overrideValue : '';
}
// 'i-am-reverse' -> 'i-am'
/* c8 ignore next */
export function extractAnimationNameIfReversed(name: string): string {
return animationIsReversed(name) ? name.slice(0, -1 * CLARITY_MOTION_REVERSE_ANIMATION_SUFFIX.length) : name;
}
// if the name retrieved from the cds-motion config is present in Clarity Motion, it is returned
/* c8 ignore next */
export function getAnimationFromOverrideOrDecorator(decoratorValue: string, overrideValue?: string) {
if (!overrideValue) {
return decoratorValue;
}
return ClarityMotion.has(extractAnimationNameIfReversed(overrideValue)) ? overrideValue : decoratorValue;
}
// ('animation-name') -> ['animation-name', [ { target: '.an-element', animation: [<keyframes>], options: { ... }}]];
// ('animation-name', 'name-retrieved-from-cds-motion-config') ->
// ['name-retrieved-from-cds-motion-config', [ { target: ..., animation: [<keyframes>], options: { ... }}]];0
/* c8 ignore next */
export function getAnimationConfigForPropertyValue(
nameOfAnimationFromObject: string,
cdsMotionOverride?: string
): [string, TargetedAnimation[]] {
const nameOfAnimation = getAnimationFromOverrideOrDecorator(nameOfAnimationFromObject, cdsMotionOverride);
return [nameOfAnimation, clone(ClarityMotion.get(extractAnimationNameIfReversed(nameOfAnimation)))];
}
// --- SETTING ANIMATION CONFIG VALUES ---
/* c8 ignore next */
export function setAnimationDuration(config: TargetedAnimation[], hostEl: AnimatableElement): TargetedAnimation[] {
return setAnimationProperty('duration', hostEl, config, CLARITY_MOTION_FALLBACK_DURATION_IN_MS, (val: string) => {
return getMillisecondsFromSeconds(getNumericValueFromCssSecondsStyleValue(val));
});
}
/* c8 ignore next */
export function setAnimationEasing(config: TargetedAnimation[], hostEl: AnimatableElement): TargetedAnimation[] {
/* c8 ignore next */
return setAnimationProperty('easing', hostEl, config, CLARITY_MOTION_FALLBACK_EASING);
}
/* c8 ignore next */
export function zeroOutAnimationConfig(config: TargetedAnimation[]): TargetedAnimation[] {
/* c8 ignore next */
return config.map(anim => {
if (anim.options) {
anim.options.duration = 0;
anim.options.easing = CLARITY_MOTION_FALLBACK_EASING;
} else {
anim.options = { duration: 0, easing: CLARITY_MOTION_FALLBACK_EASING };
}
return anim;
});
}
/* c8 ignore next */
export function setAnimationProperty(
propertyName: string,
hostEl: AnimatableElement,
config: TargetedAnimation[],
staticFallback: string | number,
valueConverter?: (arg: string) => string | number
) {
return config.map((anim: TargetedAnimation) => {
if (anim.options) {
if (!(anim.options as AnimationOptions)[propertyName]) {
(anim.options as AnimationOptions)[propertyName] = staticFallback;
} else if (isCssPropertyName((anim.options as { [key: string]: any })[propertyName])) {
const myConfigOption = (anim.options as AnimationOptions)[propertyName];
let valFromProperty: string | number = getCssPropertyValue(myConfigOption as string, hostEl);
if (!valFromProperty) {
valFromProperty = staticFallback;
} else if (valueConverter) {
valFromProperty = valueConverter(valFromProperty);
}
(anim.options as AnimationOptions)[propertyName] = valFromProperty;
}
// else fallthrough not required because option is already set to intended value
} else {
const newConfigOptions: AnimationOptions = {};
newConfigOptions[propertyName] = staticFallback;
anim.options = newConfigOptions;
}
return anim;
});
}
// -- TRANSFORM PROPERTY-DRIVEN ANIMATION OBJECTS TO ANIMATION CONFIG OBJECTS ---
/* c8 ignore next */
export function sizeDimensionKeyframes(
animationKeyframes: AnimationKeyframes,
hostEl: HTMLElement
): AnimationKeyframes {
if (!Array.isArray(animationKeyframes)) {
return animationKeyframes;
}
return animationKeyframes.map((kf: Keyframe) => {
if (Object.prototype.hasOwnProperty.call(kf, 'height') && isPrefixedBy(kf?.height?.toString() || '', 'from:')) {
const selector = removePrefix(kf?.height?.toString() || '', 'from:');
const measureTarget = queryChildFromLightOrShadowDom(hostEl, selector) || null;
kf.height = !measureTarget ? 'auto' : measureTarget.getBoundingClientRect().height + 'px';
}
if (Object.prototype.hasOwnProperty.call(kf, 'width') && isPrefixedBy(kf?.width?.toString() || '', 'from:')) {
const selector = removePrefix(kf?.width?.toString() || '', 'from:');
const measureTarget = queryChildFromLightOrShadowDom(hostEl, selector) || null;
kf.width = !measureTarget ? 'auto' : measureTarget.getBoundingClientRect().width + 'px';
}
return kf;
});
}
/* c8 ignore next */
export function filterAnimationsByUpdatedProperties(
animations: PropertyDrivenAnimation,
updatingProps: PropertyValues<any>
): PropertyDrivenAnimation | null {
if (animations === null || animations === undefined) {
return null;
}
let objectIsEmpty = true;
const returnObject: PropertyDrivenAnimation = {};
Object.getOwnPropertyNames(animations).forEach((prop: string) => {
// test here against undefined value because Lit is running initializations
// with properties with an "undefined" value. as a result of this change,
// animations should only run on explicitly set values...
if (updatingProps.has(prop) && updatingProps.get(prop) !== undefined) {
returnObject[prop] = clone(animations[prop]);
objectIsEmpty = false;
}
});
return objectIsEmpty ? null : returnObject;
}
type TupleOfHiddenAndOtherAnimations = [TargetedAnimationAsPropertyTuple[], TargetedAnimationAsPropertyTuple[]];
/* c8 ignore next */
export function flattenAndSortAnimations(
hiddenAndNotAnimationTuple: TupleOfHiddenAndOtherAnimations,
isHiding: boolean
): TargetedAnimationAsPropertyTuple[] {
if (hiddenAndNotAnimationTuple === null || hiddenAndNotAnimationTuple === undefined) {
return [];
}
const [hiddenAnimations, otherAnimations] = hiddenAndNotAnimationTuple;
if (hiddenAnimations.length > 0) {
if (isHiding) {
// hiding hostEl
return [].concat(otherAnimations as never[], hiddenAnimations as never[]);
} else {
// showing hostEl
return [].concat(hiddenAnimations as never[], otherAnimations as never[]);
}
} else {
return otherAnimations;
}
}
/* c8 ignore next */
export function getHidingAndNonHidingPropertyAnimations(
animations: PropertyDrivenAnimation
): [TargetedAnimationAsPropertyTuple[], TargetedAnimationAsPropertyTuple[]] {
const hiddenAnimations: TargetedAnimationAsPropertyTuple[] = [];
const otherAnimations: TargetedAnimationAsPropertyTuple[] = [];
Object.getOwnPropertyNames(animations || {}).forEach((prop: string) => {
const animationTuple: TargetedAnimationAsPropertyTuple = [prop, clone(animations[prop])];
if (prop === CLARITY_MOTION_ENTER_LEAVE_PROPERTY) {
hiddenAnimations.push(animationTuple);
} else {
otherAnimations.push(animationTuple);
}
});
return [hiddenAnimations, otherAnimations];
}
/* c8 ignore next */
export function getPropertyAnimations(
animations: PropertyDrivenAnimation,
updatingProps: PropertyValues<any>
): TargetedAnimationAsPropertyTuple[] {
const activeAnimations = filterAnimationsByUpdatedProperties(animations || {}, updatingProps);
if (activeAnimations === null) {
return [];
}
const hidingAndOtherAnimationsAsTuple = getHidingAndNonHidingPropertyAnimations(activeAnimations);
const isHiding = updatingProps.get(CLARITY_MOTION_ENTER_LEAVE_PROPERTY) as boolean;
return flattenAndSortAnimations(hidingAndOtherAnimationsAsTuple, isHiding);
}