Skip to content

Commit

Permalink
feat(MenuSurface): Add opening event for menus.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Adds #notifyOpening method to menu surface adapter.

PiperOrigin-RevId: 444830518
  • Loading branch information
Yavanosta authored and copybara-github committed Apr 27, 2022
1 parent 3ab9565 commit 53b3cad
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/mdc-menu-surface/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ export interface MDCMenuSurfaceAdapter {

/** Emits an event when the menu surface is opened. */
notifyOpen(): void;

/** Emits an event when the menu surface is opening. */
notifyOpening(): void;
}
2 changes: 2 additions & 0 deletions packages/mdc-menu-surface/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export class MDCMenuSurface extends MDCComponent<MDCMenuSurfaceFoundation> {
},
notifyOpen: () =>
this.emit(MDCMenuSurfaceFoundation.strings.OPENED_EVENT, {}),
notifyOpening: () =>
this.emit(MDCMenuSurfaceFoundation.strings.OPENING_EVENT, {}),
isElementInContainer: (el) => this.root.contains(el),
isRtl: () =>
getComputedStyle(this.root).getPropertyValue('direction') === 'rtl',
Expand Down
1 change: 1 addition & 0 deletions packages/mdc-menu-surface/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const strings = {
CLOSED_EVENT: 'MDCMenuSurface:closed',
CLOSING_EVENT: 'MDCMenuSurface:closing',
OPENED_EVENT: 'MDCMenuSurface:opened',
OPENING_EVENT: 'MDCMenuSurface:opening',
FOCUSABLE_ELEMENTS: [
'button:not(:disabled)',
'[href]:not([aria-disabled="true"])',
Expand Down
4 changes: 3 additions & 1 deletion packages/mdc-menu-surface/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export class MDCMenuSurfaceFoundation extends
restoreFocus: () => undefined,

notifyClose: () => undefined,
notifyOpen: () => undefined,
notifyClosing: () => undefined,
notifyOpen: () => undefined,
notifyOpening: () => undefined,
};
// tslint:enable:object-literal-sort-keys
}
Expand Down Expand Up @@ -238,6 +239,7 @@ export class MDCMenuSurfaceFoundation extends
return;
}

this.adapter.notifyOpening();
this.adapter.saveFocus();

if (this.isQuickOpen) {
Expand Down
9 changes: 9 additions & 0 deletions packages/mdc-menu-surface/test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,15 @@ describe('MDCMenuSurface', () => {
expect(handler).toHaveBeenCalled();
});

it(`adapter#notifyOpening fires an ${strings.OPENING_EVENT} custom event`,
() => {
const {root, component} = setupTest();
const handler = jasmine.createSpy('notifyOpening handler');
root.addEventListener(strings.OPENING_EVENT, handler);
(component.getDefaultFoundation() as any).adapter.notifyOpening();
expect(handler).toHaveBeenCalled();
});

it('adapter#restoreFocus restores focus saved by adapter#saveFocus', () => {
const {root, component} = setupTest({open: true});
const button = document.createElement('button');
Expand Down
8 changes: 8 additions & 0 deletions packages/mdc-menu-surface/test/foundation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ describe('MDCMenuSurfaceFoundation', () => {
'notifyClose',
'notifyClosing',
'notifyOpen',
'notifyOpening',
'isElementInContainer',
'isRtl',
'setTransformOrigin',
Expand Down Expand Up @@ -264,6 +265,13 @@ describe('MDCMenuSurfaceFoundation', () => {
.toHaveBeenCalledWith(cssClasses.ANIMATING_OPEN);
});

testFoundation(
'#open emits the opening event at the beginning of the animation',
({foundation, mockAdapter}) => {
foundation.open();
expect(mockAdapter.notifyOpening).toHaveBeenCalled();
});

testFoundation(
'#open emits the open event at the end of the animation',
({foundation, mockAdapter}) => {
Expand Down

0 comments on commit 53b3cad

Please sign in to comment.