material icon indicating copy to clipboard operation
material copied to clipboard

dialog: cuts off/hides body when window is scrolled

Open rgthree opened this issue 11 years ago • 43 comments

Fiddle: http://jsfiddle.net/rgthree/9m7z8cqy/

If body has any overflow applied by the app, angular overrides its style to hidden when the dialog is open. This clashes with the other styles added to body, particularly, position: fixed; top: -XXpx; and max-height: 100% from the md-dialog-is-showing class which ends up cutting off the body content or completely hiding it when the window has scrolled more than 100% the height.

Note, we use body {overflow-x:scroll;} to force a scrollbar and reduce horizontal jitter when page content height changes due to navigation.

rgthree avatar Aug 12 '15 15:08 rgthree

@rschmukler

jelbourn avatar Aug 12 '15 18:08 jelbourn

+1

leonn avatar Sep 10 '15 13:09 leonn

+1

ngasst avatar Oct 22 '15 07:10 ngasst

@rgthree can you please check if it's still happening on HEAD?

EladBezalel avatar Oct 24 '15 12:10 EladBezalel

@EladBezalel I don't know about @rgthree, but I just checked against HEAD and the issue is still very much there for me. Happy to provide as many details as I can/you require.

ngasst avatar Oct 26 '15 10:10 ngasst

Can you please post a codepen(not jsfiddle)?

EladBezalel avatar Oct 26 '15 16:10 EladBezalel

Couldn't reproduce this with a codepen (including it anyway, in case it might prove useful). I'll keep trying to reproduce the issue, but in the meantime, here are a couple screenshots of the problem taken from the app I'm working on. The pictures include the position of the scrollbar when it works (only at the very top) and when it doesn't (anywhere when scrolled down).

This could be an isolated issue. Maybe someone else can shout out if they've experienced the same problem so you don't waste time tracking something that might be due to my individual config?

CP: http://codepen.io/ngasst/pen/XmEoaK ngmatissue

ngasst avatar Oct 26 '15 21:10 ngasst

@ngasst all you had to do is adding disableParentScroll: false to reproduce it http://codepen.io/EladBezalel/pen/OyvdBN

the question is, why you wouldn't like to disable parent scroll

EladBezalel avatar Oct 26 '15 22:10 EladBezalel

Very strange. The behavior is indeed reproduced by setting the disableParentScroll to false in the codenpen. But I didn't set it in my actual code, and my understanding is it defaults to true when not set. Here's the options definition from my ModalService. It's in TypeScript, but essentially not much changes:

export class ModalService implements IModalService {
        static $inject: Array<string> = ['$mdDialog'];
        options: ng.material.MDDialogOptions;
        constructor(
            private $mdDialog: ng.material.MDDialogService
        ) {
            //
        }

