Skip to content

Commit 5b78d36

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

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/aria/menu/menu.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ export class MenuTrigger<V> {
8383
constructor() {
8484
effect(() => this.menu()?.parent.set(this));
8585
}
86+
87+
/** Marks the menu trigger as having been focused. */
88+
onFocusIn() {
89+
this.hasBeenFocused.set(true);
90+
}
91+
92+
/** Opens the menu focusing on the first menu item. */
93+
open() {
94+
this._pattern.open({first: true});
95+
}
96+
97+
/** Closes the menu. */
98+
close() {
99+
this._pattern.close();
100+
}
86101
}
87102

88103
/**
@@ -220,24 +235,9 @@ export class Menu<V> {
220235
});
221236
}
222237

223-
// TODO(wagnermaciel): Author close, closeAll, and open methods for each directive.
224-
225238
/** 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-
}
239+
close() {
240+
this._pattern.close();
241241
}
242242
}
243243

@@ -318,6 +318,11 @@ export class MenuBar<V> {
318318
}
319319
});
320320
}
321+
322+
/** Closes the menubar. */
323+
close() {
324+
this._pattern.close();
325+
}
321326
}
322327

323328
/**
@@ -396,6 +401,21 @@ export class MenuItem<V> {
396401
constructor() {
397402
effect(() => this.submenu()?.parent.set(this));
398403
}
404+
405+
/** Marks the menu item as having been focused. */
406+
onFocusIn() {
407+
this.hasBeenFocused.set(true);
408+
}
409+
410+
/** Opens the submenu focusing on the first menu item. */
411+
open() {
412+
this._pattern.open({first: true});
413+
}
414+
415+
/** Closes the submenu. */
416+
close() {
417+
this._pattern.close();
418+
}
399419
}
400420

401421
/** 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)