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

Commit 9f6eecb

Browse files
devversionThomasBurleson
authored andcommitted
fix(sidenav): update position of sidenav and backdrop if scrolled.
Currently the sidenav is not animating when the parent is scrolled, this was caused by the absolute position. This commit, updates the position of the sidenav and backdrop when the parent is scrolled. It sets the position to the current scroll position and sets the height to the parents `clientHeight`. Using a `100%` height, will impact the performance heavily. Fixes #5850. Closes #8184
1 parent 6aa9e08 commit 9f6eecb

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

src/components/sidenav/sidenav.js

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $animate,
256256
var lastParentOverFlow;
257257
var backdrop;
258258
var triggeringElement = null;
259+
var previousContainerStyles;
259260
var promise = $q.when(true);
260261
var isLockedOpenParsed = $parse(attr.mdIsLockedOpen);
261262
var isLocked = function() {
@@ -324,6 +325,8 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $animate,
324325
parent[isOpen ? 'on' : 'off']('keydown', onKeyDown);
325326
if (backdrop) backdrop[isOpen ? 'on' : 'off']('click', close);
326327

328+
var restorePositioning = updateContainerPositions(parent, isOpen);
329+
327330
if ( isOpen ) {
328331
// Capture upon opening..
329332
triggeringElement = $document[0].activeElement;
@@ -332,16 +335,60 @@ function SidenavDirective($mdMedia, $mdUtil, $mdConstant, $mdTheming, $animate,
332335
disableParentScroll(isOpen);
333336

334337
return promise = $q.all([
335-
isOpen && backdrop ? $animate.enter(backdrop, parent) :
336-
backdrop ? $animate.leave(backdrop) : $q.when(true),
337-
$animate[isOpen ? 'removeClass' : 'addClass'](element, '_md-closed')
338-
])
339-
.then(function() {
340-
// Perform focus when animations are ALL done...
341-
if (scope.isOpen) {
342-
focusEl && focusEl.focus();
343-
}
344-
});
338+
isOpen && backdrop ? $animate.enter(backdrop, parent) : backdrop ?
339+
$animate.leave(backdrop) : $q.when(true),
340+
$animate[isOpen ? 'removeClass' : 'addClass'](element, '_md-closed')
341+
]).then(function() {
342+
// Perform focus when animations are ALL done...
343+
if (scope.isOpen) {
344+
focusEl && focusEl.focus();
345+
}
346+
347+
// Restores the positioning on the sidenav and backdrop.
348+
restorePositioning && restorePositioning();
349+
});
350+
}
351+
352+
function updateContainerPositions(parent, willOpen) {
353+
var drawerEl = element[0];
354+
var scrollTop = parent[0].scrollTop;
355+
356+
if (willOpen && scrollTop) {
357+
previousContainerStyles = {
358+
top: drawerEl.style.top,
359+
bottom: drawerEl.style.bottom,
360+
height: drawerEl.style.height
361+
};
362+
363+
// When the parent is scrolled down, then we want to be able to show the sidenav at the current scroll
364+
// position. We're moving the sidenav down to the correct scroll position and apply the height of the
365+
// parent, to increase the performance. Using 100% as height, will impact the performance heavily.
366+
var positionStyle = {
367+
top: scrollTop + 'px',
368+
bottom: 'initial',
369+
height: parent[0].clientHeight + 'px'
370+
};
371+
372+
// Apply the new position styles to the sidenav and backdrop.
373+
element.css(positionStyle);
374+
backdrop.css(positionStyle);
375+
}
376+
377+
// When the sidenav is closing and we have previous defined container styles,
378+
// then we return a restore function, which resets the sidenav and backdrop.
379+
if (!willOpen && previousContainerStyles) {
380+
return function() {
381+
drawerEl.style.top = previousContainerStyles.top;
382+
drawerEl.style.bottom = previousContainerStyles.bottom;
383+
drawerEl.style.height = previousContainerStyles.height;
384+
385+
backdrop[0].style.top = null;
386+
backdrop[0].style.bottom = null;
387+
backdrop[0].style.height = null;
388+
389+
previousContainerStyles = null;
390+
}
391+
}
345392
}
346393

347394
/**

0 commit comments

Comments
 (0)