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

Commit 993fa2b

Browse files
committed
feat(dialog): disable scrolling on parent while showing dialog
closes #987
1 parent f46487f commit 993fa2b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/components/dialog/dialog.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ function MdDialogDirective($$rAF, $mdTheming) {
203203
* - `targetEvent` - `{DOMClickEvent=}`: A click's event object. When passed in as an option,
204204
* the location of the click will be used as the starting point for the opening animation
205205
* of the the dialog.
206+
* - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the dialog is open.
207+
* Default true.
206208
* - `hasBackdrop` - `{boolean=}`: Whether there should be an opaque backdrop behind the dialog.
207209
* Default true.
208210
* - `clickOutsideToClose` - `{boolean=}`: Whether the user can click outside the dialog to
@@ -254,7 +256,7 @@ function MdDialogProvider($$interimElementProvider) {
254256

255257
return $$interimElementProvider('$mdDialog')
256258
.setDefaults({
257-
methods: ['hasBackdrop', 'clickOutsideToClose', 'escapeToClose', 'targetEvent'],
259+
methods: ['disableParentScroll', 'hasBackdrop', 'clickOutsideToClose', 'escapeToClose', 'targetEvent'],
258260
options: dialogDefaultOptions
259261
})
260262
.addPreset('alert', {
@@ -309,11 +311,13 @@ function MdDialogProvider($$interimElementProvider) {
309311
clickOutsideToClose: true,
310312
escapeToClose: true,
311313
targetEvent: null,
314+
disableParentScroll: true,
312315
transformTemplate: function(template) {
313316
return '<div class="md-dialog-container">' + template + '</div>';
314317
}
315318
};
316319

320+
317321
// On show method for dialogs
318322
function onShow(scope, element, options) {
319323
// Incase the user provides a raw dom element, always wrap it in jqLite
@@ -330,6 +334,11 @@ function MdDialogProvider($$interimElementProvider) {
330334
$animate.enter(options.backdrop, options.parent);
331335
}
332336

337+
if (options.disableParentScroll) {
338+
options.oldOverflowStyle = options.parent.css('overflow');
339+
options.parent.css('overflow', 'hidden');
340+
}
341+
333342
return dialogPopIn(
334343
element,
335344
options.parent,
@@ -377,6 +386,10 @@ function MdDialogProvider($$interimElementProvider) {
377386
if (options.backdrop) {
378387
$animate.leave(options.backdrop);
379388
}
389+
if (options.disableParentScroll) {
390+
options.parent.css('overflow', options.oldOverflowStyle);
391+
$document[0].removeEventListener('scroll', options.captureScroll, true);
392+
}
380393
if (options.escapeToClose) {
381394
$rootElement.off('keyup', options.rootElementKeyupCallback);
382395
}

src/components/dialog/dialog.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ describe('$mdDialog', function() {
237237
expect(parent[0].querySelectorAll('md-dialog').length).toBe(1);
238238
}));
239239

240+
it('should disableParentScroll == true', inject(function($mdDialog, $animate, $rootScope) {
241+
var parent = angular.element('<div>');
242+
$mdDialog.show({
243+
template: '<md-dialog>',
244+
parent: parent,
245+
disableParentScroll: true
246+
});
247+
$rootScope.$apply();
248+
$animate.triggerCallbacks();
249+
$rootScope.$apply();
250+
expect(parent.css('overflow')).toBe('hidden');
251+
}));
252+
240253
it('should hasBackdrop == true', inject(function($mdDialog, $animate, $rootScope) {
241254
var parent = angular.element('<div>');
242255
$mdDialog.show({

0 commit comments

Comments
 (0)