Skip to content

Commit 1e3402d

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

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
@@ -71,6 +71,16 @@ export class MenuTrigger<V> {
7171
element: computed(() => this._elementRef.nativeElement),
7272
submenu: computed(() => this.submenu()?._pattern),
7373
});
74+
75+
/** Opens the menu. */
76+
open(opts?: {first?: boolean; last?: boolean}) {
77+
this._pattern.open(opts);
78+
}
79+
80+
/** Closes the menu. */
81+
close(opts: {refocus?: boolean} = {}) {
82+
this._pattern.close(opts);
83+
}
7484
}
7585

7686
/**
@@ -193,24 +203,34 @@ export class Menu<V> {
193203
});
194204
}
195205

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

198226
/** Closes the menu. */
199227
close(opts?: {refocus?: boolean}) {
200-
this._pattern.inputs.parent()?.close(opts);
228+
this._pattern.close(opts);
201229
}
202230

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

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

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

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)