Skip to content

Commit fc8247d

Browse files
thePunderWomanNothingEverHappens
authored andcommitted
refactor(core): add compiler support for animation instructions (#62528)
this adds the compiler code to support the animate instructions. PR Close #62528
1 parent e25e634 commit fc8247d

65 files changed

Lines changed: 1924 additions & 148 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/compiler-cli/src/ngtsc/typecheck/extended/checks/interpolated_signal_not_invoked/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class InterpolatedSignalCheck extends TemplateCheckWithVisitor<ErrorCode.INTERPO
7171
node.type === BindingType.Style ||
7272
// or an attribute binding like `[attr.role]="mySignal"`
7373
node.type === BindingType.Attribute ||
74+
// or an animation binding like `[animate.enter]="mySignal"`
75+
node.type === BindingType.Animation ||
7476
// or an animation binding like `[@myAnimation]="mySignal"`
7577
node.type === BindingType.LegacyAnimation) &&
7678
nodeAst

‎packages/compiler-cli/src/ngtsc/typecheck/src/host_bindings.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ function createNodeFromHostLiteralProperty(
192192
const {attrName, type} = inferBoundAttribute(name.text.slice(1, -1));
193193
const valueSpan = createStaticExpressionSpan(initializer);
194194
const ast = parser.parseBinding(initializer.text, true, valueSpan, valueSpan.start.offset);
195-
196195
if (ast.errors.length > 0) {
197196
return; // See TODO above.
198197
}
@@ -213,6 +212,7 @@ function createNodeFromHostLiteralProperty(
213212
);
214213
} else if (name.text.startsWith('(') && name.text.endsWith(')')) {
215214
const events: ParsedEvent[] = [];
215+
216216
parser.parseEvent(
217217
name.text.slice(1, -1),
218218
initializer.text,
@@ -428,7 +428,8 @@ function inferBoundAttribute(name: string): {attrName: string; type: BindingType
428428
const attrPrefix = 'attr.';
429429
const classPrefix = 'class.';
430430
const stylePrefix = 'style.';
431-
const animationPrefix = '@';
431+
const animationPrefix = 'animate.';
432+
const legacyAnimationPrefix = '@';
432433
let attrName: string;
433434
let type: BindingType;
434435

@@ -443,7 +444,10 @@ function inferBoundAttribute(name: string): {attrName: string; type: BindingType
443444
attrName = name.slice(stylePrefix.length);
444445
type = BindingType.Style;
445446
} else if (name.startsWith(animationPrefix)) {
446-
attrName = name.slice(animationPrefix.length);
447+
attrName = name;
448+
type = BindingType.Animation;
449+
} else if (name.startsWith(legacyAnimationPrefix)) {
450+
attrName = name.slice(legacyAnimationPrefix.length);
447451
type = BindingType.LegacyAnimation;
448452
} else {
449453
attrName = name;

‎packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,14 @@ class TcbUnclaimedOutputsOp extends TcbOp {
16341634
? this.tcb.env.referenceExternalType('@angular/animations', 'AnimationEvent')
16351635
: EventParamType.Any;
16361636

1637+
const handler = tcbCreateEventHandler(output, this.tcb, this.scope, eventType);
1638+
this.scope.addStatement(ts.factory.createExpressionStatement(handler));
1639+
} else if (output.type === ParsedEventType.Animation) {
1640+
const eventType = this.tcb.env.referenceExternalType(
1641+
'@angular/core',
1642+
'AnimationCallbackEvent',
1643+
);
1644+
16371645
const handler = tcbCreateEventHandler(output, this.tcb, this.scope, eventType);
16381646
this.scope.addStatement(ts.factory.createExpressionStatement(handler));
16391647
} else if (this.tcb.env.config.checkTypeOfDomEvents) {

‎packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ describe('type check blocks', () => {
2121
expect(tcb('{{hello}} {{world}}')).toContain('"" + (((this).hello)) + (((this).world));');
2222
});
2323

24+
it('should generate an animation in function call', () => {
25+
const TEMPLATE = '<p (animate.enter)="animateFn($event)"></p>';
26+
const results = tcb(TEMPLATE);
27+
expect(results).toContain(
28+
'($event: i1.AnimationCallbackEvent): any => { (this).animateFn($event); };',
29+
);
30+
});
31+
2432
it('should generate literal map expressions', () => {
2533
const TEMPLATE = '{{ method({foo: a, bar: b}) }}';
2634
expect(tcb(TEMPLATE)).toContain('(this).method({ "foo": ((this).a), "bar": ((this).b) })');

‎packages/compiler-cli/test/compliance/test_cases/r3_view_compiler/animations/GOLDEN_PARTIAL.js‎

Lines changed: 282 additions & 51 deletions
Large diffs are not rendered by default.

‎packages/compiler-cli/test/compliance/test_cases/r3_view_compiler/animations/TEST_CASES.json‎

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,140 @@
22
"$schema": "../../test_case_schema.json",
33
"cases": [
44
{
5-
"description": "should not register any @attr attributes as static attributes",
5+
"description": "should generate animate enter instructions on element with a simple string",
66
"inputFiles": [
7-
"static_animation_attribute.ts"
7+
"animate_enter_with_string.ts"
88
],
99
"expectations": [
1010
{
1111
"files": [
1212
{
13-
"expected": "static_animation_attribute_template.js",
14-
"generated": "static_animation_attribute.js"
13+
"expected": "animate_enter_with_string_template.js",
14+
"generated": "animate_enter_with_string.js"
1515
}
1616
],
17-
"failureMessage": "Incorrect initialization attributes"
17+
"failureMessage": "Incorrect ɵɵanimateEnter() call"
1818
}
1919
]
2020
},
2121
{
22-
"description": "should dedup multiple [@event] listeners",
22+
"description": "should generate animate enter instructions with host binding and simple string",
2323
"inputFiles": [
24-
"duplicate_animation_listeners.ts"
24+
"animate_enter_with_string_host_bindings.ts"
2525
],
2626
"expectations": [
2727
{
2828
"files": [
2929
{
30-
"expected": "duplicate_animation_listeners_template.js",
31-
"generated": "duplicate_animation_listeners.js"
30+
"expected": "animate_enter_with_string_host_bindings_template.js",
31+
"generated": "animate_enter_with_string_host_bindings.js"
3232
}
3333
],
34-
"failureMessage": "Incorrect initialization attributes"
34+
"failureMessage": "Incorrect ɵɵanimateEnter() call"
35+
}
36+
]
37+
},
38+
{
39+
"description": "should generate animate enter instructions on element with a binding",
40+
"inputFiles": [
41+
"animate_enter_with_binding.ts"
42+
],
43+
"expectations": [
44+
{
45+
"files": [
46+
{
47+
"expected": "animate_enter_with_binding_template.js",
48+
"generated": "animate_enter_with_binding.js"
49+
}
50+
],
51+
"failureMessage": "Incorrect ɵɵanimateEnter() call"
52+
}
53+
]
54+
},
55+
{
56+
"description": "should generate animate enter instructions on element with an event binding",
57+
"inputFiles": [
58+
"animate_enter_with_event_listener.ts"
59+
],
60+
"expectations": [
61+
{
62+
"files": [
63+
{
64+
"expected": "animate_enter_with_event_listener_template.js",
65+
"generated": "animate_enter_with_event_listener.js"
66+
}
67+
],
68+
"failureMessage": "Incorrect ɵɵanimateEnter() call"
69+
}
70+
]
71+
},
72+
{
73+
"description": "should generate animate leave instructions on element with a simple string",
74+
"inputFiles": [
75+
"animate_leave_with_string.ts"
76+
],
77+
"expectations": [
78+
{
79+
"files": [
80+
{
81+
"expected": "animate_leave_with_string_template.js",
82+
"generated": "animate_leave_with_string.js"
83+
}
84+
],
85+
"failureMessage": "Incorrect ɵɵanimateLeave() call"
86+
}
87+
]
88+
},
89+
{
90+
"description": "should generate animate leave instructions on element with a binding",
91+
"inputFiles": [
92+
"animate_leave_with_binding.ts"
93+
],
94+
"expectations": [
95+
{
96+
"files": [
97+
{
98+
"expected": "animate_leave_with_binding_template.js",
99+
"generated": "animate_leave_with_binding.js"
100+
}
101+
],
102+
"failureMessage": "Incorrect ɵɵanimateLeave() call"
103+
}
104+
]
105+
},
106+
{
107+
"description": "should generate animate leave instructions on element with an event binding",
108+
"inputFiles": [
109+
"animate_leave_with_event_listener.ts"
110+
],
111+
"expectations": [
112+
{
113+
"files": [
114+
{
115+
"expected": "animate_leave_with_event_listener_template.js",
116+
"generated": "animate_leave_with_event_listener.js"
117+
}
118+
],
119+
"failureMessage": "Incorrect ɵɵanimateLeave() call"
120+
}
121+
]
122+
},
123+
{
124+
"description": "should generate animate leave instructions with host binding and simple string",
125+
"inputFiles": [
126+
"animate_leave_with_string_host_bindings.ts"
127+
],
128+
"expectations": [
129+
{
130+
"files": [
131+
{
132+
"expected": "animate_leave_with_string_host_bindings_template.js",
133+
"generated": "animate_leave_with_string_host_bindings.js"
134+
}
135+
],
136+
"failureMessage": "Incorrect ɵɵanimateLeave() call"
35137
}
36138
]
37139
}
38140
]
39-
}
141+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {Component, signal} from '@angular/core';
2+
3+
@Component({
4+
selector: 'my-component',
5+
template: `
6+
<div>
7+
<p [animate.enter]="enterClass()">Sliding Content</p>
8+
</div>
9+
`,
10+
})
11+
export class MyComponent {
12+
enterClass = signal('slide');
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MyComponent.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: MyComponent, selectors: [["my-component"]], decls: 3, vars: 0, template: function MyComponent_Template(rf, ctx) { if (rf & 1) {
2+
i0.ɵɵdomElementStart(0, "div")(1, "p");
3+
i0.ɵɵanimateEnter(function MyComponent_Template_animateenter_cb() { return ctx.enterClass(); });
4+
i0.ɵɵtext(2, "Sliding Content");
5+
i0.ɵɵdomElementEnd()();
6+
} }, encapsulation: 2 });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Component} from '@angular/core';
2+
3+
@Component({
4+
selector: 'my-component',
5+
template: `
6+
<div>
7+
<p (animate.enter)="slideFn($event)">Sliding Content</p>
8+
</div>
9+
`,
10+
})
11+
export class MyComponent {
12+
slideFn(event: any) {
13+
event.target.classList.add('slide-in');
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MyComponent.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: MyComponent, selectors: [["my-component"]], decls: 3, vars: 0, template: function MyComponent_Template(rf, ctx) { if (rf & 1) {
2+
i0.ɵɵdomElementStart(0, "div")(1, "p");
3+
i0.ɵɵanimateEnterListener(function MyComponent_Template_p_animateenter_1_listener($event) { return ctx.slideFn($event); });
4+
i0.ɵɵtext(2, "Sliding Content");
5+
i0.ɵɵdomElementEnd()();
6+
} }, encapsulation: 2 });

0 commit comments

Comments
 (0)