Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 47e4c1b

Browse files
crisbetokara
authored andcommitted
fix(panel): panel not being constrained to viewport on repeat openings (#9944)
Fixes #9942.
1 parent c851204 commit 47e4c1b

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/components/panel/panel.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ MdPanelPosition.prototype._reduceTranslateValues =
27132713
* @private
27142714
*/
27152715
MdPanelPosition.prototype._setPanelPosition = function(panelEl) {
2716-
// Remove the class in case it has been added before.
2716+
// Remove the "position adjusted" class in case it has been added before.
27172717
panelEl.removeClass('_md-panel-position-adjusted');
27182718

27192719
// Only calculate the position if necessary.
@@ -2725,6 +2725,7 @@ MdPanelPosition.prototype._setPanelPosition = function(panelEl) {
27252725
if (this._actualPosition) {
27262726
this._calculatePanelPosition(panelEl, this._actualPosition);
27272727
this._setTransform(panelEl);
2728+
this._constrainToViewport(panelEl);
27282729
return;
27292730
}
27302731

@@ -2738,8 +2739,6 @@ MdPanelPosition.prototype._setPanelPosition = function(panelEl) {
27382739
}
27392740
}
27402741

2741-
// Class that can be used to re-style the panel if it was repositioned.
2742-
panelEl.addClass('_md-panel-position-adjusted');
27432742
this._constrainToViewport(panelEl);
27442743
};
27452744

@@ -2751,6 +2750,8 @@ MdPanelPosition.prototype._setPanelPosition = function(panelEl) {
27512750
*/
27522751
MdPanelPosition.prototype._constrainToViewport = function(panelEl) {
27532752
var margin = MdPanelPosition.viewportMargin;
2753+
var initialTop = this._top;
2754+
var initialLeft = this._left;
27542755

27552756
if (this.getTop()) {
27562757
var top = parseInt(this.getTop());
@@ -2775,6 +2776,12 @@ MdPanelPosition.prototype._constrainToViewport = function(panelEl) {
27752776
this._left = left - (right - viewportWidth + margin) + 'px';
27762777
}
27772778
}
2779+
2780+
// Class that can be used to re-style the panel if it was repositioned.
2781+
panelEl.toggleClass(
2782+
'_md-panel-position-adjusted',
2783+
this._top !== initialTop || this._left !== initialLeft
2784+
);
27782785
};
27792786

27802787

src/components/panel/panel.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,39 @@ describe('$mdPanel', function() {
23642364
});
23652365
});
23662366

2367+
it('should keep the panel within the viewport on repeat openings',
2368+
function() {
2369+
2370+
config.position = mdPanelPosition
2371+
.relativeTo(myButton)
2372+
.addPanelPosition(xPosition.ALIGN_END, yPosition.ALIGN_TOPS);
2373+
2374+
var panelRef = $mdPanel.create(config);
2375+
2376+
myButton.css({
2377+
position: 'absolute',
2378+
left: '-100px',
2379+
margin: 0
2380+
});
2381+
2382+
panelRef.open();
2383+
flushPanel();
2384+
2385+
expect(panelRef.panelEl[0].offsetLeft).toBe(VIEWPORT_MARGIN);
2386+
expect(panelRef.panelEl[0]).toHaveClass(ADJUSTED_CLASS);
2387+
2388+
panelRef.close();
2389+
flushPanel();
2390+
2391+
panelRef.open();
2392+
flushPanel();
2393+
2394+
expect(panelRef.panelEl[0].offsetLeft).toBe(VIEWPORT_MARGIN);
2395+
expect(panelRef.panelEl[0]).toHaveClass(ADJUSTED_CLASS);
2396+
2397+
panelRef.destroy();
2398+
});
2399+
23672400
describe('vertically', function() {
23682401
it('above an element', function() {
23692402
var position = mdPanelPosition

0 commit comments

Comments
 (0)