primeng icon indicating copy to clipboard operation
primeng copied to clipboard

p-dialog mask all viewport, even appendTo to is set to an element

Open jone30rw opened this issue 7 years ago • 1 comments

I'm submitting a ... (check one with "x")

[x] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Current behavior In p-dialog with appendTo="someElement", the mask layer is still added to body.

Expected behavior The mask layer should also appended to the specified element, not to the body.

What is the motivation / use case for changing the behavior? Sometimes appendTo is used when the dialog is needed to be bound (with it being modal) to specific part of the viewport. In my example: I have multiple tabs, each used for working on different task. If one tab is blocked by a modal dialog, the user should be able to work on other tabs.

As shown in screenshots; in this tab the user cannot proceed until s/he save or close the dialog.

Screen Shot 2019-08-02 at 4 36 05 PM

In the mean time, s/he can navigate to another tab and work on something else.

Screen Shot 2019-08-02 at 4 36 23 PM

Proposed Solution

As the solution, I modified some lines in dialog.js file,

  • I changed following line in enableModality() function

      // ...
      	document.body.appendChild(this.mask); // <== this line
      	if(this.blockScroll) {
      		DomHandler.addClass(document.body, 'ui-overflow-hidden');
      	}
      // ....
    

    with

      // instead of always adding the mask to body add it to specified appendTo
      if (this.appendTo && this.appendTo !== 'body') {
      	// this will make the mask fit the parent unlike fixed
      	this.mask.style.position = "absolute";
      	domhandler_1.DomHandler.appendChild(this.mask, this.appendTo);
      } else {
      	document.body.appendChild(this.mask);
      }
    
  • and in onDrag() function

      if (this.dragging) {
      	let containerWidth = DomHandler.getOuterWidth(this.container);
      	let containerHeight = DomHandler.getOuterHeight(this.container);
      	let deltaX = event.pageX - this.lastPageX;
      	let deltaY = event.pageY - this.lastPageY;
      	let offset = DomHandler.getOffset(this.container);
      	let leftPos = offset.left + deltaX;
      	let topPos = offset.top + deltaY;
      	let viewport = DomHandler.getViewport();
    
      	if (leftPos >= this.minX && (leftPos + containerWidth) < viewport.width) {
      		this.container.style.left = leftPos + 'px';
      	}
      	if (topPos >= this.minY && (topPos + containerHeight) < viewport.height) {
      		this.container.style.top = topPos + 'px';
      	}
      	this.lastPageX = event.pageX;
      	this.lastPageY = event.pageY;
      }
    

    I brought following changes

      // === bound to appendTo if other than body
      var  bound  = { left:  this.minX, top:  this.minY, width:  viewport.width, height:  viewport.height };
    
      if (this.appendTo  &&  this.appendTo  !==  'body') {
      	var  appendToOffset  =  domhandler_1.DomHandler.getOffset(this.appendTo);
      	bound.left  =  appendToOffset.left;
      	bound.top  =  appendToOffset.top;
      	bound.width  =  bound.left  +  domhandler_1.DomHandler.getOuterWidth(this.appendTo);
      	bound.height  =  bound.top  +  domhandler_1.DomHandler.getOuterHeight(this.appendTo);
      }
    
      if (leftPos  >  bound.left  && (leftPos  +  containerWidth) <  bound.width) {
      	this.container.style.left  =  leftPos  +  'px';
      }
      if (topPos  >  bound.top  && (topPos  +  containerHeight) <  bound.height) {
      	this.container.style.top  =  topPos  +  'px';
      }
      // ===
    
  • In disableModality() function, I replaced the

      	document.body.removeChild(this.mask);
    

    line with this too

      if (this.appendTo  &&  this.appendTo  !==  'body') {
      	domhandler_1.DomHandler.removeChild(this.mask, this.appendTo);
      } else {
      	document.body.removeChild(this.mask);
      }
    

Angular version: 8.0.0 PrimeNG version: 8.0.2

jone30rw avatar Aug 02 '19 21:08 jone30rw

Unable to replicate with PrimeNG 9 which has a new Dialog implementation, if the issue persists please create a new ticket with a test case reproducing the issue e.g. stackblitz or a github repo and it will be reviewed by our team once again.

cagataycivici avatar Mar 29 '20 13:03 cagataycivici