Skip to content

Commit 3393ba2

Browse files
committed
fix(select): prevent the panel from going outside the viewport horizontally
Prevents the select panel from going outside the viewport along the x axis. Fixes angular#3504. Fixes angular#3831.
1 parent e7a4a03 commit 3393ba2

File tree

3 files changed

+153
-33
lines changed

3 files changed

+153
-33
lines changed

src/lib/select/select.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<ng-template cdk-connected-overlay [origin]="origin" [open]="panelOpen" hasBackdrop (backdropClick)="close()"
1717
backdropClass="cdk-overlay-transparent-backdrop" [positions]="_positions" [minWidth]="_triggerWidth"
18-
[offsetY]="_offsetY" [offsetX]="_offsetX" (attach)="_setScrollTop()">
18+
[offsetY]="_offsetY" (attach)="_onAttached()">
1919
<div class="mat-select-panel" [@transformPanel]="'showing'" (@transformPanel.done)="_onPanelDone()"
2020
(keydown)="_keyManager.onKeydown($event)" [style.transformOrigin]="_transformOrigin"
2121
[class.mat-select-panel-done-animating]="_panelDoneAnimating">

src/lib/select/select.spec.ts

+104-16
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,22 @@ import {dispatchFakeEvent} from '../core/testing/dispatch-events';
2424
import {wrappedErrorMessage} from '../core/testing/wrapped-error-message';
2525

2626

