Skip to content

Commit f52cf99

Browse files
committed
feat(select): support fallback positions
1 parent 7572e34 commit f52cf99

File tree

5 files changed

+137
-18
lines changed

5 files changed

+137
-18
lines changed

src/lib/select/select-animations.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,26 @@ export const transformPlaceholder: AnimationEntryMetadata = trigger('transformPl
4242
* When the panel is removed from the DOM, it simply fades out linearly.
4343
*/
4444
export const transformPanel: AnimationEntryMetadata = trigger('transformPanel', [
45-
state('showing-ltr', style({
45+
state('top-ltr', style({
4646
opacity: 1,
4747
width: 'calc(100% + 32px)',
4848
transform: `translate3d(-16px, -9px, 0) scaleY(1)`
4949
})),
50-
state('showing-rtl', style({
50+
state('top-rtl', style({
5151
opacity: 1,
5252
width: 'calc(100% + 32px)',
5353
transform: `translate3d(16px, -9px, 0) scaleY(1)`
5454
})),
55+
state('bottom-ltr', style({
56+
opacity: 1,
57+
width: 'calc(100% + 32px)',
58+
transform: `translate3d(-16px, 8px, 0) scaleY(1)`
59+
})),
60+
state('bottom-rtl', style({
61+
opacity: 1,
62+
width: 'calc(100% + 32px)',
63+
transform: `translate3d(16px, 8px, 0) scaleY(1)`
64+
})),
5565
transition('void => *', [
5666
style({
5767
opacity: 0,

src/lib/select/select.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
</div>
66

77
<template connected-overlay [origin]="origin" [open]="panelOpen" hasBackdrop (backdropClick)="close()"
8-
backdropClass="md-overlay-transparent-backdrop" [positions]="_positions" [width]="_getWidth()">
8+
backdropClass="md-overlay-transparent-backdrop" [positions]="_positions" [width]="_getWidth()"
9+
(positionChange)="_updateTransformOrigin($event)">
910
<div class="md-select-panel" [@transformPanel]="_getPanelState()" (@transformPanel.done)="_onPanelDone()"
10-
(keydown)="_keyManager.onKeydown($event)">
11+
(keydown)="_keyManager.onKeydown($event)" [style.transformOrigin]="_transformOrigin">
1112
<div class="md-select-content" [@fadeInContent]="'showing'">
1213
<ng-content></ng-content>
1314
</div>

src/lib/select/select.spec.ts

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {MdSelect} from './select';
77
import {MdOption} from './option';
88
import {Dir} from '../core/rtl/dir';
99
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
10+
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
1011

1112
describe('MdSelect', () => {
1213
let overlayContainerElement: HTMLElement;
@@ -19,17 +20,31 @@ describe('MdSelect', () => {
1920
providers: [
2021
{provide: OverlayContainer, useFactory: () => {
2122
overlayContainerElement = document.createElement('div');
23+
overlayContainerElement.style.position = 'fixed';
24+
overlayContainerElement.style.top = '0';
25+
overlayContainerElement.style.left = '0';
26+
document.body.appendChild(overlayContainerElement);
27+
28+
// remove body padding to keep consistent cross-browser
29+
document.body.style.padding = '0';
30+
document.body.style.margin = '0';
31+
2232
return {getContainerElement: () => overlayContainerElement};
2333
}},
2434
{provide: Dir, useFactory: () => {
2535
return dir = { value: 'ltr' };
26-
}}
36+
}},
37+
{provide: ViewportRuler, useClass: FakeViewportRuler}
2738
]
2839
});
2940

3041
TestBed.compileComponents();
3142
}));
3243

44+
afterEach(() => {
45+
document.body.removeChild(overlayContainerElement);
46+
});
47+
3348
describe('overlay panel', () => {
3449
let fixture: ComponentFixture<BasicSelect>;
3550
let trigger: HTMLElement;
@@ -457,19 +472,78 @@ describe('MdSelect', () => {
457472

458473
trigger.click();
459474
fixture.detectChanges();
460-
expect(fixture.componentInstance.select._getPanelState()).toEqual('showing-ltr');
475+
expect(fixture.componentInstance.select._getPanelState()).toEqual('top-ltr');
461476
});
462477

463478
it('should use the rtl panel state when the dir is rtl', () => {
464479
dir.value = 'rtl';
465480

466481
trigger.click();
467482
fixture.detectChanges();
468-
expect(fixture.componentInstance.select._getPanelState()).toEqual('showing-rtl');
483+
expect(fixture.componentInstance.select._getPanelState()).toEqual('top-rtl');
469484
});
470485

471486
});
472487

488+
describe('positioning', () => {
489+
let fixture: ComponentFixture<BasicSelect>;
490+
let trigger: HTMLElement;
491+
492+
beforeEach(() => {
493+
fixture = TestBed.createComponent(BasicSelect);
494+
fixture.detectChanges();
495+
trigger = fixture.debugElement.query(By.css('.md-select-trigger')).nativeElement;
496+
});
497+
498+
it('should open below the trigger if the panel will fit', () => {
499+
trigger.click();
500+
fixture.detectChanges();
501+
502+
const overlayPane = overlayContainerElement.children[0] as HTMLElement;
503+
const overlayRect = overlayPane.getBoundingClientRect();
504+
const triggerRect = trigger.getBoundingClientRect();
505+
506+
// when the select panel opens below the trigger, the tops of the trigger and the overlay
507+
// should be aligned.
508+
expect(overlayRect.top.toFixed(2))
509+
.toEqual(triggerRect.top.toFixed(2), `Expected panel to open below by default.`);
510+
511+
// animation should match the position
512+
expect(fixture.componentInstance.select._getPanelState())
513+
.toEqual('top-ltr', `Expected panel animation values to match the position.`);
514+
expect(fixture.componentInstance.select._transformOrigin)
515+
.toBe('top', `Expected panel animation to originate at the top.`);
516+
});
517+
518+
it('should open above the trigger if there is not space below for the panel', () => {
519+
// Push trigger to the bottom part of viewport, so it doesn't have space to open
520+
// in its default position below the trigger.
521+
trigger.style.position = 'relative';
522+
trigger.style.top = '650px';
523+
524+
trigger.click();
525+
fixture.detectChanges();
526+
527+
const overlayPane = overlayContainerElement.children[0] as HTMLElement;
528+
const overlayRect = overlayPane.getBoundingClientRect();
529+
const triggerRect = trigger.getBoundingClientRect();
530+
531+
// In "above" position, the bottom edges of the overlay and the origin are aligned.
532+
// To find the overlay top, subtract the panel height from the origin's bottom edge.
533+
const expectedTop = triggerRect.bottom - overlayRect.height;
534+
expect(overlayRect.top.toFixed(2))
535+
.toEqual(expectedTop.toFixed(2),
536+
`Expected panel to open above the trigger if below wouldn't fit.`);
537+
538+
// animation should match the position
539+
expect(fixture.componentInstance.select._getPanelState())
540+
.toEqual('bottom-ltr', `Expected panel animation values to match the position.`);
541+
expect(fixture.componentInstance.select._transformOrigin)
542+
.toBe('bottom', `Expected panel animation to originate at the bottom.`);
543+
});
544+
545+
});
546+
473547
describe('accessibility', () => {
474548
let fixture: ComponentFixture<BasicSelect>;
475549

@@ -658,3 +732,15 @@ function dispatchEvent(eventName: string, element: HTMLElement): void {
658732
event.initEvent(eventName, true, true);
659733
element.dispatchEvent(event);
660734
}
735+
736+
class FakeViewportRuler {
737+
getViewportRect() {
738+
return {
739+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
740+
};
741+
}
742+
743+
getViewportScrollPosition() {
744+
return {top: 0, left: 0};
745+
}
746+
}

src/lib/select/select.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {Subscription} from 'rxjs/Subscription';
2121
import {transformPlaceholder, transformPanel, fadeInContent} from './select-animations';
2222
import {ControlValueAccessor, NgControl} from '@angular/forms';
2323
import {coerceBooleanProperty} from '../core/coersion/boolean-property';
24+
import {ConnectedOverlayPositionChange} from '../core/overlay/position/connected-position';
2425

2526
@Component({
2627
moduleId: module.id,
@@ -77,16 +78,29 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
7778
/** View -> model callback called when select has been touched */
7879
_onTouched: Function;
7980

80-
/** This position config ensures that the top left corner of the overlay
81-
* is aligned with with the top left of the origin (overlapping the trigger
82-
* completely). In RTL mode, the top right corners are aligned instead.
81+
/** The value of the select panel's transform-origin property. */
82+
_transformOrigin: string = 'top';
83+
84+
/**
85+
* This position config ensures that the top "start" corner of the overlay
86+
* is aligned with with the top "start" of the origin by default (overlapping
87+
* the trigger completely). If the panel cannot fit below the trigger, it
88+
* will fall back to a position above the trigger.
8389
*/
84-
_positions = [{
85-
originX: 'start',
86-
originY: 'top',
87-
overlayX: 'start',
88-
overlayY: 'top'
89-
}];
90+
_positions = [
91+
{
92+
originX: 'start',
93+
originY: 'top',
94+
overlayX: 'start',
95+
overlayY: 'top',
96+
},
97+
{
98+
originX: 'start',
99+
originY: 'bottom',
100+
overlayX: 'start',
101+
overlayY: 'bottom',
102+
},
103+
];
90104

91105
@ViewChild('trigger') trigger: ElementRef;
92106
@ContentChildren(MdOption) options: QueryList<MdOption>;
@@ -226,7 +240,7 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
226240

227241
/** The animation state of the overlay panel. */
228242
_getPanelState(): string {
229-
return this._isRtl() ? 'showing-rtl' : 'showing-ltr';
243+
return this._isRtl() ? `${this._transformOrigin}-rtl` : `${this._transformOrigin}-ltr`;
230244
}
231245

232246
/** Ensures the panel opens if activated by the keyboard. */
@@ -264,6 +278,14 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
264278
return this.disabled ? '-1' : '0';
265279
}
266280

281+
/**
282+
* Sets the transform-origin property of the panel to ensure that it
283+
* animates in the correct direction based on its positioning.
284+
*/
285+
_updateTransformOrigin(pos: ConnectedOverlayPositionChange): void {
286+
this._transformOrigin = pos.connectionPair.originY;
287+
}
288+
267289
/** Sets up a key manager to listen to keyboard events on the overlay panel. */
268290
private _initKeyManager() {
269291
this._keyManager = new ListKeyManager(this.options);

test/browser-providers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const configuration: { [name: string]: ConfigurationInfo } = {
4444
'Safari8': { unitTest: {target: 'BS', required: false}, e2e: {target: null, required: true}},
4545
'Safari9': { unitTest: {target: 'BS', required: false}, e2e: {target: null, required: true}},
4646
'iOS7': { unitTest: {target: null, required: false}, e2e: {target: null, required: true}},
47-
'iOS8': { unitTest: {target: 'BS', required: true}, e2e: {target: null, required: true}},
47+
'iOS8': { unitTest: {target: null, required: false}, e2e: {target: null, required: true}},
4848
'iOS9': { unitTest: {target: 'BS', required: true}, e2e: {target: null, required: true}},
4949
'WindowsPhone': { unitTest: {target: 'BS', required: false}, e2e: {target: null, required: true}}
5050
};

0 commit comments

Comments
 (0)