Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(animations): fix stagger timing not handling params #47208

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ export class AnimationAstBuilderVisitor implements AnimationDslVisitor {
}
const timings = metadata.timings === 'full' ?
{duration: 0, delay: 0, easing: 'full'} :
resolveTiming(metadata.timings, context.errors, true);
constructTimingAst(metadata.timings, context.errors);

return {
type: AnimationMetadataType.Stagger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ export class AnimationTimelineBuilderVisitor implements AstVisitor {
context.previousNode = ast;
}

private _visitTiming(ast: TimingAst, context: AnimationTimelineContext): AnimateTimings {
private _visitTiming(
ast: TimingAst, context: AnimationTimelineContext,
allowNegativeValues = false): AnimateTimings {
if ((ast as DynamicTimingAst).dynamic) {
const strValue = (ast as DynamicTimingAst).strValue;
const timingValue =
context.params ? interpolateParams(strValue, context.params, context.errors) : strValue;
return resolveTiming(timingValue, context.errors);
return resolveTiming(timingValue, context.errors, allowNegativeValues);
} else {
return {duration: ast.duration, delay: ast.delay, easing: ast.easing};
}
Expand Down Expand Up @@ -413,7 +415,8 @@ export class AnimationTimelineBuilderVisitor implements AstVisitor {
visitStagger(ast: StaggerAst, context: AnimationTimelineContext) {
const parentContext = context.parentContext!;
const tl = context.currentTimeline;
const timings = ast.timings;
const timings = this._visitTiming(ast.timings, context, true);

const duration = Math.abs(timings.duration);
const maxTime = duration * (context.currentQueryTotal - 1);
let delay = duration * context.currentQueryIndex;
Expand Down
99 changes: 99 additions & 0 deletions packages/core/test/animation/animation_query_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,105 @@ describe('animation query tests', function() {
expect(p5.delay).toEqual(6000);
});

it(`should handle params used in the stagger's timing argument`, () => {
@Component({
selector: 'ani-cmp',
template: `
<div [@myAnimation]="exp">
<div *ngFor="let item of items" class="item">
{{ item }}
</div>
</div>
`,
animations: [
trigger('myAnimation', [
transition('* => go', [
query('.item', [
stagger('{{staggerDelay}}ms',[
style({opacity: 0}), animate(1000, style({opacity: .5})),
animate(500, style({opacity: 1}))
])
])
], {params: { staggerDelay: '1111' }})
])
]
})
class Cmp {
public exp: any;
public items: any[] = [0, 1, 2, 3, 4];
}

TestBed.configureTestingModule({declarations: [Cmp]});

const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;

cmp.exp = 'go';
fixture.detectChanges();
engine.flush();

const players = getLog();
expect(players.length).toEqual(5);

const [p1, p2, p3, p4, p5] = players;
expect(p1.delay).toEqual(0);
expect(p2.delay).toEqual(1111);
expect(p3.delay).toEqual(2222);
expect(p4.delay).toEqual(3333);
expect(p5.delay).toEqual(4444);
});

it(`should handle params used in the stagger's timing argument producing a negative value`,
() => {
@Component({
selector: 'ani-cmp',
template: `
<div [@myAnimation]="exp">
<div *ngFor="let item of items" class="item">
{{ item }}
</div>
</div>
`,
animations: [
trigger('myAnimation', [
transition('* => go', [
query('.item', [
stagger('{{staggerDelay}}ms',[
style({opacity: 0}), animate(1000, style({opacity: .5})),
animate(500, style({opacity: 1}))
])
])
], {params: { staggerDelay: -1111 }})
])
]
})
class Cmp {
public exp: any;
public items: any[] = [0, 1, 2, 3, 4];
}

TestBed.configureTestingModule({declarations: [Cmp]});

const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;

cmp.exp = 'go';
fixture.detectChanges();
engine.flush();

const players = getLog();
expect(players.length).toEqual(5);

const [p1, p2, p3, p4, p5] = players;
expect(p5.delay).toEqual(0);
expect(p4.delay).toEqual(1111);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is non-blocking, but it would be great if these tests also validated the style values and not just the delays. That way we verify stagger is working as intended, rather than just that the negative delay is applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure I can add some checks 🙂👍

expect(p3.delay).toEqual(2222);
expect(p2.delay).toEqual(3333);
expect(p1.delay).toEqual(4444);
});

it('should persist inner sub trigger styles once their animation is complete', () => {
@Component({
selector: 'ani-cmp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@
{
"name": "connectableObservableDescriptor"
},
{
"name": "constructTimingAst"
},
{
"name": "containsElement"
},
Expand Down