27+
class FakeViewportRuler {
28+
getViewportRect() {
29+
return {
30+
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
31+
};
32+
}
33+
34+
getViewportScrollPosition() {
35+
return {top: 0, left: 0};
36+
}
37+
}
38+
2739
describe('MdSelect', () => {
2840
let overlayContainerElement: HTMLElement;
2941
let dir: {value: 'ltr'|'rtl'};
42+
let fakeViewportRuler = new FakeViewportRuler();
3043

3144
beforeEach(async(() => {
3245
TestBed.configureTestingModule({
@@ -65,7 +78,7 @@ describe('MdSelect', () => {
6578
{provide: Dir, useFactory: () => {
6679
return dir = { value: 'ltr' };
6780
}},
68-
{provide: ViewportRuler, useClass: FakeViewportRuler}
81+
{provide: ViewportRuler, useValue: fakeViewportRuler}
6982
]
7083
});
7184

@@ -882,6 +895,94 @@ describe('MdSelect', () => {
882895

883896
});
884897

898+
describe('limited space to open horizontally', () => {
899+
beforeEach(async(() => {
900+
select.style.position = 'absolute';
901+
select.style.top = '200px';
902+
}));
903+
904+
it('should stay within the viewport when overflowing on the left in ltr', async(() => {
905+
select.style.left = '-100px';
906+
trigger.click();
907+
fixture.detectChanges();
908+
909+
fixture.whenStable().then(() => {
910+
const panelLeft = document.querySelector('.mat-select-panel')
911+
.getBoundingClientRect().left;
912+
expect(panelLeft).toBeGreaterThan(0,
913+
`Expected select panel to be inside the viewport in ltr.`);
914+
});
915+
}));
916+
917+
it('should stay within the viewport when overflowing on the right in rtl', async(() => {
918+
dir.value = 'rtl';
919+
select.style.left = '-100px';
920+
trigger.click();
921+
fixture.detectChanges();
922+
923+
fixture.whenStable().then(() => {
924+
const panelLeft = document.querySelector('.mat-select-panel')
925+
.getBoundingClientRect().left;
926+
927+
expect(panelLeft).toBeGreaterThan(0,
928+
`Expected select panel to be inside the viewport in rtl.`);
929+
});
930+
}));
931+
932+
it('should stay within the viewport when overflowing on the right in ltr', async(() => {
933+
select.style.right = '-100px';
934+
trigger.click();
935+
fixture.detectChanges();
936+
937+
fixture.whenStable().then(() => {
938+
const viewportRect = fakeViewportRuler.getViewportRect().right;
939+
const panelRight = document.querySelector('.mat-select-panel')
940+
.getBoundingClientRect().right;
941+
942+
expect(viewportRect - panelRight).toBeGreaterThan(0,
943+
`Expected select panel to be inside the viewport in ltr.`);
944+
});
945+
}));
946+
947+
it('should stay within the viewport when overflowing on the right in rtl', async(() => {
948+
dir.value = 'rtl';
949+
select.style.right = '-100px';
950+
trigger.click();
951+
fixture.detectChanges();
952+
953+
fixture.whenStable().then(() => {
954+
const viewportRect = fakeViewportRuler.getViewportRect().right;
955+
const panelRight = document.querySelector('.mat-select-panel')
956+
.getBoundingClientRect().right;
957+
958+
expect(viewportRect - panelRight).toBeGreaterThan(0,
959+
`Expected select panel to be inside the viewport in rtl.`);
960+
});
961+
}));
962+
963+
it('should keep the position within the viewport on repeat openings', async(() => {
964+
select.style.left = '-100px';
965+
trigger.click();
966+
fixture.detectChanges();
967+
968+
let panelLeft = document.querySelector('.mat-select-panel').getBoundingClientRect().left;
969+
970+
expect(panelLeft).toBeGreaterThan(0, `Expected select panel to be inside the viewport.`);
971+
972+
fixture.componentInstance.select.close();
973+
fixture.detectChanges();
974+
975+
fixture.whenStable().then(() => {
976+
trigger.click();
977+
fixture.detectChanges();
978+
panelLeft = document.querySelector('.mat-select-panel').getBoundingClientRect().left;
979+
980+
expect(panelLeft).toBeGreaterThan(0,
981+
`Expected select panel continue being inside the viewport.`);
982+
});
983+
}));
984+
});
985+
885986
describe('when scrolled', () => {
886987

887988
// Need to set the scrollTop two different ways to support
@@ -1030,8 +1131,8 @@ describe('MdSelect', () => {
10301131
trigger = multiFixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
10311132
select = multiFixture.debugElement.query(By.css('md-select')).nativeElement;
10321133

1033-
select.style.marginLeft = '20px';
1034-
select.style.marginRight = '20px';
1134+
select.style.marginLeft = '60px';
1135+
select.style.marginRight = '60px';
10351136
});
10361137

10371138
it('should adjust for the checkbox in ltr', () => {
@@ -1956,16 +2057,3 @@ class SelectWithPlainTabindex { }
19562057
`
19572058
})
19582059
class SelectEarlyAccessSibling { }
1959-
1960-
1961-
class FakeViewportRuler {
1962-
getViewportRect() {
1963-
return {
1964-
left: 0, top: 0, width: 1014, height: 686, bottom: 686, right: 1014
1965-
};
1966-
}
1967-
1968-
getViewportScrollPosition() {
1969-
return {top: 0, left: 0};
1970-
}
1971-
}

src/lib/select/select.ts

+48-16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ export const SELECT_PANEL_PADDING_Y = 16;
8585
*/
8686
export const SELECT_PANEL_VIEWPORT_PADDING = 8;
8787

88+
/** Extra width that gets added the to select panel during the open animation. */
89+
export const SELECT_PANEL_EXTRA_WIDTH = 32;
90+
8891
/** Change event object that is emitted when the select value has changed. */
8992
export class MdSelectChange {
9093
constructor(public source: MdSelect, public value: any) { }
@@ -187,13 +190,6 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
187190
/** Whether the panel's animation is done. */
188191
_panelDoneAnimating: boolean = false;
189192

190-
/**
191-
* The x-offset of the overlay panel in relation to the trigger's top start corner.
192-
* This must be adjusted to align the selected option text over the trigger text when
193-
* the panel opens. Will change based on LTR or RTL text direction.
194-
*/
195-
_offsetX = 0;
196-
197193
/**
198194
* The y-offset of the overlay panel in relation to the trigger's top start corner.
199195
* This must be adjusted to align the selected option text over the trigger text.
@@ -468,6 +464,7 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
468464
} else {
469465
this.onClose.emit();
470466
this._panelDoneAnimating = false;
467+
this.overlayDir.offsetX = 0;
471468
}
472469
}
473470

@@ -489,12 +486,20 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
489486
}
490487
}
491488

489+
/**
490+
* Callback that is invoked when the overlay panel has been attached.
491+
*/
492+
_onAttached(): void {
493+
this._calculateOverlayXOffset();
494+
this._setScrollTop();
495+
}
496+
492497
/**
493498
* Sets the scroll position of the scroll container. This must be called after
494499
* the overlay pane is attached or the scroll container element will not yet be
495500
* present in the DOM.
496501
*/
497-
_setScrollTop(): void {
502+
private _setScrollTop(): void {
498503
const scrollContainer =
499504
this.overlayDir.overlayRef.overlayElement.querySelector('.mat-select-panel');
500505
scrollContainer.scrollTop = this._scrollTop;
@@ -693,12 +698,6 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
693698

694699
/** Calculates the scroll position and x- and y-offsets of the overlay panel. */
695700
private _calculateOverlayPosition(): void {
696-
this._offsetX = this.multiple ? SELECT_MULTIPLE_PANEL_PADDING_X : SELECT_PANEL_PADDING_X;
697-
698-
if (!this._isRtl()) {
699-
this._offsetX *= -1;
700-
}
701-
702701
const panelHeight =
703702
Math.min(this.options.length * SELECT_OPTION_HEIGHT, SELECT_PANEL_MAX_HEIGHT);
704703
const scrollContainerHeight = this.options.length * SELECT_OPTION_HEIGHT;
@@ -712,7 +711,7 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
712711
// center of the overlay panel rather than the top.
713712
const scrollBuffer = panelHeight / 2;
714713
this._scrollTop = this._calculateOverlayScroll(selectedIndex, scrollBuffer, maxScroll);
715-
this._offsetY = this._calculateOverlayOffset(selectedIndex, scrollBuffer, maxScroll);
714+
this._offsetY = this._calculateOverlayYOffset(selectedIndex, scrollBuffer, maxScroll);
716715
} else {
717716
// If no option is selected, the panel centers on the first option. In this case,
718717
// we must only adjust for the height difference between the option element
@@ -774,12 +773,45 @@ export class MdSelect implements AfterContentInit, OnDestroy, OnInit, ControlVal
774773
return this.ariaLabelledby ? null : this.ariaLabel || this.placeholder;
775774
}
776775

776+
/**
777+
* Sets the x-offset of the overlay panel in relation to the trigger's top start corner.
778+
* This must be adjusted to align the selected option text over the trigger text when
779+
* the panel opens. Will change based on LTR or RTL text direction. Note that the offset
780+
* can't be calculated until the panel has been attached, because we need to know the
781+
* content width in order to constrain the panel within the viewport.
782+
*/
783+
private _calculateOverlayXOffset(): void {
784+
let overlayRect = this.overlayDir.overlayRef.overlayElement.getBoundingClientRect();
785+
let viewportRect = this._viewportRuler.getViewportRect();
786+
let offsetX = this.multiple ? SELECT_MULTIPLE_PANEL_PADDING_X : SELECT_PANEL_PADDING_X;
787+
let isRtl = this._isRtl();
788+
789+
if (!isRtl) {
790+
offsetX *= -1;
791+
}
792+
793+
let leftOverflow = 0 - (overlayRect.left + offsetX - (isRtl ? SELECT_PANEL_EXTRA_WIDTH : 0));
794+
let rightOverflow = overlayRect.right + offsetX - viewportRect.width
795+
+ (isRtl ? 0 : SELECT_PANEL_EXTRA_WIDTH);
796+
797+
if (leftOverflow > 0) {
798+
offsetX += leftOverflow + SELECT_PANEL_VIEWPORT_PADDING;
799+
} else if (rightOverflow > 0) {
800+
offsetX -= rightOverflow + SELECT_PANEL_VIEWPORT_PADDING;
801+
}
802+
803+
// Set the offset directly in order to avoid having to go through change detection and
804+
// potentially triggering "changed after it was checked" errors.
805+
this.overlayDir.offsetX = offsetX;
806+
this.overlayDir.overlayRef.updatePosition();
807+
}
808+
777809
/**
778810
* Calculates the y-offset of the select's overlay panel in relation to the
779811
* top start corner of the trigger. It has to be adjusted in order for the
780812
* selected option to be aligned over the trigger when the panel opens.
781813
*/
782-
private _calculateOverlayOffset(selectedIndex: number, scrollBuffer: number,
814+
private _calculateOverlayYOffset(selectedIndex: number, scrollBuffer: number,
783815
maxScroll: number): number {
784816
let optionOffsetFromPanelTop: number;
785817

0 commit comments

Comments
 (0)