Skip to content

Commit 195d4a1

Browse files
committed
refactor(aria/menu): Extend public api with open/close methods
1 parent b9f1207 commit 195d4a1

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/aria/menu/menu.ts

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

8898
/**
@@ -222,24 +232,14 @@ export class Menu<V> {
222232
});
223233
}
224234

225-
// TODO(wagnermaciel): Author close, closeAll, and open methods for each directive.
226-
227235
/** Closes the menu. */
228236
close(opts?: {refocus?: boolean}) {
229-
this._pattern.inputs.parent()?.close(opts);
237+
this._pattern.close(opts);
230238
}
231239

232240
/** Closes all parent menus. */
233241
closeAll(opts?: {refocus?: boolean}) {
234-
const root = this._pattern.root();
235-
236-
if (root instanceof MenuTriggerPattern) {
237-
root.close(opts);
238-
}
239-
240-
if (root instanceof MenuPattern || root instanceof MenuBarPattern) {
241-
root.inputs.activeItem()?.close(opts);
242-
}
242+
this._pattern.closeAll(opts);
243243
}
244244
}
245245

@@ -325,6 +325,11 @@ export class MenuBar<V> {
325325
}
326326
});
327327
}
328+
329+
/** Closes the menubar and refocuses the root menu bar item. */
330+
close(opts?: {refocus?: boolean}) {
331+
this._pattern.close(opts);
332+
}
328333
}
329334

330335
/**
@@ -402,6 +407,16 @@ export class MenuItem<V> {
402407
onFocusIn() {
403408
this.hasBeenFocused.set(true);
404409
}
410+
411+
/** Opens the submenu. */
412+
open(opts?: {first?: boolean; last?: boolean}) {
413+
this._pattern.open(opts);
414+
}
415+
416+
/** Closes the submenu. */
417+
close(opts: {refocus?: boolean} = {}) {
418+
this._pattern.close(opts);
419+
}
405420
}
406421

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

src/aria/private/menu/menu.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class MenuPattern<V> {
136136
.on('Home', () => this.first())
137137
.on('End', () => this.last())
138138
.on('Enter', () => this.trigger())
139-
.on('Escape', () => this.closeAll())
139+
.on('Escape', () => this.closeAll({refocus: true}))
140140
.on(this._expandKey, () => this.expand())
141141
.on(this._collapseKey, () => this.collapse())
142142
.on(this.dynamicSpaceKey, () => this.trigger())
@@ -345,20 +345,25 @@ export class MenuPattern<V> {
345345
}
346346
}
347347

348+
/** Closes the menu. */
349+
close(opts?: {refocus?: boolean}) {
350+
this.inputs.parent()?.close(opts);
351+
}
352+
348353
/** Closes the menu and all parent menus. */
349-
closeAll() {
354+
closeAll(opts?: {refocus?: boolean}) {
350355
const root = this.root();
351356

352357
if (root instanceof MenuTriggerPattern) {
353-
root.close({refocus: true});
358+
root.close(opts);
354359
}
355360

356361
if (root instanceof MenuBarPattern) {
357362
root.close();
358363
}
359364

360365
if (root instanceof MenuPattern) {
361-
root.inputs.activeItem()?.close({refocus: true});
366+
root.inputs.activeItem()?.close(opts);
362367
}
363368
}
364369
}
@@ -496,8 +501,10 @@ export class MenuBarPattern<V> {
496501
}
497502

498503
/** Closes the menubar and refocuses the root menu bar item. */
499-
close() {
500-
this.inputs.activeItem()?.close({refocus: this.isFocused()});
504+
close(opts?: {refocus?: boolean}) {
505+
opts ??= {refocus: this.isFocused()};
506+
507+
this.inputs.activeItem()?.close(opts);
501508
}
502509
}
503510

0 commit comments

Comments
 (0)