Skip to content

Commit d1729bd

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

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

src/aria/menu/menu.ts

Lines changed: 56 additions & 11 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,34 @@ export class Menu<V> {
222232
});
223233
}
224234

225-
// TODO(wagnermaciel): Author close, closeAll, and open methods for each directive.
235+
/** Focuses the previous menu item. */
236+
prev() {
237+
this._pattern.prev();
238+
}
239+
240+
/** Focuses the next menu item. */
241+
next() {
242+
this._pattern.next();
243+
}
244+
245+
/** Focuses the first menu item. */
246+
first() {
247+
this._pattern.first();
248+
}
249+
250+
/** Focuses the last menu item. */
251+
last() {
252+
this._pattern.last();
253+
}
226254

227255
/** Closes the menu. */
228256
close(opts?: {refocus?: boolean}) {
229-
this._pattern.inputs.parent()?.close(opts);
257+
this._pattern.close(opts);
230258
}
231259

232260
/** Closes all parent menus. */
233261
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-
}
262+
this._pattern.closeAll(opts);
243263
}
244264
}
245265

@@ -325,6 +345,21 @@ export class MenuBar<V> {
325345
}
326346
});
327347
}
348+
349+
/** Focuses the previous menu item. */
350+
prev() {
351+
this._pattern.prev();
352+
}
353+
354+
/** Focuses the next menu item. */
355+
next() {
356+
this._pattern.next();
357+
}
358+
359+
/** Closes the menubar and refocuses the root menu bar item. */
360+
close(opts?: {refocus?: boolean}) {
361+
this._pattern.close(opts);
362+
}
328363
}
329364

330365
/**
@@ -402,6 +437,16 @@ export class MenuItem<V> {
402437
onFocusIn() {
403438
this.hasBeenFocused.set(true);
404439
}
440+
441+
/** Opens the submenu. */
442+
open(opts?: {first?: boolean; last?: boolean}) {
443+
this._pattern.open(opts);
444+
}
445+
446+
/** Closes the submenu. */
447+
close(opts: {refocus?: boolean} = {}) {
448+
this._pattern.close(opts);
449+
}
405450
}
406451

407452
/** 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 = opts ?? {refocus: this.isFocused()};
506+
507+
this.inputs.activeItem()?.close(opts);
501508
}
502509
}
503510

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

51-
setTimeout(() => menu._pattern.first());
51+
setTimeout(() => menu.first());
5252
}
5353
}
5454
}

0 commit comments

Comments
 (0)