Skip to content

Commit 6cd723e

Browse files
committed
refactor(aria/menu): Extend public api with open/close methods
1 parent 5cd7520 commit 6cd723e

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/aria/menu/menu.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ export class MenuTrigger<V> {
8383
constructor() {
8484
effect(() => this.menu()?.parent.set(this));
8585
}
86+
87+
/** Opens the menu focusing on the first menu item. */
88+
open() {
89+
this._pattern.open({first: true});
90+
}
91+
92+
/** Closes the menu. */
93+
close() {
94+
this._pattern.close();
95+
}
8696
}
8797

8898
/**
@@ -220,24 +230,9 @@ export class Menu<V> {
220230
});
221231
}
222232

223-
// TODO(wagnermaciel): Author close, closeAll, and open methods for each directive.
224-
225233
/** Closes the menu. */
226-
close(opts?: {refocus?: boolean}) {
227-
this._pattern.inputs.parent()?.close(opts);
228-
}
229-
230-
/** Closes all parent menus. */
231-
closeAll(opts?: {refocus?: boolean}) {
232-
const root = this._pattern.root();
233-
234-
if (root instanceof MenuTriggerPattern) {
235-
root.close(opts);
236-
}
237-
238-
if (root instanceof MenuPattern || root instanceof MenuBarPattern) {
239-
root.inputs.activeItem()?.close(opts);
240-
}
234+
close() {
235+
this._pattern.close();
241236
}
242237
}
243238

@@ -318,6 +313,11 @@ export class MenuBar<V> {
318313
}
319314
});
320315
}
316+
317+
/** Closes the menubar. */
318+
close() {
319+
this._pattern.close();
320+
}
321321
}
322322

323323
/**
@@ -396,6 +396,16 @@ export class MenuItem<V> {
396396
constructor() {
397397
effect(() => this.submenu()?.parent.set(this));
398398
}
399+
400+
/** Opens the submenu focusing on the first menu item. */
401+
open() {
402+
this._pattern.open({first: true});
403+
}
404+
405+
/** Closes the submenu. */
406+
close() {
407+
this._pattern.close();
408+
}
399409
}
400410

401411
/** Defers the rendering of the menu content. */

src/aria/private/menu/menu.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ export class MenuPattern<V> {
395395
}
396396
}
397397

398+
/** Closes the menu. */
399+
close() {
400+
this.inputs.parent()?.close();
401+
}
402+
398403
/** Closes the menu and all parent menus. */
399404
closeAll() {
400405
const root = this.root();

src/components-examples/aria/menu/menu-context/menu-context-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class MenuContextExample {
3737

3838
open(event: MouseEvent) {
3939
const menu = this.menu();
40-
menu?.closeAll();
40+
menu?._pattern.closeAll();
4141

4242
if (menu) {
4343
event.preventDefault();

0 commit comments

Comments
 (0)