@@ -17,9 +17,7 @@ export const enum FocusNext {
1717 currentItem ,
1818}
1919
20- /**
21- * Interface for the elements tracked in the MenuStack.
22- */
20+ /** Interface for the elements tracked in the MenuStack. */
2321export interface MenuStackItem {
2422 /** A reference to the previous Menus MenuStack instance. */
2523 menuStack ?: MenuStack ;
@@ -42,9 +40,20 @@ export const PARENT_OR_NEW_INLINE_MENU_STACK_PROVIDER = {
4240 useFactory : ( parentMenuStack ?: MenuStack ) => parentMenuStack || MenuStack . inline ( ) ,
4341} ;
4442
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. */
4552export interface MenuStackCloseEvent {
53+ /** The menu being closed. */
4654 item : MenuStackItem | undefined ;
47- focusParentMenu ?: boolean ;
55+ /** Whether to focus the parent trigger after closing the menu. */
56+ focusParentTrigger ?: boolean ;
4857}
4958
5059/**
@@ -99,21 +108,19 @@ export class MenuStack {
99108 * Pop items off of the stack up to and including `lastItem` and emit each on the close
100109 * observable. If the stack is empty or `lastItem` is not on the stack it does nothing.
101110 * @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.
106112 */
107- close ( lastItem : MenuStackItem , focusNext ?: FocusNext , focusParentMenu = false ) {
113+ close ( lastItem : MenuStackItem , options ?: CloseOptions ) {
114+ const { focusNextOnEmpty, focusParentTrigger} = { ...options } ;
108115 if ( this . _elements . indexOf ( lastItem ) >= 0 ) {
109116 let poppedElement : MenuStackItem | undefined ;
110117 do {
111118 poppedElement = this . _elements . pop ( ) ;
112- this . _close . next ( { item : poppedElement , focusParentMenu } ) ;
119+ this . _close . next ( { item : poppedElement , focusParentTrigger } ) ;
113120 } while ( poppedElement !== lastItem ) ;
114121
115122 if ( this . isEmpty ( ) ) {
116- this . _empty . next ( focusNext ) ;
123+ this . _empty . next ( focusNextOnEmpty ) ;
117124 }
118125 }
119126 }
@@ -137,18 +144,18 @@ export class MenuStack {
137144
138145 /**
139146 * 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.
142148 */
143- closeAll ( focusNext ?: FocusNext , focusParentMenu = false ) {
149+ closeAll ( options ?: CloseOptions ) {
150+ const { focusNextOnEmpty, focusParentTrigger} = { ...options } ;
144151 if ( ! this . isEmpty ( ) ) {
145152 while ( ! this . isEmpty ( ) ) {
146153 const menuStackItem = this . _elements . pop ( ) ;
147154 if ( menuStackItem ) {
148- this . _close . next ( { item : menuStackItem , focusParentMenu } ) ;
155+ this . _close . next ( { item : menuStackItem , focusParentTrigger } ) ;
149156 }
150157 }
151- this . _empty . next ( focusNext ) ;
158+ this . _empty . next ( focusNextOnEmpty ) ;
152159 }
153160 }
154161
@@ -167,10 +174,12 @@ export class MenuStack {
167174 return this . _elements [ this . _elements . length - 1 ] ;
168175 }
169176
177+ /** Whether the menu stack is associated with an inline menu. */
170178 hasInlineMenu ( ) {
171179 return this . _hasInlineMenu ;
172180 }
173181
182+ /** Sets whether the menu stack contains the focused element. */
174183 setHasFocus ( hasFocus : boolean ) {
175184 this . _hasFocus . next ( hasFocus ) ;
176185 }
0 commit comments