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

Commit c4c7c33

Browse files
crisbetoThomasBurleson
authored andcommitted
fix(backdrop): re-introduce resize handler, general cleanup
* Re-introduces the resize handler that was accidentally removed in 4803b49. This fixes issues where the backdrop wouldn't adjust when the body resizes. * Cleans up a few redundancies. Closes #9249
1 parent e85a115 commit c4c7c33

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/components/backdrop/backdrop.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
angular
2121
.module('material.components.backdrop', ['material.core'])
2222
.directive('mdBackdrop', function BackdropDirective($mdTheming, $mdUtil, $animate, $rootElement, $window, $log, $$rAF, $document) {
23-
var ERROR_CSS_POSITION = "<md-backdrop> may not work properly in a scrolled, static-positioned parent container.";
23+
var ERROR_CSS_POSITION = '<md-backdrop> may not work properly in a scrolled, static-positioned parent container.';
2424

2525
return {
2626
restrict: 'E',
@@ -31,48 +31,53 @@ angular
3131
// backdrop may be outside the $rootElement, tell ngAnimate to animate regardless
3232
if ($animate.pin) $animate.pin(element, $rootElement);
3333

34-
var bodyRect;
35-
$$rAF(function () {
34+
var bodyStyles;
35+
36+
$$rAF(function() {
3637
// If body scrolling has been disabled using mdUtil.disableBodyScroll(),
3738
// adjust the 'backdrop' height to account for the fixed 'body' top offset.
3839
// Note that this can be pretty expensive and is better done inside the $$rAF.
39-
bodyRect = $window.getComputedStyle($document[0].body);
40-
if (bodyRect.position == 'fixed') {
41-
var hViewport = parseInt(bodyRect.height, 10) + Math.abs(parseInt(bodyRect.top, 10));
42-
element.css({
43-
height: hViewport + 'px'
40+
bodyStyles = $window.getComputedStyle($document[0].body);
41+
42+
if (bodyStyles.position === 'fixed') {
43+
var resizeHandler = $mdUtil.debounce(function(){
44+
bodyStyles = $window.getComputedStyle($document[0].body);
45+
resize();
46+
}, 60, null, false);
47+
48+
resize();
49+
angular.element($window).on('resize', resizeHandler);
50+
51+
scope.$on('$destroy', function() {
52+
angular.element($window).off('resize', resizeHandler);
4453
});
4554
}
4655

4756
// Often $animate.enter() is used to append the backDrop element
4857
// so let's wait until $animate is done...
49-
var parent = element.parent()[0];
50-
if (parent) {
58+
var parent = element.parent();
5159

52-
if ( parent.nodeName == 'BODY' ) {
53-
element.css({position : 'fixed'});
60+
if (parent.length) {
61+
if (parent[0].nodeName === 'BODY') {
62+
element.css('position', 'fixed');
5463
}
5564

56-
var styles = $window.getComputedStyle(parent);
57-
if (styles.position == 'static') {
65+
var styles = $window.getComputedStyle(parent[0]);
66+
67+
if (styles.position === 'static') {
5868
// backdrop uses position:absolute and will not work properly with parent position:static (default)
5969
$log.warn(ERROR_CSS_POSITION);
6070
}
61-
}
6271

63-
// Only inherit the parent if the backdrop has a parent.
64-
if (element.parent().length) {
65-
$mdTheming.inherit(element, element.parent());
72+
// Only inherit the parent if the backdrop has a parent.
73+
$mdTheming.inherit(element, parent);
6674
}
6775
});
6876

6977
function resize() {
70-
var hViewport = parseInt(bodyRect.height, 10) + Math.abs(parseInt(bodyRect.top, 10));
71-
element.css({
72-
height: hViewport + 'px'
73-
});
78+
var viewportHeight = parseInt(bodyStyles.height, 10) + Math.abs(parseInt(bodyStyles.top, 10));
79+
element.css('height', viewportHeight + 'px');
7480
}
75-
7681
}
7782

7883
});

0 commit comments

Comments
 (0)