Skip to content

Commit 90f8fee

Browse files
authored
fix(aria/menu): render content only when visible (#34104)
Fixes a couple of issues with the Aria menu: 1. We were rendering out the content on focus, even if the menu isn't opened. 2. We weren't destroying the content when `preserveContent` is `false`. Fixes #34101.
1 parent aa1afc7 commit 90f8fee

5 files changed

Lines changed: 76 additions & 9 deletions

File tree

‎goldens/aria/private/index.api.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ export class MenuItemPattern<V> implements ListItem<V | undefined> {
420420
first?: boolean;
421421
last?: boolean;
422422
}): void;
423+
readonly pendingFocus: WritableSignalLike<"first" | "last" | null>;
424+
pendingFocusEffect(): void;
423425
readonly role: () => "menuitem" | "menuitemcheckbox" | "menuitemradio";
424426
readonly searchTerm: SignalLike<string>;
425427
readonly selectable: SignalLike<boolean>;

‎src/aria/menu/menu-item.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class MenuItem<V> implements OnInit, OnDestroy {
105105

106106
constructor() {
107107
effect(() => this.submenu()?.parent.set(this));
108+
effect(() => this._pattern.pendingFocusEffect());
108109

109110
// Check for any violations after the DOM has been updated.
110111
if (typeof ngDevMode === 'undefined' || ngDevMode) {

‎src/aria/menu/menu.spec.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,37 @@ describe('Menu Trigger Pattern', () => {
798798
await click(getItem('Apple')!);
799799
expect(fixture.componentInstance.itemSelected).toHaveBeenCalledWith('Apple');
800800
});
801+
802+
it('should not render deferred content on trigger focus and should destroy it when menu closes', async () => {
803+
expect(getItem('Apple')).toBeNull();
804+
await focusin(getTrigger());
805+
expect(getItem('Apple')).toBeNull();
806+
807+
await click(getTrigger());
808+
expect(isExpanded()).toBe(true);
809+
expect(getItem('Apple')).not.toBeNull();
810+
811+
await keydown(getTrigger(), 'Escape');
812+
expect(isExpanded()).toBe(false);
813+
expect(getItem('Apple')).toBeNull();
814+
});
815+
816+
it('should preserve content when preserveContent is true', async () => {
817+
fixture.componentInstance.preserveContent.set(true);
818+
await fixture.whenStable();
819+
820+
expect(getItem('Apple')).toBeNull();
821+
822+
// Open the menu
823+
await click(getTrigger());
824+
expect(isExpanded()).toBe(true);
825+
expect(getItem('Apple')).not.toBeNull();
826+
827+
// Close the menu
828+
await keydown(getTrigger(), 'Escape');
829+
expect(isExpanded()).toBe(false);
830+
expect(getItem('Apple')).not.toBeNull();
831+
});
801832
});
802833
});
803834

@@ -1293,7 +1324,13 @@ class StandaloneMenuExample {
12931324
template: `
12941325
<button ngMenuTrigger [menu]="menu">Open menu</button>
12951326
1296-
<div ngMenu [expansionDelay]="0" #menu="ngMenu" (itemSelected)="itemSelected($event)">
1327+
<div
1328+
ngMenu
1329+
[preserveContent]="preserveContent()"
1330+
[expansionDelay]="0"
1331+
#menu="ngMenu"
1332+
(itemSelected)="itemSelected($event)"
1333+
>
12971334
<ng-template ngMenuContent>
12981335
<div ngMenuItem value='Apple' searchTerm='Apple'>Apple</div>
12991336
<div ngMenuItem value='Banana' searchTerm='Banana'>Banana</div>
@@ -1315,6 +1352,7 @@ class StandaloneMenuExample {
13151352
changeDetection: ChangeDetectionStrategy.Eager,
13161353
})
13171354
class MenuTriggerExample {
1355+
preserveContent = signal(false);
13181356
itemSelected(value: string) {}
13191357
}
13201358

‎src/aria/menu/menu.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ export class Menu<V> implements OnDestroy {
169169
if (parent instanceof MenuItem && parent.parent instanceof MenuBar) {
170170
this._deferredContentAware?.contentVisible.set(true);
171171
} else {
172-
this._deferredContentAware?.contentVisible.set(
173-
this._pattern.visible() || !!this.parent()?._pattern.hasBeenInteracted(),
174-
);
172+
this._deferredContentAware?.contentVisible.set(this._pattern.visible());
175173
}
176174
},
177175
});

‎src/aria/private/menu/menu.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,9 @@ export class MenuItemPattern<V> implements ListItem<V | undefined> {
827827
/** Whether the menu item is selectable. */
828828
readonly selectable: SignalLike<boolean>;
829829

830+
/** The pending focus target when the submenu is opened before the submenu instance is available. */
831+
readonly pendingFocus = signal<'first' | 'last' | null>(null);
832+
830833
constructor(readonly inputs: MenuItemInputs<V>) {
831834
this.id = inputs.id;
832835
this.value = inputs.value ?? signal(undefined);
@@ -836,6 +839,22 @@ export class MenuItemPattern<V> implements ListItem<V | undefined> {
836839
this.selectable = computed(() => !this.submenu());
837840
}
838841

842+
/** Flushes any pending focus when the submenu instance becomes available. */
843+
pendingFocusEffect(): void {
844+
const submenu = this.submenu();
845+
const intent = this.pendingFocus();
846+
const items = submenu?.items();
847+
848+
if (submenu && intent && items?.length) {
849+
if (intent === 'first') {
850+
submenu.first();
851+
} else if (intent === 'last') {
852+
submenu.last();
853+
}
854+
this.pendingFocus.set(null);
855+
}
856+
}
857+
839858
/** Opens the submenu. */
840859
open(opts?: {first?: boolean; last?: boolean}) {
841860
if (this.disabled()) {
@@ -844,17 +863,26 @@ export class MenuItemPattern<V> implements ListItem<V | undefined> {
844863

845864
this._expanded.set(true);
846865

847-
if (opts?.first) {
848-
this.submenu()?.first();
849-
}
850-
if (opts?.last) {
851-
this.submenu()?.last();
866+
const submenu = this.submenu();
867+
const items = submenu?.items();
868+
869+
if (submenu && items?.length) {
870+
if (opts?.first) {
871+
submenu.first();
872+
} else if (opts?.last) {
873+
submenu.last();
874+
}
875+
} else if (opts?.first) {
876+
this.pendingFocus.set('first');
877+
} else if (opts?.last) {
878+
this.pendingFocus.set('last');
852879
}
853880
}
854881

855882
/** Closes the submenu. */
856883
close(opts: {refocus?: boolean} = {}) {
857884
this._expanded.set(false);
885+
this.pendingFocus.set(null);
858886

859887
if (opts.refocus) {
860888
this.inputs.parent()?.listBehavior.goto(this);

0 commit comments

Comments
 (0)