Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(menu): update menu to use overlay's new rtl support #1687

Merged
merged 1 commit into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/lib/core/style/_menu-common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@ $md-menu-side-padding: 16px !default;
&.md-menu-before.md-menu-above {
transform-origin: right bottom;
}

[dir='rtl'] & {
&.md-menu-after.md-menu-below {
transform-origin: right top;
}

&.md-menu-after.md-menu-above {
transform-origin: right bottom;
}

&.md-menu-before.md-menu-below {
transform-origin: left top;
}

&.md-menu-before.md-menu-above {
transform-origin: left bottom;
}
}
}
23 changes: 17 additions & 6 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {
AfterViewInit,
Directive,
ElementRef,
EventEmitter,
Input,
OnDestroy,
Optional,
Output,
EventEmitter,
Renderer,
ViewContainerRef,
AfterViewInit,
OnDestroy,
Renderer
} from '@angular/core';
import {MdMenuPanel} from './menu-panel';
import {MdMenuMissingError} from './menu-errors';
import {
isFakeMousedownFromScreenReader,
Dir,
LayoutDirection,
Overlay,
OverlayState,
OverlayRef,
Expand Down Expand Up @@ -51,7 +54,8 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
@Output() onMenuClose = new EventEmitter<void>();

constructor(private _overlay: Overlay, private _element: ElementRef,
private _viewContainerRef: ViewContainerRef, private _renderer: Renderer) {}
private _viewContainerRef: ViewContainerRef, private _renderer: Renderer,
@Optional() private _dir: Dir) {}

ngAfterViewInit() {
this._checkMenu();
Expand Down Expand Up @@ -98,6 +102,11 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
this._renderer.invokeElementMethod(this._element.nativeElement, 'focus');
}

/** The text direction of the containing app. */
get dir(): LayoutDirection {
return this._dir && this._dir.value === 'rtl' ? 'rtl' : 'ltr';
}

/**
* This method ensures that the menu closes when the overlay backdrop is clicked.
* We do not use first() here because doing so would not catch clicks from within
Expand Down Expand Up @@ -173,9 +182,11 @@ export class MdMenuTrigger implements AfterViewInit, OnDestroy {
*/
private _getOverlayConfig(): OverlayState {
const overlayState = new OverlayState();
overlayState.positionStrategy = this._getPosition();
overlayState.positionStrategy = this._getPosition()
.withDirection(this.dir);
overlayState.hasBackdrop = true;
overlayState.backdropClass = 'md-overlay-transparent-backdrop';
overlayState.direction = this.dir;
return overlayState;
}

Expand Down
16 changes: 16 additions & 0 deletions src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import {
MenuPositionY
} from './menu';
import {OverlayContainer} from '../core/overlay/overlay-container';
import {Dir, LayoutDirection} from '../core/rtl/dir';

describe('MdMenu', () => {
let overlayContainerElement: HTMLElement;
let dir: LayoutDirection = 'ltr';

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -27,6 +29,9 @@ describe('MdMenu', () => {
{provide: OverlayContainer, useFactory: () => {
overlayContainerElement = document.createElement('div');
return {getContainerElement: () => overlayContainerElement};
}},
{provide: Dir, useFactory: () => {
return {value: dir};
}}
]
});
Expand Down Expand Up @@ -74,6 +79,17 @@ describe('MdMenu', () => {
}).not.toThrowError();
});

it('should set the panel direction based on the trigger direction', () => {
dir = 'rtl';
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

const overlayPane = overlayContainerElement.children[0];
expect(overlayPane.getAttribute('dir')).toEqual('rtl');
});

describe('positions', () => {
it('should append md-menu-after and md-menu-below classes by default', () => {
const fixture = TestBed.createComponent(SimpleMenu);
Expand Down