Skip to content

Commit

Permalink
Merge pull request #4621 from qburst/master
Browse files Browse the repository at this point in the history
fix #4620 Overlaypanel: Overlay panel doesnot close on escape button click
  • Loading branch information
tugcekucukoglu authored Oct 17, 2023
2 parents 3feaf72 + 02cbc1e commit 366f136
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/lib/overlaypanel/BaseOverlayPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default {
closeIcon: {
type: String,
default: undefined
},
closeOnEscape: {
type: Boolean,
default: true
}
},
style: OverlayPanelStyle,
Expand Down
5 changes: 5 additions & 0 deletions components/lib/overlaypanel/OverlayPanel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ export interface OverlayPanelProps {
* @defaultValue false
*/
unstyled?: boolean;
/**
* Specifies if pressing escape key should hide the dialog.
* @defaultValue true
*/
closeOnEscape?: boolean | undefined;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions components/lib/overlaypanel/OverlayPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default {
container: null,
styleElement: null,
overlayEventListener: null,
documentKeydownListener: null,
beforeUnmount() {
if (this.dismissable) {
this.unbindOutsideClickListener();
Expand Down Expand Up @@ -129,6 +130,10 @@ export default {
this.focus();
OverlayEventBus.on('overlay-click', this.overlayEventListener);
this.$emit('show');
if (this.closeOnEscape) {
this.bindDocumentKeyDownListener();
}
},
onLeave() {
this.unbindOutsideClickListener();
Expand Down Expand Up @@ -186,6 +191,23 @@ export default {
focusTarget.focus();
}
},
onKeyDown(event) {
if (event.code === 'Escape' && this.closeOnEscape) {
this.visible = false;
}
},
bindDocumentKeyDownListener() {
if (!this.documentKeydownListener) {
this.documentKeydownListener = this.onKeyDown.bind(this);
window.document.addEventListener('keydown', this.documentKeydownListener);
}
},
unbindDocumentKeyDownListener() {
if (this.documentKeydownListener) {
window.document.removeEventListener('keydown', this.documentKeydownListener);
this.documentKeydownListener = null;
}
},
bindOutsideClickListener() {
if (!this.outsideClickListener && DomHandler.isClient()) {
this.outsideClickListener = (event) => {
Expand Down

0 comments on commit 366f136

Please sign in to comment.