Skip to content

Commit 75dbd93

Browse files
committed
refactor(aria/menu): Extend public api with open/close methods
1 parent 1b418e6 commit 75dbd93

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
@@ -85,6 +85,16 @@ export class MenuTrigger<V> {
8585
onFocusIn() {
8686
this.hasBeenFocused.set(true);
8787
}
88+
89+
/** Opens the menu. */
90+
open(opts?: {first?: boolean; last?: boolean}) {
91+
this._pattern.open(opts);
92+
}
93+
94+
/** Closes the menu. */
95+
close(opts: {refocus?: boolean} = {}) {
96+
this._pattern.close(opts);
97+
}
8898
}
8999

90100
/**
@@ -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

@@ -320,6 +320,11 @@ export class MenuBar<V> {
320320
}
321321
});
322322
}
323+
324+
/** Closes the menubar and refocuses the root menu bar item. */
325+
close(opts?: {refocus?: boolean}) {
326+
this._pattern.close(opts);
327+
}
323328
}
324329

325330
/**
@@ -397,6 +402,16 @@ export class MenuItem<V> {
397402
onFocusIn() {
398403
this.hasBeenFocused.set(true);
399404
}
405+
406+
/** Opens the submenu. */
407+
open(opts?: {first?: boolean; last?: boolean}) {
408+
this._pattern.open(opts);
409+
}
410+
411+
/** Closes the submenu. */
412+
close(opts: {refocus?: boolean} = {}) {
413+
this._pattern.close(opts);
414+
}
400415
}
401416

402417
/** 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
@@ -154,7 +154,7 @@ export class MenuPattern<V> {
154154
.on('Home', () => this.first())
155155
.on('End', () => this.last())
156156
.on('Enter', () => this.trigger())
157-
.on('Escape', () => this.closeAll())
157+
.on('Escape', () => this.closeAll({refocus: true}))
158158
.on(this._expandKey, () => this.expand())
159159
.on(this._collapseKey, () => this.collapse())
160160
.on(this.dynamicSpaceKey, () => this.trigger())
@@ -395,20 +395,25 @@ export class MenuPattern<V> {
395395
}
396396
}
397397

398+
/** Closes the menu. */
399+
close(opts?: {refocus?: boolean}) {
400+
this.inputs.parent()?.close(opts);
401+
}
402+
398403
/** Closes the menu and all parent menus. */
399-
closeAll() {
404+
closeAll(opts?: {refocus?: boolean}) {
400405
const root = this.root();
401406

402407
if (root instanceof MenuTriggerPattern) {
403-
root.close({refocus: true});
408+
root.close(opts);
404409
}
405410

406411
if (root instanceof MenuBarPattern) {
407412
root.close();
408413
}
409414

410415
if (root instanceof MenuPattern) {
411-
root.inputs.activeItem()?.close({refocus: true});
416+
root.inputs.activeItem()?.close(opts);
412417
}
413418
}
414419

@@ -568,8 +573,10 @@ export class MenuBarPattern<V> {
568573
}
569574

570575
/** Closes the menubar and refocuses the root menu bar item. */
571-
close() {
572-
this.inputs.activeItem()?.close({refocus: this.isFocused()});
576+
close(opts?: {refocus?: boolean}) {
577+
opts ??= {refocus: this.isFocused()};
578+
579+
this.inputs.activeItem()?.close(opts);
573580
}
574581
}
575582

0 commit comments

Comments
 (0)