@@ -17,9 +17,7 @@ export const enum FocusNext {
17
17
currentItem ,
18
18
}
19
19
20
- /**
21
- * Interface for the elements tracked in the MenuStack.
22
- */
20
+ /** Interface for the elements tracked in the MenuStack. */
23
21
export interface MenuStackItem {
24
22
/** A reference to the previous Menus MenuStack instance. */
25
23
menuStack ?: MenuStack ;
@@ -42,9 +40,20 @@ export const PARENT_OR_NEW_INLINE_MENU_STACK_PROVIDER = {
42
40
useFactory : ( parentMenuStack ?: MenuStack ) => parentMenuStack || MenuStack . inline ( ) ,
43
41
} ;
44
42
43
+ /** Options that can be provided to the close or closeAll methods. */
44
+ export interface CloseOptions {
45
+ /** The element to focus next if the close operation causes the menu stack to become empty. */
46
+ focusNextOnEmpty ?: FocusNext ;
47
+ /** Whether to focus the parent trigger after closing the menu. */
48
+ focusParentTrigger ?: boolean ;
49
+ }
50
+
51
+ /** Event dispatched when a menu is closed. */
45
52
export interface MenuStackCloseEvent {
53
+ /** The menu being closed. */
46
54
item : MenuStackItem | undefined ;
47
- focusParentMenu ?: boolean ;
55
+ /** Whether to focus the parent trigger after closing the menu. */
56
+ focusParentTrigger ?: boolean ;
48
57
}
49
58
50
59
/**
@@ -99,21 +108,19 @@ export class MenuStack {
99
108
* Pop items off of the stack up to and including `lastItem` and emit each on the close
100
109
* observable. If the stack is empty or `lastItem` is not on the stack it does nothing.
101
110
* @param lastItem the last item to pop off the stack.
102
- * @param focusNext the event to emit on the `empty` observable if the method call resulted in an
103
- * empty stack. Does not emit if the stack was initially empty or if `lastItem` was not on the
104
- * @param focusParentMenu Whether to focus the parent menu after closing current one.
105
- * stack.
111
+ * @param options Options that configure behavior on close.
106
112
*/
107
- close ( lastItem : MenuStackItem , focusNext ?: FocusNext , focusParentMenu = false ) {
113
+ close ( lastItem : MenuStackItem , options ?: CloseOptions ) {
114
+ const { focusNextOnEmpty, focusParentTrigger} = { ...options } ;
108
115
if ( this . _elements . indexOf ( lastItem ) >= 0 ) {
109
116
let poppedElement : MenuStackItem | undefined ;
110
117
do {
111
118
poppedElement = this . _elements . pop ( ) ;
112
- this . _close . next ( { item : poppedElement , focusParentMenu } ) ;
119
+ this . _close . next ( { item : poppedElement , focusParentTrigger } ) ;
113
120
} while ( poppedElement !== lastItem ) ;
114
121
115
122
if ( this . isEmpty ( ) ) {
116
- this . _empty . next ( focusNext ) ;
123
+ this . _empty . next ( focusNextOnEmpty ) ;
117
124
}
118
125
}
119
126
}
@@ -137,18 +144,18 @@ export class MenuStack {
137
144
138
145
/**
139
146
* Pop off all MenuStackItems and emit each one on the `close` observable one by one.
140
- * @param focusNext the event to emit on the `empty` observable once the stack is emptied. Does
141
- * not emit if the stack was initially empty.
147
+ * @param options Options that configure behavior on close.
142
148
*/
143
- closeAll ( focusNext ?: FocusNext , focusParentMenu = false ) {
149
+ closeAll ( options ?: CloseOptions ) {
150
+ const { focusNextOnEmpty, focusParentTrigger} = { ...options } ;
144
151
if ( ! this . isEmpty ( ) ) {
145
152
while ( ! this . isEmpty ( ) ) {
146
153
const menuStackItem = this . _elements . pop ( ) ;
147
154
if ( menuStackItem ) {
148
- this . _close . next ( { item : menuStackItem , focusParentMenu } ) ;
155
+ this . _close . next ( { item : menuStackItem , focusParentTrigger } ) ;
149
156
}
150
157
}
151
- this . _empty . next ( focusNext ) ;
158
+ this . _empty . next ( focusNextOnEmpty ) ;
152
159
}
153
160
}
154
161
@@ -167,10 +174,12 @@ export class MenuStack {
167
174
return this . _elements [ this . _elements . length - 1 ] ;
168
175
}
169
176
177
+ /** Whether the menu stack is associated with an inline menu. */
170
178
hasInlineMenu ( ) {
171
179
return this . _hasInlineMenu ;
172
180
}
173
181
182
+ /** Sets whether the menu stack contains the focused element. */
174
183
setHasFocus ( hasFocus : boolean ) {
175
184
this . _hasFocus . next ( hasFocus ) ;
176
185
}
0 commit comments