Skip to content

Commit 7578394

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

File tree

5 files changed

+139
-18
lines changed

5 files changed

+139
-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: 91 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,33 @@ describe('MdSelect', () => {
1920
providers: [
2021
{provide: OverlayContainer, useFactory: () => {
2122
overlayContainerElement = document.createElement('div');
23+
24+
// add fixed positioning to match real overlay container styles
25+
overlayContainerElement.style.position = 'fixed';
26+
overlayContainerElement.style.top = '0';
27+
overlayContainerElement.style.left = '0';
28+
document.body.appendChild(overlayContainerElement);
29+
30+
// remove body padding to keep consistent cross-browser
31+
document.body.style.padding = '0';
32+
document.body.style.margin = '0';
33+
2234
return {getContainerElement: () => overlayContainerElement};
2335
}},
2436
{provide: Dir, useFactory: () => {
2537
return dir = { value: 'ltr' };
26-
}}
38+
}},
39+
{provide: ViewportRuler, useClass: FakeViewportRuler}
2740
]
2841
});
2942

3043
TestBed.compileComponents();
3144
}));
3245

46+
afterEach(() => {
47+
document.body.removeChild(overlayContainerElement);
48+
});
49+
3350
describe('overlay panel', () => {
3451
let fixture: ComponentFixture<BasicSelect>;
3552
let trigger: HTMLElement;
@@ -457,19 +474,78 @@ describe('MdSelect', () => {
457474

458475
trigger.click();
459476
fixture.detectChanges();
460-
expect(fixture.componentInstance.select._getPanelState()).toEqual('showing-ltr');
477+
expect(fixture.componentInstance.select._getPanelState()).toEqual('top-ltr');
461478
});
462479

463480
it('should use the rtl panel state when the dir is rtl', () => {
464481
dir.value = 'rtl';
465482

466483
trigger.click();
467484
fixture.detectChanges();
468-
expect(fixture.componentInstance.select._getPanelState()).toEqual('showing-rtl');
485+
expect(fixture.componentInstance.select._getPanelState()).toEqual('top-rtl');
469486
});
470487

471488
});
472489

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

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

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)