        open(template: string, controller:string, event?: MouseEvent, data?:any, animcb?:any, ctlAs?:string) {
            this.options = {
                templateUrl: template,
                targetEvent: event,
                controller: controller,
                locals: {
                    data: data  
                },
                bindToController: true,
                controllerAs: ctlAs ? ctlAs : 'vm',
                onComplete: animcb
            }; 

            this.$mdDialog.show(this.options)
                .then(() => {}, () => {});
        }

ngasst avatar Oct 27 '15 08:10 ngasst

A little bit more information about this: For whatever reason, when the dialog is triggered after a scroll, the body receives a top positioning equivalent in pixels to the scrolled distance (if that makes sense -- i.e.: I scroll 889 pixels down, the body receives {[...]; top: -889px}). Now, I see that this comes from the scrollTop function on the $mdUtil service. I have enough to manage a dirty fix, but not the right contextual experience with the implementation to offer a permanent solution. And I still don't know why I seem to be the only one affected by this. Will dig some more and report back if I have something worth sharing.

ngasst avatar Oct 29 '15 09:10 ngasst

Hi,

I have the same problem. After a scroll, the top property of body is set to a negative value when the modal is shown, thus hiding some content of the body. @ngasst can you share your temp workaround?

bampakoa avatar Nov 02 '15 10:11 bampakoa

@bampakoa,

Please find a sample of the code below. Maybe it'll be useful in finding a more universal fix? I'll walk through what the code does because, like I said above, it's TypeScript, and I know that not everyone uses that, so...

My fix implies having a wrapper service that takes responsibility for opening all modals in your app. If that service has an open function, you use it to get the value of the scroll position and store it (you actually need to store it, because it'll change as soon as the modal is invoked). To get that value, you can use the useful but undocumented $mdUtil service. Then you select the body and set its style to {position:fixed; top: 0; height:100%; width:100%;}. I use D3 because I'm already using it for other stuff in the app and it's already loaded, but you can use JQuery or even plain JS for this.

That corrects the issue and lets the modal be positioned correctly. But you need to cleanup once you close the modal. That's where the onRemoving event comes in. You give it a callback and use whatever method you want to set the body style to {height:100%;} and just to be fancy, you can get the scroll position, which you stored earlier, and use $window.scrollBy() to go back to whereverfrom you triggered the modal, otherwise it'll default to the top of your app -- which may or may not be what you're going for.

Here's the code.

export class ModalService implements IModalService {
        static $inject: Array<string> = ['$mdDialog', '$mdUtil', '$document', '$window'];
        options: ng.material.MDDialogOptions;
        scrollAmount: number;
        constructor(
            private $mdDialog: ng.material.MDDialogService,
            private $mdUtil: any,
            private $document: ng.IDocumentService,
            private $window: ng.IWindowService
        ) {
            //
        }

        open(template: string, controller:string, event?: MouseEvent, data?:any, animcb?:any, ctlAs?:string) {
            this.scrollAmount = this.$mdUtil.scrollTop(this.$document[0].body);
            d3.select('body').style({
                position: 'fixed',
                height: '100%',
                width: '100%',
                top: '0'
            });

            this.options = {
                templateUrl: template,
                targetEvent: event,
                controller: controller,
                locals: {
                    data: data  
                },
                bindToController: true,
                controllerAs: ctlAs ? ctlAs : 'vm',
                onComplete: animcb,
                onRemoving: () => {
                    d3.select('body').attr({style: 'height: 100%;'});
                    console.log(this.scrollAmount);
                    this.$window.scrollBy(0, this.scrollAmount);
                }   
            }; 


            this.$mdDialog.show(this.options)
                .then((scAmount) => {}, () => {});
        }


        close() {
            this.$mdDialog.hide();
        }
    }

ngasst avatar Nov 03 '15 09:11 ngasst

@ngasst thanks for your tip!

bampakoa avatar Nov 13 '15 15:11 bampakoa

I don't see it much of an issue, disableParentScroll default value is true and the developer should be aware of disabling this.

@robertmesserle @topherfangio can you guys say your thoughts about it?

EladBezalel avatar Nov 24 '15 12:11 EladBezalel

Just to share a workaround on this, through css:

.md-dialog-is-showing {
    max-height: none;
}

I do not know if it can be applied globally, but it seems to work in my occasion.

bampakoa avatar Nov 25 '15 14:11 bampakoa

I am experiencing this and looking at the demo, it is exactly what I get. When opening a dialog after scrolling the view a white bar shows up at the bottom of the view. The size of the bar is equivalent to how much the view was scrolled. This is especially troublesome because, just like the demo, I do not have disableParentScroll set to false. However, setting the overflow to initial for the app body did indeed fix this issue.

abouillon avatar Mar 07 '16 15:03 abouillon

@rgthree where you able to solve the issue.

akashjarad avatar Mar 10 '16 10:03 akashjarad

Also having this problem within my entire project

zilions avatar Mar 13 '16 11:03 zilions

Also having this issue, able to solve it by overriding css positioning which seems like a hack . Will be good to get a permanent fix. I am using v1.0.6. Thanks.

animesh1987 avatar Mar 16 '16 13:03 animesh1987

@animesh1987 - Are you able to provide some code that solves this issue?

zilions avatar Mar 16 '16 22:03 zilions