Skip to content

Angular animation: :leave animation starts at wrong position #29526

Closed

Description

@mfursov

Affected Package

@angular/animations

Description

:leave animation has incorrect initial position for the animated elements.

🔬 Minimal Reproduction

https://stackblitz.com/edit/angular-fqxnxd?file=src%2Fapp%2Fapp.component.ts

Follow the steps in the demo. Notice that only first element exits correctly.

🌍 Your Environment

Chrome v73

Angular Version:
"@angular/animations": "^7.2.10",
"@angular/common": "7.2.5",
"@angular/compiler": "7.2.5",
"@angular/core": "7.2.5"

Activity

  1. malieop commented on Mar 26, 2019

    @malieop

    The problem only happens when you reverse your list order.

  2. added
    area: animations
    on Mar 27, 2019
  3. added this to the needsTriage milestone on Mar 27, 2019
  4. modified the milestones: needsTriage, Backlog on Apr 16, 2019
  5. marcusrettig commented on Jan 2, 2020

    @marcusrettig

    I have the same issue, here are my conclusions so far:

    • The issue is actually that the :enter transition runs instead of :leave.
    • It only happens when :enter is before :leave in the array passed to trigger(..).
    • It still happens if you completely remove the :leave transition.
  6. added
    P3
    and removed on Oct 1, 2020
  7. dario-piotrowicz commented on Dec 29, 2021

    @dario-piotrowicz
    Contributor

    Hi @mfursov and all, I've just created a PR with the fix 🙂 👆

    The issue is as @marcusrettig suggested that :enter runs instead of :leave (also the other points he made are correct, as for why, keep reading and that should become quite clear 🙂), that is because when the elements in the list are moved their DOM elements are removed and re-inserted
    (PS: when reversing the array the last one is an exception since it, as a side effect gets moved/pushed down by the other n-1 items, that is why the last item in the reversed list doesn't present the issue)


    The real issue here is that the reordering "triggers" the leaving of the elements, but then cancels it (you don't see them disappear or anything), as a side effect however the elements (again, except the last one) lose their trigger's value.

    To explain better, please have a look at this updated stackblitz with console.logs, there:

    • when you add the 3 items, in the console you can see that normal transitions from void to null occur (remember that void => * matches :enter)
    • when you reverse the list you will see 2 (and not 3, as I mentioned earlier) transitions from null to void (which match :leave) but the items haven't actually left the DOM nor any leave animation is executed
    • when you remove the 3 items, (as already mentioned) the last one (first to be removed) works fine (null to void) but the other 2 transition from void to void (it should be from null to void, but the null got lost/replaced widh void during the reordering)

    by remembering that void to * (where * indicates anything) matches :enter we can now see why the :enter transition is executed instead of :leave (note: the first successful transition in the array cancels the others)

    (also note that void to void also matched :leave/* => void so, if you switch the transitions in the array everything would work as expected even without my PR's fix)


    (also feel free to experiment with the stackblitz, this also happens if we bind some value to the trigger, not only if the value is null)

    (ps: if my explanation about the last item not being effected doesn't sound convincing, you can reverse the list 2 times, in that way you all the items are actually moved at least once by themselves and will have their trigger's values set to void and all transitions will be broken/look like :leave 🙂 )

    I hope this helps clarifying things 🙂

  8. added a commit that references this issue on Jan 7, 2022
    abc217b
  9. angular-automatic-lock-bot commented on Feb 7, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  10. locked and limited conversation to collaborators on Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: animationsfreq1: lowtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions