Skip to content

Commit 8fc6eac

Browse files
authored
fix(aria/menu): avoid re-rendering when unrelated elements are added (#33996)
Updates the `SortedCollection` so it doesn't emit changes when a DOM node that's not a member of the collection is added. I also did some internal cleanup so we don't need to re-create the `Set` all the time and to keep a version counter. Fixes #33976.
1 parent 19c1610 commit 8fc6eac

2 files changed

Lines changed: 39 additions & 27 deletions

File tree

‎src/aria/private/utils/collection.spec.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ describe('SortedCollection with real directives', () => {
7474
expect(orderedItems[0].value).toBe('C');
7575
expect(orderedItems[2].value).toBe('A');
7676
});
77+
78+
it('should not update collection if the DOM changes but the items have not moved', async () => {
79+
const collection = collectionDirective._collection;
80+
const previousItems = collection.orderedItems();
81+
82+
collectionDirective.element.appendChild(document.createElement('div'));
83+
await waitForMicrotasks();
84+
85+
expect(collection.orderedItems()).toBe(previousItems);
86+
});
7787
});
7888

7989
@Component({
@@ -97,9 +107,7 @@ class TestCollectionComponent implements HasElement, OnDestroy {
97107

98108
@Component({
99109
selector: 'testItem',
100-
template: `
101-
<div>{{value}}</div>
102-
`,
110+
template: `<div>{{value}}</div>`,
103111
})
104112
class TestItemComponent implements HasElement, OnDestroy {
105113
readonly element = inject(ElementRef).nativeElement as HTMLElement;

‎src/aria/private/utils/collection.ts‎

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {signal, computed, Signal} from '@angular/core';
9+
import {signal, Signal} from '@angular/core';
1010
import {sortDirectives, HasElement} from './element';
1111

1212
/**
@@ -17,49 +17,53 @@ import {sortDirectives, HasElement} from './element';
1717
* TODO(ok7sai): replace Mutation Observer with internal API.
1818
*/
1919
export class SortedCollection<T extends HasElement> {
20-
private readonly _items = signal<Set<T>>(new Set());
21-
private readonly _version = signal(0);
20+
private readonly _items = new Set<T>();
21+
private readonly _sortedItems = signal<T[]>([]);
2222
private _observer?: MutationObserver;
2323

24-
readonly orderedItems: Signal<T[]> = computed(() => {
25-
this._version(); // Track DOM changes
26-
const itemsArray = Array.from(this._items());
27-
return itemsArray.sort(sortDirectives);
28-
});
24+
readonly orderedItems: Signal<T[]> = this._sortedItems.asReadonly();
2925

3026
register(item: T) {
31-
this._items.update(set => {
32-
const newSet = new Set(set);
33-
newSet.add(item);
34-
return newSet;
35-
});
27+
if (!this._items.has(item)) {
28+
this._items.add(item);
29+
this._sortedItems.set(this._getSortedItems());
30+
}
3631
}
3732

3833
unregister(item: T) {
39-
this._items.update(set => {
40-
const newSet = new Set(set);
41-
newSet.delete(item);
42-
return newSet;
43-
});
34+
if (this._items.has(item)) {
35+
this._items.delete(item);
36+
this._sortedItems.set(this._getSortedItems());
37+
}
4438
}
4539

4640
startObserving(element: HTMLElement) {
47-
if (this._observer) {
48-
this._observer.disconnect();
49-
}
50-
41+
this.stopObserving();
5142
this._observer = new MutationObserver(mutations => {
5243
const hasStructuralChange = mutations.some(m => m.addedNodes.length || m.removedNodes.length);
44+
5345
if (hasStructuralChange) {
54-
this._version.update(v => v + 1);
46+
const newItems = this._getSortedItems();
47+
const currentItems = this._sortedItems();
48+
const hasChanged =
49+
newItems.length !== currentItems.length ||
50+
newItems.some((item, index) => currentItems[index] !== item);
51+
52+
if (hasChanged) {
53+
this._sortedItems.set(newItems);
54+
}
5555
}
5656
});
5757

58-
this._observer.observe(element, {childList: true, subtree: true});
58+
this._observer.observe(element, {childList: true, subtree: true, attributes: false});
5959
}
6060

6161
stopObserving() {
6262
this._observer?.disconnect();
6363
this._observer = undefined;
6464
}
65+
66+
private _getSortedItems() {
67+
return Array.from(this._items).sort(sortDirectives);
68+
}
6569
}

0 commit comments

Comments
 (0)