Skip to content

Commit

Permalink
feat(menu): pass role to ionWillClose and ionDidClose events (#29954)
Browse files Browse the repository at this point in the history
- Adds the `MenuCloseEventDetail` interface which includes an optional `role` property
- The `ionWillClose` and `ionDidClose` emit the `role` property for the following scenarios:
  - A role of `'gesture'` when dragging the menu closed
- A role of `'backdrop'` when clicking on the backdrop to close the menu
- A role of `'backdrop'` when the the menu is closed using the escape key
- A role of `undefined` when the menu is closed from a button inside of
the menu
  • Loading branch information
brandyscarney authored and tanner-reits committed Nov 4, 2024
1 parent 2d6eeee commit ee2fa19
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 42 deletions.
8 changes: 4 additions & 4 deletions core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1000,15 +1000,15 @@ ion-menu,prop,menuId,string | undefined,undefined,false,true
ion-menu,prop,side,"end" | "start",'start',false,true
ion-menu,prop,swipeGesture,boolean,true,false,false
ion-menu,prop,type,"overlay" | "push" | "reveal" | undefined,undefined,false,false
ion-menu,method,close,close(animated?: boolean) => Promise<boolean>
ion-menu,method,close,close(animated?: boolean, role?: string) => Promise<boolean>
ion-menu,method,isActive,isActive() => Promise<boolean>
ion-menu,method,isOpen,isOpen() => Promise<boolean>
ion-menu,method,open,open(animated?: boolean) => Promise<boolean>
ion-menu,method,setOpen,setOpen(shouldOpen: boolean, animated?: boolean) => Promise<boolean>
ion-menu,method,setOpen,setOpen(shouldOpen: boolean, animated?: boolean, role?: string) => Promise<boolean>
ion-menu,method,toggle,toggle(animated?: boolean) => Promise<boolean>
ion-menu,event,ionDidClose,void,true
ion-menu,event,ionDidClose,MenuCloseEventDetail,true
ion-menu,event,ionDidOpen,void,true
ion-menu,event,ionWillClose,void,true
ion-menu,event,ionWillClose,MenuCloseEventDetail,true
ion-menu,event,ionWillOpen,void,true
ion-menu,css-prop,--background,ios
ion-menu,css-prop,--background,md
Expand Down
16 changes: 8 additions & 8 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ScrollBaseDetail, ScrollDetail } from "./components/content/content-int
import { DatetimeChangeEventDetail, DatetimeHighlight, DatetimeHighlightCallback, DatetimeHourCycle, DatetimePresentation, FormatOptions, TitleSelectedDatesFormatter } from "./components/datetime/datetime-interface";
import { SpinnerTypes } from "./components/spinner/spinner-configs";
import { InputChangeEventDetail, InputInputEventDetail } from "./components/input/input-interface";
import { MenuChangeEventDetail, MenuType, Side } from "./components/menu/menu-interface";
import { MenuChangeEventDetail, MenuCloseEventDetail, MenuType, Side } from "./components/menu/menu-interface";
import { ModalBreakpointChangeEventDetail, ModalHandleBehavior } from "./components/modal/modal-interface";
import { NavComponent, NavComponentWithProps, NavOptions, RouterOutletOptions, SwipeGestureHandler, TransitionDoneFn, TransitionInstruction } from "./components/nav/nav-interface";
import { ViewController } from "./components/nav/view-controller";
Expand Down Expand Up @@ -53,7 +53,7 @@ export { ScrollBaseDetail, ScrollDetail } from "./components/content/content-int
export { DatetimeChangeEventDetail, DatetimeHighlight, DatetimeHighlightCallback, DatetimeHourCycle, DatetimePresentation, FormatOptions, TitleSelectedDatesFormatter } from "./components/datetime/datetime-interface";
export { SpinnerTypes } from "./components/spinner/spinner-configs";
export { InputChangeEventDetail, InputInputEventDetail } from "./components/input/input-interface";
export { MenuChangeEventDetail, MenuType, Side } from "./components/menu/menu-interface";
export { MenuChangeEventDetail, MenuCloseEventDetail, MenuType, Side } from "./components/menu/menu-interface";
export { ModalBreakpointChangeEventDetail, ModalHandleBehavior } from "./components/modal/modal-interface";
export { NavComponent, NavComponentWithProps, NavOptions, RouterOutletOptions, SwipeGestureHandler, TransitionDoneFn, TransitionInstruction } from "./components/nav/nav-interface";
export { ViewController } from "./components/nav/view-controller";
Expand Down Expand Up @@ -1596,7 +1596,7 @@ export namespace Components {
/**
* Closes the menu. If the menu is already closed or it can't be closed, it returns `false`.
*/
"close": (animated?: boolean) => Promise<boolean>;
"close": (animated?: boolean, role?: string) => Promise<boolean>;
/**
* The `id` of the main content. When using a router this is typically `ion-router-outlet`. When not using a router, this is typically your main view's `ion-content`. This is not the id of the `ion-content` inside of your `ion-menu`.
*/
Expand Down Expand Up @@ -1628,7 +1628,7 @@ export namespace Components {
/**
* Opens or closes the button. If the operation can't be completed successfully, it returns `false`.
*/
"setOpen": (shouldOpen: boolean, animated?: boolean) => Promise<boolean>;
"setOpen": (shouldOpen: boolean, animated?: boolean, role?: string) => Promise<boolean>;
/**
* Which side of the view the menu should be placed.
*/
Expand Down Expand Up @@ -3969,9 +3969,9 @@ declare global {
};
interface HTMLIonMenuElementEventMap {
"ionWillOpen": void;
"ionWillClose": void;
"ionWillClose": MenuCloseEventDetail;
"ionDidOpen": void;
"ionDidClose": void;
"ionDidClose": MenuCloseEventDetail;
"ionMenuChange": MenuChangeEventDetail;
}
interface HTMLIonMenuElement extends Components.IonMenu, HTMLStencilElement {
Expand Down Expand Up @@ -6364,7 +6364,7 @@ declare namespace LocalJSX {
/**
* Emitted when the menu is closed.
*/
"onIonDidClose"?: (event: IonMenuCustomEvent<void>) => void;
"onIonDidClose"?: (event: IonMenuCustomEvent<MenuCloseEventDetail>) => void;
/**
* Emitted when the menu is open.
*/
Expand All @@ -6376,7 +6376,7 @@ declare namespace LocalJSX {
/**
* Emitted when the menu is about to be closed.
*/
"onIonWillClose"?: (event: IonMenuCustomEvent<void>) => void;
"onIonWillClose"?: (event: IonMenuCustomEvent<MenuCloseEventDetail>) => void;
/**
* Emitted when the menu is about to be opened.
*/
Expand Down
8 changes: 6 additions & 2 deletions core/src/components/menu/menu-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface MenuI {
close(animated?: boolean): Promise<boolean>;
toggle(animated?: boolean): Promise<boolean>;
setOpen(shouldOpen: boolean, animated?: boolean): Promise<boolean>;
_setOpen(shouldOpen: boolean, animated?: boolean): Promise<boolean>;
_setOpen(shouldOpen: boolean, animated?: boolean, role?: string): Promise<boolean>;
}

export interface MenuControllerI {
Expand All @@ -42,14 +42,18 @@ export interface MenuControllerI {
_createAnimation(type: string, menuCmp: MenuI): Promise<Animation>;
_register(menu: MenuI): void;
_unregister(menu: MenuI): void;
_setOpen(menu: MenuI, shouldOpen: boolean, animated: boolean): Promise<boolean>;
_setOpen(menu: MenuI, shouldOpen: boolean, animated: boolean, role?: string): Promise<boolean>;
}

export interface MenuChangeEventDetail {
disabled: boolean;
open: boolean;
}

export interface MenuCloseEventDetail {
role?: string;
}

export interface MenuCustomEvent<T = any> extends CustomEvent {
detail: T;
target: HTMLIonMenuElement;
Expand Down
40 changes: 20 additions & 20 deletions core/src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { shouldUseCloseWatcher } from '@utils/hardware-back-button';
import type { Attributes } from '@utils/helpers';
import { inheritAriaAttributes, assert, clamp, isEndSide as isEnd } from '@utils/helpers';
import { menuController } from '@utils/menu-controller';
import { getPresentedOverlay } from '@utils/overlays';
import { BACKDROP, GESTURE, getPresentedOverlay } from '@utils/overlays';
import { hostContext } from '@utils/theme';

import { config } from '../../global/config';
import { getIonMode } from '../../global/ionic-global';
import type { Animation, Gesture, GestureDetail } from '../../interface';

import type { MenuChangeEventDetail, MenuI, MenuType, Side } from './menu-interface';
import type { MenuChangeEventDetail, MenuCloseEventDetail, MenuI, MenuType, Side } from './menu-interface';

const iosEasing = 'cubic-bezier(0.32,0.72,0,1)';
const mdEasing = 'cubic-bezier(0.0,0.0,0.2,1)';
Expand Down Expand Up @@ -179,7 +179,7 @@ export class Menu implements ComponentInterface, MenuI {
/**
* Emitted when the menu is about to be closed.
*/
@Event() ionWillClose!: EventEmitter<void>;
@Event() ionWillClose!: EventEmitter<MenuCloseEventDetail>;
/**
* Emitted when the menu is open.
*/
Expand All @@ -188,7 +188,7 @@ export class Menu implements ComponentInterface, MenuI {
/**
* Emitted when the menu is closed.
*/
@Event() ionDidClose!: EventEmitter<void>;
@Event() ionDidClose!: EventEmitter<MenuCloseEventDetail>;

/**
* Emitted when the menu state is changed.
Expand Down Expand Up @@ -331,14 +331,14 @@ export class Menu implements ComponentInterface, MenuI {
if (shouldClose) {
ev.preventDefault();
ev.stopPropagation();
this.close();
this.close(undefined, BACKDROP);
}
}
}

onKeydown(ev: KeyboardEvent) {
if (ev.key === 'Escape') {
this.close();
this.close(undefined, BACKDROP);
}
}

Expand Down Expand Up @@ -375,8 +375,8 @@ export class Menu implements ComponentInterface, MenuI {
* it returns `false`.
*/
@Method()
close(animated = true): Promise<boolean> {
return this.setOpen(false, animated);
close(animated = true, role?: string): Promise<boolean> {
return this.setOpen(false, animated, role);
}

/**
Expand All @@ -393,8 +393,8 @@ export class Menu implements ComponentInterface, MenuI {
* If the operation can't be completed successfully, it returns `false`.
*/
@Method()
setOpen(shouldOpen: boolean, animated = true): Promise<boolean> {
return menuController._setOpen(this, shouldOpen, animated);
setOpen(shouldOpen: boolean, animated = true, role?: string): Promise<boolean> {
return menuController._setOpen(this, shouldOpen, animated, role);
}

private trapKeyboardFocus(ev: Event, doc: Document) {
Expand Down Expand Up @@ -438,13 +438,13 @@ export class Menu implements ComponentInterface, MenuI {
}
}

async _setOpen(shouldOpen: boolean, animated = true): Promise<boolean> {
async _setOpen(shouldOpen: boolean, animated = true, role?: string): Promise<boolean> {
// If the menu is disabled or it is currently being animated, let's do nothing
if (!this._isActive() || this.isAnimating || shouldOpen === this._isOpen) {
return false;
}

this.beforeAnimation(shouldOpen);
this.beforeAnimation(shouldOpen, role);

await this.loadAnimation();
await this.startAnimation(shouldOpen, animated);
Expand All @@ -459,7 +459,7 @@ export class Menu implements ComponentInterface, MenuI {
return false;
}

this.afterAnimation(shouldOpen);
this.afterAnimation(shouldOpen, role);

return true;
}
Expand Down Expand Up @@ -542,7 +542,7 @@ export class Menu implements ComponentInterface, MenuI {
}

private onWillStart(): Promise<void> {
this.beforeAnimation(!this._isOpen);
this.beforeAnimation(!this._isOpen, GESTURE);
return this.loadAnimation();
}

Expand Down Expand Up @@ -624,11 +624,11 @@ export class Menu implements ComponentInterface, MenuI {

this.animation
.easing('cubic-bezier(0.4, 0.0, 0.6, 1)')
.onFinish(() => this.afterAnimation(shouldOpen), { oneTimeCallback: true })
.onFinish(() => this.afterAnimation(shouldOpen, GESTURE), { oneTimeCallback: true })
.progressEnd(playTo ? 1 : 0, this._isOpen ? 1 - newStepValue : newStepValue, 300);
}

private beforeAnimation(shouldOpen: boolean) {
private beforeAnimation(shouldOpen: boolean, role?: string) {
assert(!this.isAnimating, '_before() should not be called while animating');

// this places the menu into the correct location before it animates in
Expand Down Expand Up @@ -671,11 +671,11 @@ export class Menu implements ComponentInterface, MenuI {
if (shouldOpen) {
this.ionWillOpen.emit();
} else {
this.ionWillClose.emit();
this.ionWillClose.emit({ role });
}
}

private afterAnimation(isOpen: boolean) {
private afterAnimation(isOpen: boolean, role?: string) {
// keep opening/closing the menu disabled for a touch more yet
// only add listeners/css if it's enabled and isOpen
// and only remove listeners/css if it's not open
Expand Down Expand Up @@ -731,7 +731,7 @@ export class Menu implements ComponentInterface, MenuI {
}

// emit close event
this.ionDidClose.emit();
this.ionDidClose.emit({ role });

// undo focus trapping so multiple menus don't collide
document.removeEventListener('focus', this.handleFocus, true);
Expand Down Expand Up @@ -767,7 +767,7 @@ export class Menu implements ComponentInterface, MenuI {
* If the menu is disabled then we should
* forcibly close the menu even if it is open.
*/
this.afterAnimation(false);
this.afterAnimation(false, GESTURE);
}
}

Expand Down
17 changes: 16 additions & 1 deletion core/src/components/menu/test/basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
</ion-header>
<ion-content>
<ion-list>
<ion-button id="start-menu-button">Button</ion-button>
<ion-menu-toggle>
<ion-button id="start-menu-button">Button</ion-button>
</ion-menu-toggle>
<ion-item>Menu Item</ion-item>
<ion-item>Menu Item</ion-item>
<ion-item>Menu Item</ion-item>
Expand Down Expand Up @@ -125,6 +127,19 @@
</ion-app>

<script>
window.addEventListener('ionWillOpen', function (e) {
console.log('ionWillOpen', e);
});
window.addEventListener('ionDidOpen', function (e) {
console.log('ionDidOpen', e);
});
window.addEventListener('ionWillClose', function (e) {
console.log('ionWillClose', e);
});
window.addEventListener('ionDidClose', function (e) {
console.log('ionDidClose', e);
});

async function openStart() {
// Open the menu by menu-id
await menuController.enable(true, 'start-menu');
Expand Down
Loading

0 comments on commit ee2fa19

Please sign in to comment.