Skip to content

Commit

Permalink
Fix 17016 Close dynamic dialog on escape also when autoZindex is set …
Browse files Browse the repository at this point in the history
…to false (#17508)

* fix(#17016): Also close on escape when autoZIndex is set to false

* docs(#17016): Be more clear on applying zIndex
  • Loading branch information
shaman-apprentice authored Jan 29, 2025
1 parent 483cce5 commit 54b16c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/primeng/src/dynamicdialog/dynamicdialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DynamicDialogConfig<DataType = any, InputValuesType extends Record<
*/
baseZIndex?: number;
/**
* Whether to automatically manage layering.
* Whether to re-enforce layering through applying zIndex.
* @group Props
*/
autoZIndex?: boolean = false;
Expand Down
12 changes: 10 additions & 2 deletions packages/primeng/src/dynamicdialog/dynamicdialog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { animate, animation, AnimationEvent, style, transition, trigger, useAnimation } from '@angular/animations';
import { CommonModule, isPlatformBrowser } from '@angular/common';
import { AfterViewInit, ChangeDetectionStrategy, Component, ComponentRef, ElementRef, inject, NgModule, NgZone, OnDestroy, Optional, Renderer2, SkipSelf, TemplateRef, Type, ViewChild, ViewEncapsulation, ViewRef } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, ComponentRef, ElementRef, inject, NgModule, NgZone, OnDestroy, Optional, Renderer2, SkipSelf, Type, ViewChild, ViewEncapsulation, ViewRef } from '@angular/core';
import { addClass, getOuterHeight, getOuterWidth, getViewport, hasClass, removeClass, setAttribute, uuid } from '@primeuix/utils';
import { SharedModule, TranslationKeys } from 'primeng/api';
import { BaseComponent } from 'primeng/basecomponent';
Expand Down Expand Up @@ -288,6 +288,8 @@ export class DynamicDialogComponent extends BaseComponent implements AfterViewIn
return this.attrSelector;
}

private zIndexForLayering?: number;

constructor(
public renderer: Renderer2,
public ddconfig: DynamicDialogConfig,
Expand Down Expand Up @@ -364,6 +366,8 @@ export class DynamicDialogComponent extends BaseComponent implements AfterViewIn
if (this.ddconfig.autoZIndex !== false) {
ZIndexUtils.set('modal', this.container, (this.ddconfig.baseZIndex || 0) + this.config.zIndex.modal);
(this.wrapper as HTMLElement).style.zIndex = String(parseInt((this.container as HTMLDivElement).style.zIndex, 10) - 1);
} else {
this.zIndexForLayering = ZIndexUtils.generateZIndex('modal', (this.ddconfig.baseZIndex || 0) + this.config.zIndex.modal);
}
}

Expand Down Expand Up @@ -412,6 +416,9 @@ export class DynamicDialogComponent extends BaseComponent implements AfterViewIn
if (this.container && this.ddconfig.autoZIndex !== false) {
ZIndexUtils.clear(this.container);
}
if (this.zIndexForLayering) {
ZIndexUtils.revertZIndex(this.zIndexForLayering);
}

if (this.ddconfig.modal !== false) {
this.disableModality();
Expand Down Expand Up @@ -695,7 +702,8 @@ export class DynamicDialogComponent extends BaseComponent implements AfterViewIn

this.documentEscapeListener = this.renderer.listen(documentTarget, 'keydown', (event) => {
if (event.which == 27) {
if (parseInt((this.container as HTMLDivElement).style.zIndex) == ZIndexUtils.getCurrent()) {
const currentZIndex = ZIndexUtils.getCurrent();
if (parseInt((this.container as HTMLDivElement).style.zIndex) == currentZIndex || this.zIndexForLayering == currentZIndex) {
this.hide();
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/primeng/src/utils/zindexutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function ZIndexUtils() {
el.style.zIndex = '';
}
},
getCurrent: () => getCurrentZIndex()
getCurrent: () => getCurrentZIndex(),
generateZIndex,
revertZIndex
};
}

Expand Down

0 comments on commit 54b16c2

Please sign in to comment.