Skip to content

Commit 79d334b

Browse files
dario-piotrowiczalxhub
authored andcommitted
feat(animations): provide warnings for non-animatable CSS properties (#45212)
warn developers when they are trying to animate non-animatable CSS properties so that can more easily understand why something is not being animated as they would expect it to resolves #27577 PR Close #45212
1 parent a049840 commit 79d334b

12 files changed

Lines changed: 329 additions & 10 deletions

File tree

‎goldens/public-api/animations/browser/browser.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export abstract class AnimationDriver {
2020
// (undocumented)
2121
abstract query(element: any, selector: string, multi: boolean): any[];
2222
// (undocumented)
23+
abstract validateAnimatableStyleProperty?: (prop: string) => boolean;
24+
// (undocumented)
2325
abstract validateStyleProperty(prop: string): boolean;
2426
}
2527

‎goldens/public-api/animations/browser/testing/testing.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class MockAnimationDriver implements AnimationDriver {
2626
// (undocumented)
2727
query(element: any, selector: string, multi: boolean): any[];
2828
// (undocumented)
29+
validateAnimatableStyleProperty(prop: string): boolean;
30+
// (undocumented)
2931
validateStyleProperty(prop: string): boolean;
3032
}
3133

‎goldens/size-tracking/aio-payloads.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"master": {
1616
"uncompressed": {
1717
"runtime": 4343,
18-
"main": 450268,
18+
"main": 450342,
1919
"polyfills": 33869,
2020
"styles": 70416,
2121
"light-theme": 77582,

‎packages/animations/browser/src/dsl/animation_ast_builder.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {invalidDefinition, invalidKeyframes, invalidOffset, invalidParallelAnima
1111
import {AnimationDriver} from '../render/animation_driver';
1212
import {getOrSetDefaultValue} from '../render/shared';
1313
import {convertToMap, copyObj, extractStyleParams, iteratorToArray, NG_ANIMATING_SELECTOR, NG_TRIGGER_SELECTOR, normalizeAnimationEntry, resolveTiming, SUBSTITUTION_EXPR_START, validateStyleParams, visitDslNode} from '../util';
14-
import {pushUnrecognizedPropertiesWarning} from '../warning_helpers';
14+
import {pushNonAnimatablePropertiesWarning, pushUnrecognizedPropertiesWarning} from '../warning_helpers';
1515

1616
import {AnimateAst, AnimateChildAst, AnimateRefAst, Ast, DynamicTimingAst, GroupAst, KeyframesAst, QueryAst, ReferenceAst, SequenceAst, StaggerAst, StateAst, StyleAst, TimingAst, TransitionAst, TriggerAst} from './animation_ast';
1717
import {AnimationDslVisitor} from './animation_dsl_visitor';
@@ -80,6 +80,15 @@ export class AnimationAstBuilderVisitor implements AnimationDslVisitor {
8080
[...context.unsupportedCSSPropertiesFound.keys()],
8181
);
8282
}
83+
84+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
85+
context.nonAnimatableCSSPropertiesFound.size) {
86+
pushNonAnimatablePropertiesWarning(
87+
warnings,
88+
[...context.nonAnimatableCSSPropertiesFound.keys()],
89+
);
90+
}
91+
8392
return ast;
8493
}
8594

@@ -311,6 +320,16 @@ export class AnimationAstBuilderVisitor implements AnimationDslVisitor {
311320
return;
312321
}
313322

323+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
324+
this._driver.validateAnimatableStyleProperty) {
325+
if (!this._driver.validateAnimatableStyleProperty(prop)) {
326+
context.nonAnimatableCSSPropertiesFound.add(prop);
327+
// note: non animatable properties are not removed for the tuple just in case they are
328+
// categorized as non animatable but can actually be animated
329+
return;
330+
}
331+
}
332+
314333
// This is guaranteed to have a defined Map at this querySelector location making it
315334
// safe to add the assertion here. It is set as a default empty map in prior methods.
316335
const collectedStyles = context.collectedStyles.get(context.currentQuerySelector!)!;
@@ -514,6 +533,7 @@ export class AnimationAstBuilderContext {
514533
public collectedStyles = new Map<string, Map<string, StyleTimeTuple>>();
515534
public options: AnimationOptions|null = null;
516535
public unsupportedCSSPropertiesFound: Set<string> = new Set<string>();
536+
public readonly nonAnimatableCSSPropertiesFound: Set<string> = new Set<string>();
517537
constructor(public errors: Error[]) {}
518538
}
519539

