MCPcopy Create free account
hub / github.com/angular/angular / cancelLeavingNodes

Function cancelLeavingNodes

packages/core/src/animation/utils.ts:181–233  ·  view source on GitHub ↗
(tNode: TNode, newElement: HTMLElement, newLView?: LView)

Source from the content-addressed store, hash-verified

179 * `TNode`) from the same logical view being re-rendered into a new DOM parent.
180 */
181export function cancelLeavingNodes(tNode: TNode, newElement: HTMLElement, newLView?: LView): void {
182 const nodes = leavingNodes.get(tNode);
183 if (!nodes || nodes.length === 0) return;
184
185 const newParent = newElement.parentNode;
186 const prevSibling = newElement.previousSibling;
187 const newDeclarationView = getDeclarationView(newLView);
188
189 for (let i = nodes.length - 1; i >= 0; i--) {
190 const {el: leavingEl, declarationView: leavingDeclarationView} = nodes[i];
191 const leavingParent = leavingEl.parentNode;
192 // Cancel if the leaving element is:
193 // - The direct previousSibling of the new element. This is reliable
194 // because Angular inserts new elements at the same position (before
195 // the container anchor) where the leaving element was, making them
196 // always adjacent. Covers @if toggling and same-VCR toggling.
197 // - The leaving element IS the new element. This happens when a node is moved
198 // (e.g., drag-and-drop reordering). We must cancel its pending leave animation
199 // and ensure it's not physically removed from the DOM by marking it as reused.
200 if (leavingEl === newElement) {
201 nodes.splice(i, 1);
202 reusedNodes.add(leavingEl);
203 leavingEl.dispatchEvent(new CustomEvent('animationend', {detail: {cancel: true}}));
204 } else if (prevSibling && leavingEl === prevSibling) {
205 nodes.splice(i, 1);
206 leavingEl.dispatchEvent(new CustomEvent('animationend', {detail: {cancel: true}}));
207 leavingEl.parentNode?.removeChild(leavingEl);
208 } else if (leavingParent && newParent && leavingParent !== newParent) {
209 // The leaving element is in a different DOM parent than the entering one.
210 // This is ambiguous: it can be the same logical view re-rendered into a new
211 // container (e.g. a dynamic component re-created in a fresh CDK overlay
212 // pane), which must be de-duplicated by removing it immediately; or it can
213 // be a *distinct* instance of the same template (accordions, exclusive-
214 // expansion menus, master/detail nav) that merely shares this `TNode` and
215 // is legitimately leaving in its own parent. Only force-remove when the
216 // entering element belongs to the SAME declaration view as the leaving one
217 // — i.e. a true re-render of the same logical view. Distinct instances are
218 // left alone so their `animate.leave` runs to completion.
219 const sameLogicalView =
220 newDeclarationView === null ||
221 leavingDeclarationView === null ||
222 newDeclarationView === leavingDeclarationView;
223 if (sameLogicalView) {
224 nodes.splice(i, 1);
225 leavingEl.dispatchEvent(new CustomEvent('animationend', {detail: {cancel: true}}));
226 leavingEl.parentNode?.removeChild(leavingEl);
227 }
228 }
229 }
230 if (nodes.length === 0) {
231 leavingNodes.delete(tNode);
232 }
233}
234
235/**
236 * Tracks the nodes list of nodes that are leaving the DOM so we can cancel any leave animations

Callers 1

Calls 6

getDeclarationViewFunction · 0.85
getMethod · 0.65
addMethod · 0.65
removeChildMethod · 0.65
dispatchEventMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected