Skip to content

Commit 7e8d4b8

Browse files
committed
feat(menu): add injection token for overriding the default options
Adds a new injection token that allows the consumer to override the default values for the `xPosition`, `yPosition` and `overlapTrigger` options. Fixes #5479.
1 parent b00f838 commit 7e8d4b8

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/lib/menu/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {NgModule} from '@angular/core';
1010
import {CommonModule} from '@angular/common';
1111
import {OverlayModule, MdCommonModule} from '../core';
12-
import {MdMenu} from './menu-directive';
12+
import {MdMenu, MD_MENU_DEFAULT_OPTIONS} from './menu-directive';
1313
import {MdMenuItem} from './menu-item';
1414
import {MdMenuTrigger} from './menu-trigger';
1515
import {MdRippleModule} from '../core/ripple/index';
@@ -24,6 +24,14 @@ import {MdRippleModule} from '../core/ripple/index';
2424
],
2525
exports: [MdMenu, MdMenuItem, MdMenuTrigger, MdCommonModule],
2626
declarations: [MdMenu, MdMenuItem, MdMenuTrigger],
27+
providers: [{
28+
provide: MD_MENU_DEFAULT_OPTIONS,
29+
useValue: {
30+
overlapTrigger: true,
31+
xPosition: 'after',
32+
yPosition: 'below',
33+
},
34+
}],
2735
})
2836
export class MdMenuModule {}
2937

src/lib/menu/menu-directive.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919
ViewChild,
2020
ViewEncapsulation,
2121
ElementRef,
22+
InjectionToken,
23+
Inject,
2224
} from '@angular/core';
2325
import {MenuPositionX, MenuPositionY} from './menu-positions';
2426
import {throwMdMenuInvalidPositionX, throwMdMenuInvalidPositionY} from './menu-errors';
@@ -29,6 +31,16 @@ import {Subscription} from 'rxjs/Subscription';
2931
import {transformMenu, fadeInItems} from './menu-animations';
3032
import {ESCAPE} from '../core/keyboard/keycodes';
3133

34+
/** Default `md-menu` options that can be overriden. */
35+
export interface MdMenuDefaultOptions {
36+
xPosition: MenuPositionX;
37+
yPosition: MenuPositionY;
38+
overlapTrigger: boolean;
39+
}
40+
41+
/** Injection token to be used to override the default options for `md-menu`. */
42+
export const MD_MENU_DEFAULT_OPTIONS =
43+
new InjectionToken<MdMenuDefaultOptions>('md-menu-default-options');
3244

3345
@Component({
3446
moduleId: module.id,
@@ -44,8 +56,8 @@ import {ESCAPE} from '../core/keyboard/keycodes';
4456
})
4557
export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
4658
private _keyManager: FocusKeyManager;
47-
private _xPosition: MenuPositionX = 'after';
48-
private _yPosition: MenuPositionY = 'below';
59+
private _xPosition: MenuPositionX = this._defaultOptions.xPosition;
60+
private _yPosition: MenuPositionY = this._defaultOptions.yPosition;
4961

5062
/** Subscription to tab events on the menu panel */
5163
private _tabSubscription: Subscription;
@@ -81,7 +93,7 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
8193
@ContentChildren(MdMenuItem) items: QueryList<MdMenuItem>;
8294

8395
/** Whether the menu should overlap its trigger. */
84-
@Input() overlapTrigger = true;
96+
@Input() overlapTrigger = this._defaultOptions.overlapTrigger;
8597

8698
/**
8799
* This method takes classes set on the host md-menu element and applies them on the
@@ -105,7 +117,9 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
105117
/** Event emitted when the menu is closed. */
106118
@Output() close = new EventEmitter<void>();
107119

108-
constructor(private _elementRef: ElementRef) { }
120+
constructor(
121+
private _elementRef: ElementRef,
122+
@Inject(MD_MENU_DEFAULT_OPTIONS) private _defaultOptions: MdMenuDefaultOptions) { }
109123

110124
ngAfterContentInit() {
111125
this._keyManager = new FocusKeyManager(this.items).withWrap();

src/lib/menu/menu.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
MdMenuTrigger,
1616
MdMenuPanel,
1717
MenuPositionX,
18-
MenuPositionY
18+
MenuPositionY,
19+
MdMenu,
20+
MD_MENU_DEFAULT_OPTIONS,
1921
} from './index';
2022
import {OverlayContainer} from '../core/overlay/overlay-container';
2123
import {Directionality, Direction} from '../core/bidi/index';
@@ -487,6 +489,29 @@ describe('MdMenu', () => {
487489
});
488490
});
489491

492+
describe('MdMenu default overrides', () => {
493+
beforeEach(async(() => {
494+
TestBed.configureTestingModule({
495+
imports: [MdMenuModule, NoopAnimationsModule],
496+
declarations: [SimpleMenu],
497+
providers: [{
498+
provide: MD_MENU_DEFAULT_OPTIONS,
499+
useValue: {overlapTrigger: false, xPosition: 'before', yPosition: 'above'},
500+
}],
501+
}).compileComponents();
502+
}));
503+
504+
it('should allow for the default menu options to be overriden', () => {
505+
const fixture = TestBed.createComponent(SimpleMenu);
506+
fixture.detectChanges();
507+
const menu = fixture.componentInstance.menu;
508+
509+
expect(menu.overlapTrigger).toBe(false);
510+
expect(menu.xPosition).toBe('before');
511+
expect(menu.yPosition).toBe('above');
512+
});
513+
});
514+
490515
@Component({
491516
template: `
492517
<button [mdMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
@@ -499,6 +524,7 @@ describe('MdMenu', () => {
499524
class SimpleMenu {
500525
@ViewChild(MdMenuTrigger) trigger: MdMenuTrigger;
501526
@ViewChild('triggerEl') triggerEl: ElementRef;
527+
@ViewChild('menu') menu: MdMenu;
502528
closeCallback = jasmine.createSpy('menu closed callback');
503529
}
504530

src/lib/menu/menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export {MdMenu} from './menu-directive';
9+
export {MdMenu, MdMenuDefaultOptions, MD_MENU_DEFAULT_OPTIONS} from './menu-directive';
1010
export {MdMenuItem} from './menu-item';
1111
export {MdMenuTrigger} from './menu-trigger';
1212
export {MdMenuPanel} from './menu-panel';

0 commit comments

Comments
 (0)