‎packages/animations/browser/src/render/animation_driver.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export abstract class AnimationDriver {
5656

5757
abstract validateStyleProperty(prop: string): boolean;
5858

59+
abstract validateAnimatableStyleProperty?: (prop: string) => boolean;
60+
5961
/**
6062
* @deprecated No longer in use. Will be removed.
6163
*/

‎packages/animations/browser/src/render/shared.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {AnimationStyleNormalizer} from '../../src/dsl/style_normalization/animat
1111
import {AnimationDriver} from '../../src/render/animation_driver';
1212
import {animationFailed} from '../error_helpers';
1313

14+
import {ANIMATABLE_PROP_SET} from './web_animations/animatable_props_set';
15+
1416
// We don't include ambient node types here since @angular/animations/browser
1517
// is meant to target the browser so technically it should not depend on node
1618
// types. `process` is just declared locally here as a result.
@@ -209,6 +211,10 @@ export function validateStyleProperty(prop: string): boolean {
209211
return result;
210212
}
211213

214+
export function validateWebAnimatableStyleProperty(prop: string): boolean {
215+
return ANIMATABLE_PROP_SET.has(prop);
216+
}
217+
212218
export function getBodyNode(): any|null {
213219
if (typeof document != 'undefined') {
214220
return document.body;
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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.io/license
7+
*/
8+
9+
/**
10+
* Set of all animatable CSS properties
11+
*
12+
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
13+
*/
14+
export const ANIMATABLE_PROP_SET = new Set([
15+
'-moz-outline-radius',
16+
'-moz-outline-radius-bottomleft',
17+
'-moz-outline-radius-bottomright',
18+
'-moz-outline-radius-topleft',
19+
'-moz-outline-radius-topright',
20+
'-ms-grid-columns',
21+
'-ms-grid-rows',
22+
'-webkit-line-clamp',
23+
'-webkit-text-fill-color',
24+
'-webkit-text-stroke',
25+
'-webkit-text-stroke-color',
26+
'accent-color',
27+
'all',
28+
'backdrop-filter',
29+
'background',
30+
'background-color',
31+
'background-position',
32+
'background-size',
33+
'block-size',
34+
'border',
35+
'border-block-end',
36+
'border-block-end-color',
37+
'border-block-end-width',
38+
'border-block-start',
39+
'border-block-start-color',
40+
'border-block-start-width',
41+
'border-bottom',
42+
'border-bottom-color',
43+
'border-bottom-left-radius',
44+
'border-bottom-right-radius',
45+
'border-bottom-width',
46+
'border-color',
47+
'border-end-end-radius',
48+
'border-end-start-radius',
49+
'border-image-outset',
50+
'border-image-slice',
51+
'border-image-width',
52+
'border-inline-end',
53+
'border-inline-end-color',
54+
'border-inline-end-width',
55+
'border-inline-start',
56+
'border-inline-start-color',
57+
'border-inline-start-width',
58+
'border-left',
59+
'border-left-color',
60+
'border-left-width',
61+
'border-radius',
62+
'border-right',
63+
'border-right-color',
64+
'border-right-width',
65+
'border-start-end-radius',
66+
'border-start-start-radius',
67+
'border-top',
68+
'border-top-color',
69+
'border-top-left-radius',
70+
'border-top-right-radius',
71+
'border-top-width',
72+
'border-width',
73+
'bottom',
74+
'box-shadow',
75+
'caret-color',
76+
'clip',
77+
'clip-path',
78+
'color',
79+
'column-count',
80+
'column-gap',
81+
'column-rule',
82+
'column-rule-color',
83+
'column-rule-width',
84+
'column-width',
85+
'columns',
86+
'filter',
87+
'flex',
88+
'flex-basis',
89+
'flex-grow',
90+
'flex-shrink',
91+
'font',
92+
'font-size',
93+
'font-size-adjust',
94+
'font-stretch',
95+
'font-variation-settings',
96+
'font-weight',
97+
'gap',
98+
'grid-column-gap',
99+
'grid-gap',
100+
'grid-row-gap',
101+
'grid-template-columns',
102+
'grid-template-rows',
103+
'height',
104+
'inline-size',
105+
'input-security',
106+
'inset',
107+
'inset-block',
108+
'inset-block-end',
109+
'inset-block-start',
110+
'inset-inline',
111+
'inset-inline-end',
112+
'inset-inline-start',
113+
'left',
114+
'letter-spacing',
115+
'line-clamp',
116+
'line-height',
117+
'margin',
118+
'margin-block-end',
119+
'margin-block-start',
120+
'margin-bottom',
121+
'margin-inline-end',
122+
'margin-inline-start',
123+
'margin-left',
124+
'margin-right',
125+
'margin-top',
126+
'mask',
127+
'mask-border',
128+
'mask-position',
129+
'mask-size',
130+
'max-block-size',
131+
'max-height',
132+
'max-inline-size',
133+
'max-lines',
134+
'max-width',
135+
'min-block-size',
136+
'min-height',
137+
'min-inline-size',
138+
'min-width',
139+
'object-position',
140+
'offset',
141+
'offset-anchor',
142+
'offset-distance',
143+
'offset-path',
144+
'offset-position',
145+
'offset-rotate',
146+
'opacity',
147+
'order',
148+
'outline',
149+
'outline-color',
150+
'outline-offset',
151+
'outline-width',
152+
'padding',
153+
'padding-block-end',
154+
'padding-block-start',
155+
'padding-bottom',
156+
'padding-inline-end',
157+
'padding-inline-start',
158+
'padding-left',
159+
'padding-right',
160+
'padding-top',
161+
'perspective',
162+
'perspective-origin',
163+
'right',
164+
'rotate',
165+
'row-gap',
166+
'scale',
167+
'scroll-margin',
168+
'scroll-margin-block',
169+
'scroll-margin-block-end',
170+
'scroll-margin-block-start',
171+
'scroll-margin-bottom',
172+
'scroll-margin-inline',
173+
'scroll-margin-inline-end',
174+
'scroll-margin-inline-start',
175+
'scroll-margin-left',
176+
'scroll-margin-right',
177+
'scroll-margin-top',
178+
'scroll-padding',
179+
'scroll-padding-block',
180+
'scroll-padding-block-end',
181+
'scroll-padding-block-start',
182+
'scroll-padding-bottom',
183+
'scroll-padding-inline',
184+
'scroll-padding-inline-end',
185+
'scroll-padding-inline-start',
186+
'scroll-padding-left',
187+
'scroll-padding-right',
188+
'scroll-padding-top',
189+
'scroll-snap-coordinate',
190+
'scroll-snap-destination',
191+
'scrollbar-color',
192+
'shape-image-threshold',
193+
'shape-margin',
194+
'shape-outside',
195+
'tab-size',
196+
'text-decoration',
197+
'text-decoration-color',
198+
'text-decoration-thickness',
199+
'text-emphasis',
200+
'text-emphasis-color',
201+
'text-indent',
202+
'text-shadow',
203+
'text-underline-offset',
204+
'top',
205+
'transform',
206+
'transform-origin',
207+
'translate',
208+
'vertical-align',
209+
'visibility',
210+
'width',
211+
'word-spacing',
212+
'z-index',
213+
'zoom',
214+
]);

‎packages/animations/browser/src/render/web_animations/web_animations_driver.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88
import {AnimationPlayer, ɵStyleDataMap} from '@angular/animations';
99

10-
import {allowPreviousPlayerStylesMerge, balancePreviousStylesIntoKeyframes, copyStyles, normalizeKeyframes} from '../../util';
10+
import {allowPreviousPlayerStylesMerge, balancePreviousStylesIntoKeyframes, camelCaseToDashCase, copyStyles, normalizeKeyframes} from '../../util';
1111
import {AnimationDriver} from '../animation_driver';
12-
import {containsElement, getParentElement, invokeQuery, validateStyleProperty} from '../shared';
12+
import {containsElement, getParentElement, invokeQuery, validateStyleProperty, validateWebAnimatableStyleProperty} from '../shared';
1313
import {packageNonAnimatableStyles} from '../special_cased_styles';
1414

1515
import {WebAnimationsPlayer} from './web_animations_player';
@@ -19,6 +19,15 @@ export class WebAnimationsDriver implements AnimationDriver {
1919
return validateStyleProperty(prop);
2020
}
2121

22+
validateAnimatableStyleProperty(prop: string): boolean {
23+
// Perform actual validation in dev mode only, in prod mode this check is a noop.
24+
if (ngDevMode) {
25+
const cssProp = camelCaseToDashCase(prop);
26+
return validateWebAnimatableStyleProperty(cssProp);
27+
}
28+
return true;
29+
}
30+
2231
matchesElement(_element: any, _selector: string): boolean {
2332
// This method is deprecated and no longer in use so we return false.
2433
return false;

‎packages/animations/browser/src/util.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export function dashCaseToCamelCase(input: string): string {
275275
return input.replace(DASH_CASE_REGEXP, (...m: any[]) => m[1].toUpperCase());
276276
}
277277

278-
function camelCaseToDashCase(input: string): string {
278+
export function camelCaseToDashCase(input: string): string {
279279
return input.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
280280
}
281281

‎packages/animations/browser/src/warning_helpers.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ export function triggerParsingWarnings(name: string, warnings: string[]): void {
3636

3737
export function pushUnrecognizedPropertiesWarning(warnings: string[], props: string[]): void {
3838
if (ngDevMode && props.length) {
39-
warnings.push(
40-
`The provided CSS properties are not recognized properties supported for animations: ${
41-
props.join(', ')}`);
39+
warnings.push(`The following provided properties are not recognized: ${props.join(', ')}`);
40+
}
41+
}
42+
43+
export function pushNonAnimatablePropertiesWarning(warnings: string[], props: string[]): void {
44+
if (props.length) {
45+
warnings.push(`The following provided properties are not animatable: ${
46+
props.join(
47+
', ')}\n (see: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)`);
4248
}
4349
}

0 commit comments

Comments
 (0)