Skip to content

Commit a5eb803

Browse files
committed
feat(aria/menu): Extend public api with open/close methods
1 parent ecd9039 commit a5eb803

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

src/aria/menu/menu.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ export class MenuTrigger<V> {
7272
element: computed(() => this._elementRef.nativeElement),
7373
submenu: computed(() => this.submenu()?._pattern),
7474
});
75+
76+
/** Opens the menu. */
77+
open(opts?: {first?: boolean; last?: boolean}) {
78+
this._pattern.open(opts);
79+
}
80+
81+
/** Closes the menu. */
82+
close(opts: {refocus?: boolean} = {}) {
83+
this._pattern.close(opts);
84+
}
7585
}
7686

7787
/**
@@ -194,24 +204,34 @@ export class Menu<V> {
194204
});
195205
}
196206

197-
// TODO(wagnermaciel): Author close, closeAll, and open methods for each directive.
207+
/** Focuses the previous menu item. */
208+
prev() {
209+
this._pattern.prev();
210+
}
211+
212+
/** Focuses the next menu item. */
213+
next() {
214+
this._pattern.next();
215+
}
216+
217+
/** Focuses the first menu item. */
218+
first() {
219+
this._pattern.first();
220+
}
221+
222+
/** Focuses the last menu item. */
223+
last() {
224+
this._pattern.last();
225+
}
198226

199227
/** Closes the menu. */
200228
close(opts?: {refocus?: boolean}) {
201-
this._pattern.inputs.parent()?.close(opts);
229+
this._pattern.close(opts);
202230
}
203231

204232
/** Closes all parent menus. */
205233
closeAll(opts?: {refocus?: boolean}) {
206-
const root = this._pattern.root();
207-
208-
if (root instanceof MenuTriggerPattern) {
209-
root.close(opts);
210-
}
211-
212-
if (root instanceof MenuPattern || root instanceof MenuBarPattern) {
213-
root.inputs.activeItem()?.close(opts);
214-
}
234+
this._pattern.closeAll(opts);
215235
}
216236
}
217237

@@ -297,6 +317,21 @@ export class MenuBar<V> {
297317
}
298318
});
299319
}
320+
321+
/** Focuses the previous menu item. */
322+
prev() {
323+
this._pattern.prev();
324+
}
325+
326+
/** Focuses the next menu item. */
327+
next() {
328+
this._pattern.next();
329+
}
330+
331+
/** Closes the menubar and refocuses the root menu bar item. */
332+
close(opts?: {refocus?: boolean}) {
333+
this._pattern.close(opts);
334+
}
300335
}
301336

302337
/**
@@ -361,4 +396,14 @@ export class MenuItem<V> {
361396
parent: computed(() => this.parent?._pattern),
362397
submenu: computed(() => this.submenu()?._pattern),
363398
});
399+
400+
/** Opens the submenu. */
401+
open(opts?: {first?: boolean; last?: boolean}) {
402+
this._pattern.open(opts);
403+
}
404+
405+
/** Closes the submenu. */
406+
close(opts: {refocus?: boolean} = {}) {
407+
this._pattern.close(opts);
408+
}
364409
}

src/aria/private/menu/menu.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,25 @@ export class MenuPattern<V> {
344344
}
345345
}
346346

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

351356
if (root instanceof MenuTriggerPattern) {
352-
root.close({refocus: true});
357+
root.close(opts);
353358
}
354359

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

359364
if (root instanceof MenuPattern) {
360-
root.inputs.activeItem()?.close({refocus: true});
365+
root.inputs.activeItem()?.close(opts);
361366
}
362367
}
363368
}
@@ -495,8 +500,8 @@ export class MenuBarPattern<V> {
495500
}
496501

497502
/** Closes the menubar and refocuses the root menu bar item. */
498-
close() {
499-
this.inputs.activeItem()?.close({refocus: this.isFocused()});
503+
close(opts?: {refocus?: boolean}) {
504+
this.inputs.activeItem()?.close(opts);
500505
}
501506
}
502507

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
@@ -47,7 +47,7 @@ export class MenuContextExample {
4747
menu.element.style.top = `${event.clientY}px`;
4848
menu.element.style.left = `${event.clientX}px`;
4949

50-
setTimeout(() => menu._pattern.first());
50+
setTimeout(() => menu.first());
5151
}
5252
}
5353
}

0 commit comments

Comments
 (0)