|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +export interface LongestAnimation { |
| 10 | + animationName: string | undefined; |
| 11 | + propertyName: string | undefined; |
| 12 | + duration: number; |
| 13 | +} |
| 14 | + |
| 15 | +/** Parses a CSS time value to milliseconds. */ |
| 16 | +function parseCssTimeUnitsToMs(value: string): number { |
| 17 | + // Some browsers will return it in seconds, whereas others will return milliseconds. |
| 18 | + const multiplier = value.toLowerCase().indexOf('ms') > -1 ? 1 : 1000; |
| 19 | + return parseFloat(value) * multiplier; |
| 20 | +} |
| 21 | + |
| 22 | +/** Parses out multiple values from a computed style into an array. */ |
| 23 | +function parseCssPropertyValue(computedStyle: CSSStyleDeclaration, name: string): string[] { |
| 24 | + const value = computedStyle.getPropertyValue(name); |
| 25 | + return value.split(',').map((part) => part.trim()); |
| 26 | +} |
| 27 | + |
| 28 | +/** Gets the transform transition duration, including the delay, of an element in milliseconds. */ |
| 29 | +function getLongestComputedTransition(computedStyle: CSSStyleDeclaration): LongestAnimation { |
| 30 | + const transitionedProperties = parseCssPropertyValue(computedStyle, 'transition-property'); |
| 31 | + const rawDurations = parseCssPropertyValue(computedStyle, 'transition-duration'); |
| 32 | + const rawDelays = parseCssPropertyValue(computedStyle, 'transition-delay'); |
| 33 | + const longest = {propertyName: '', duration: 0, animationName: undefined}; |
| 34 | + for (let i = 0; i < transitionedProperties.length; i++) { |
| 35 | + const duration = parseCssTimeUnitsToMs(rawDelays[i]) + parseCssTimeUnitsToMs(rawDurations[i]); |
| 36 | + if (duration > longest.duration) { |
| 37 | + longest.propertyName = transitionedProperties[i]; |
| 38 | + longest.duration = duration; |
| 39 | + } |
| 40 | + } |
| 41 | + return longest; |
| 42 | +} |
| 43 | + |
| 44 | +function getLongestComputedAnimation(computedStyle: CSSStyleDeclaration): LongestAnimation { |
| 45 | + const rawNames = parseCssPropertyValue(computedStyle, 'animation-name'); |
| 46 | + const rawDelays = parseCssPropertyValue(computedStyle, 'animation-delay'); |
| 47 | + const rawDurations = parseCssPropertyValue(computedStyle, 'animation-duration'); |
| 48 | + const longest: LongestAnimation = {animationName: '', propertyName: undefined, duration: 0}; |
| 49 | + for (let i = 0; i < rawNames.length; i++) { |
| 50 | + const duration = parseCssTimeUnitsToMs(rawDelays[i]) + parseCssTimeUnitsToMs(rawDurations[i]); |
| 51 | + if (duration > longest.duration) { |
| 52 | + longest.animationName = rawNames[i]; |
| 53 | + longest.duration = duration; |
| 54 | + } |
| 55 | + } |
| 56 | + return longest; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Determines the longest animation, but with `getComputedStyles` instead of `getAnimations`. This |
| 61 | + * is ultimately safer than getAnimations because it can be used when recalculations are in |
| 62 | + * progress. `getAnimations()` will be empty in that case. |
| 63 | + */ |
| 64 | +function determineLongestAnimationFromComputedStyles( |
| 65 | + el: HTMLElement, |
| 66 | + animationsMap: WeakMap<HTMLElement, LongestAnimation>, |
| 67 | +): void { |
| 68 | + const computedStyle = getComputedStyle(el); |
| 69 | + |
| 70 | + const longestAnimation = getLongestComputedAnimation(computedStyle); |
| 71 | + const longestTransition = getLongestComputedTransition(computedStyle); |
| 72 | + |
| 73 | + const longest = |
| 74 | + longestAnimation.duration > longestTransition.duration ? longestAnimation : longestTransition; |
| 75 | + if (animationsMap.has(el) && animationsMap.get(el)!.duration > longest.duration) { |
| 76 | + return; |
| 77 | + } |
| 78 | + animationsMap.set(el, longest); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Multiple animations can be set on an element. This grabs an element and |
| 83 | + * determines which of those will be the longest duration. If we didn't do |
| 84 | + * this, elements would be removed whenever the first animation completes. |
| 85 | + * This ensures we get the longest running animation and only remove when |
| 86 | + * that animation completes. |
| 87 | + */ |
| 88 | +export function determineLongestAnimation( |
| 89 | + event: AnimationEvent | TransitionEvent, |
| 90 | + el: HTMLElement, |
| 91 | + animationsMap: WeakMap<HTMLElement, LongestAnimation>, |
| 92 | + areAnimationSupported: boolean, |
| 93 | +): void { |
| 94 | + if (!areAnimationSupported || !(event.target instanceof Element) || event.target !== el) return; |
| 95 | + const animations = el.getAnimations(); |
| 96 | + return animations.length === 0 |
| 97 | + ? // fallback to computed styles if getAnimations is empty. This would happen if styles are |
| 98 | + // currently recalculating due to a reflow happening elsewhere. |
| 99 | + determineLongestAnimationFromComputedStyles(el, animationsMap) |
| 100 | + : determineLongestAnimationFromElementAnimations(el, animationsMap, animations); |
| 101 | +} |
| 102 | + |
| 103 | +function determineLongestAnimationFromElementAnimations( |
| 104 | + el: HTMLElement, |
| 105 | + animationsMap: WeakMap<HTMLElement, LongestAnimation>, |
| 106 | + animations: Animation[], |
| 107 | +): void { |
| 108 | + let currentLongest: LongestAnimation = { |
| 109 | + animationName: undefined, |
| 110 | + propertyName: undefined, |
| 111 | + duration: 0, |
| 112 | + }; |
| 113 | + for (const animation of animations) { |
| 114 | + const timing = animation.effect?.getTiming(); |
| 115 | + // duration can be a string 'auto' or a number. |
| 116 | + const animDuration = typeof timing?.duration === 'number' ? timing.duration : 0; |
| 117 | + let duration = (timing?.delay ?? 0) + animDuration; |
| 118 | + |
| 119 | + let propertyName: string | undefined; |
| 120 | + let animationName: string | undefined; |
| 121 | + |
| 122 | + if ((animation as CSSAnimation).animationName) { |
| 123 | + animationName = (animation as CSSAnimation).animationName; |
| 124 | + } else { |
| 125 | + // Check for CSSTransition specific property |
| 126 | + propertyName = (animation as CSSTransition).transitionProperty; |
| 127 | + } |
| 128 | + |
| 129 | + if (duration >= currentLongest.duration) { |
| 130 | + currentLongest = {animationName, propertyName, duration}; |
| 131 | + } |
| 132 | + } |
| 133 | + if (animationsMap.has(el) && animationsMap.get(el)!.duration > currentLongest.duration) { |
| 134 | + return; |
| 135 | + } |
| 136 | + animationsMap.set(el, currentLongest); |
| 137 | +} |
0 commit comments