2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<Portal :appendTo="appendTo">
|
2023-08-02 12:01:58 +00:00
|
|
|
<transition name="p-overlaypanel" @enter="onEnter" @leave="onLeave" @after-leave="onAfterLeave" v-bind="ptm('transition')">
|
2023-05-29 20:18:28 +00:00
|
|
|
<div v-if="visible" :ref="containerRef" v-focustrap role="dialog" :aria-modal="visible" @click="onOverlayClick" :class="cx('root')" v-bind="{ ...$attrs, ...ptm('root') }">
|
2023-09-19 10:23:50 +00:00
|
|
|
<slot v-if="$slots.container" name="container" :onClose="hide" :onKeydown="(event) => onButtonKeydown(event)"></slot>
|
|
|
|
<template v-else>
|
|
|
|
<div :class="cx('content')" @click="onContentClick" @mousedown="onContentClick" @keydown="onContentKeydown" v-bind="ptm('content')">
|
|
|
|
<slot></slot>
|
|
|
|
</div>
|
|
|
|
<button v-if="showCloseIcon" v-ripple :class="cx('closeButton')" :aria-label="closeAriaLabel" type="button" autofocus @click="hide" @keydown="onButtonKeydown" v-bind="ptm('closeButton')">
|
|
|
|
<slot name="closeicon">
|
|
|
|
<component :is="closeIcon ? 'span' : 'TimesIcon'" :class="[cx('closeIcon'), closeIcon]" v-bind="ptm('closeIcon')"></component>
|
|
|
|
</slot>
|
|
|
|
</button>
|
|
|
|
</template>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</Portal>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-12-08 11:04:25 +00:00
|
|
|
import FocusTrap from 'primevue/focustrap';
|
2023-04-18 12:53:43 +00:00
|
|
|
import TimesIcon from 'primevue/icons/times';
|
2022-09-06 12:03:37 +00:00
|
|
|
import OverlayEventBus from 'primevue/overlayeventbus';
|
|
|
|
import Portal from 'primevue/portal';
|
2022-12-08 11:04:25 +00:00
|
|
|
import Ripple from 'primevue/ripple';
|
|
|
|
import { ConnectedOverlayScrollHandler, DomHandler, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
|
2023-06-05 11:24:13 +00:00
|
|
|
import BaseOverlayPanel from './BaseOverlayPanel.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'OverlayPanel',
|
2023-05-29 20:18:28 +00:00
|
|
|
extends: BaseOverlayPanel,
|
2022-09-06 12:03:37 +00:00
|
|
|
inheritAttrs: false,
|
|
|
|
emits: ['show', 'hide'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
visible: false
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
dismissable: {
|
|
|
|
immediate: true,
|
|
|
|
handler(newValue) {
|
|
|
|
if (newValue) {
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
} else {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
selfClick: false,
|
|
|
|
target: null,
|
|
|
|
eventTarget: null,
|
|
|
|
outsideClickListener: null,
|
|
|
|
scrollHandler: null,
|
|
|
|
resizeListener: null,
|
|
|
|
container: null,
|
|
|
|
styleElement: null,
|
|
|
|
overlayEventListener: null,
|
2023-10-15 19:26:56 +00:00
|
|
|
documentKeydownListener: null,
|
2022-09-06 12:03:37 +00:00
|
|
|
beforeUnmount() {
|
|
|
|
if (this.dismissable) {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.scrollHandler) {
|
|
|
|
this.scrollHandler.destroy();
|
|
|
|
this.scrollHandler = null;
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.destroyStyle();
|
|
|
|
this.unbindResizeListener();
|
|
|
|
this.target = null;
|
|
|
|
|
|
|
|
if (this.container && this.autoZIndex) {
|
|
|
|
ZIndexUtils.clear(this.container);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.overlayEventListener) {
|
|
|
|
OverlayEventBus.off('overlay-click', this.overlayEventListener);
|
|
|
|
this.overlayEventListener = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.container = null;
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
if (this.breakpoints) {
|
|
|
|
this.createStyle();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
toggle(event, target) {
|
2022-09-14 11:26:01 +00:00
|
|
|
if (this.visible) this.hide();
|
|
|
|
else this.show(event, target);
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
show(event, target) {
|
|
|
|
this.visible = true;
|
|
|
|
this.eventTarget = event.currentTarget;
|
|
|
|
this.target = target || event.currentTarget;
|
|
|
|
},
|
|
|
|
hide() {
|
|
|
|
this.visible = false;
|
|
|
|
},
|
|
|
|
onContentClick() {
|
|
|
|
this.selfClick = true;
|
|
|
|
},
|
|
|
|
onEnter(el) {
|
|
|
|
this.container.setAttribute(this.attributeSelector, '');
|
2023-06-09 09:20:59 +00:00
|
|
|
DomHandler.addStyles(el, { position: 'absolute', top: '0', left: '0' });
|
2022-09-06 12:03:37 +00:00
|
|
|
this.alignOverlay();
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
if (this.dismissable) {
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.bindScrollListener();
|
|
|
|
this.bindResizeListener();
|
|
|
|
|
|
|
|
if (this.autoZIndex) {
|
|
|
|
ZIndexUtils.set('overlay', el, this.baseZIndex + this.$primevue.config.zIndex.overlay);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.overlayEventListener = (e) => {
|
|
|
|
if (this.container.contains(e.target)) {
|
|
|
|
this.selfClick = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
this.focus();
|
2022-09-06 12:03:37 +00:00
|
|
|
OverlayEventBus.on('overlay-click', this.overlayEventListener);
|
|
|
|
this.$emit('show');
|
2023-10-15 19:26:56 +00:00
|
|
|
|
|
|
|
if (this.closeOnEscape) {
|
|
|
|
this.bindDocumentKeyDownListener();
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
onLeave() {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
this.unbindScrollListener();
|
|
|
|
this.unbindResizeListener();
|
|
|
|
OverlayEventBus.off('overlay-click', this.overlayEventListener);
|
|
|
|
this.overlayEventListener = null;
|
|
|
|
this.$emit('hide');
|
|
|
|
},
|
|
|
|
onAfterLeave(el) {
|
|
|
|
if (this.autoZIndex) {
|
|
|
|
ZIndexUtils.clear(el);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
alignOverlay() {
|
|
|
|
DomHandler.absolutePosition(this.container, this.target);
|
|
|
|
|
|
|
|
const containerOffset = DomHandler.getOffset(this.container);
|
|
|
|
const targetOffset = DomHandler.getOffset(this.target);
|
|
|
|
let arrowLeft = 0;
|
|
|
|
|
|
|
|
if (containerOffset.left < targetOffset.left) {
|
|
|
|
arrowLeft = targetOffset.left - containerOffset.left;
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.container.style.setProperty('--overlayArrowLeft', `${arrowLeft}px`);
|
|
|
|
|
|
|
|
if (containerOffset.top < targetOffset.top) {
|
2023-06-09 12:44:26 +00:00
|
|
|
this.container.setAttribute('data-p-overlaypanel-flipped', 'true');
|
2023-05-29 20:18:28 +00:00
|
|
|
!this.isUnstyled && DomHandler.addClass(this.container, 'p-overlaypanel-flipped');
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onContentKeydown(event) {
|
2023-07-20 10:01:18 +00:00
|
|
|
if (event.code === 'Escape') {
|
|
|
|
this.hide();
|
|
|
|
DomHandler.focus(this.target);
|
|
|
|
}
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
onButtonKeydown(event) {
|
|
|
|
switch (event.code) {
|
|
|
|
case 'ArrowDown':
|
|
|
|
case 'ArrowUp':
|
|
|
|
case 'ArrowLeft':
|
|
|
|
case 'ArrowRight':
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
focus() {
|
|
|
|
let focusTarget = this.container.querySelector('[autofocus]');
|
|
|
|
|
|
|
|
if (focusTarget) {
|
|
|
|
focusTarget.focus();
|
|
|
|
}
|
|
|
|
},
|
2023-10-15 19:26:56 +00:00
|
|
|
onKeyDown(event) {
|
2023-10-25 15:27:30 +00:00
|
|
|
if (event.code === 'Escape' || this.closeOnEscape) {
|
2023-10-15 19:26:56 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
bindOutsideClickListener() {
|
|
|
|
if (!this.outsideClickListener && DomHandler.isClient()) {
|
|
|
|
this.outsideClickListener = (event) => {
|
|
|
|
if (this.visible && !this.selfClick && !this.isTargetClicked(event)) {
|
|
|
|
this.visible = false;
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.selfClick = false;
|
|
|
|
};
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
document.addEventListener('click', this.outsideClickListener);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbindOutsideClickListener() {
|
|
|
|
if (this.outsideClickListener) {
|
|
|
|
document.removeEventListener('click', this.outsideClickListener);
|
|
|
|
this.outsideClickListener = null;
|
|
|
|
this.selfClick = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
bindScrollListener() {
|
|
|
|
if (!this.scrollHandler) {
|
|
|
|
this.scrollHandler = new ConnectedOverlayScrollHandler(this.target, () => {
|
|
|
|
if (this.visible) {
|
|
|
|
this.visible = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scrollHandler.bindScrollListener();
|
|
|
|
},
|
|
|
|
unbindScrollListener() {
|
|
|
|
if (this.scrollHandler) {
|
|
|
|
this.scrollHandler.unbindScrollListener();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
bindResizeListener() {
|
|
|
|
if (!this.resizeListener) {
|
|
|
|
this.resizeListener = () => {
|
|
|
|
if (this.visible && !DomHandler.isTouchDevice()) {
|
|
|
|
this.visible = false;
|
|
|
|
}
|
|
|
|
};
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
window.addEventListener('resize', this.resizeListener);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbindResizeListener() {
|
|
|
|
if (this.resizeListener) {
|
|
|
|
window.removeEventListener('resize', this.resizeListener);
|
|
|
|
this.resizeListener = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isTargetClicked(event) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return this.eventTarget && (this.eventTarget === event.target || this.eventTarget.contains(event.target));
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
containerRef(el) {
|
|
|
|
this.container = el;
|
|
|
|
},
|
|
|
|
createStyle() {
|
2023-06-05 11:24:13 +00:00
|
|
|
if (!this.styleElement && !this.isUnstyled) {
|
2022-09-14 11:26:01 +00:00
|
|
|
this.styleElement = document.createElement('style');
|
|
|
|
this.styleElement.type = 'text/css';
|
2023-08-11 01:34:02 +00:00
|
|
|
DomHandler.setAttribute(this.styleElement, 'nonce', this.$primevue?.config?.csp?.nonce);
|
2022-09-14 11:26:01 +00:00
|
|
|
document.head.appendChild(this.styleElement);
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
let innerHTML = '';
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
for (let breakpoint in this.breakpoints) {
|
|
|
|
innerHTML += `
|
|
|
|
@media screen and (max-width: ${breakpoint}) {
|
|
|
|
.p-overlaypanel[${this.attributeSelector}] {
|
|
|
|
width: ${this.breakpoints[breakpoint]} !important;
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
`;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.styleElement.innerHTML = innerHTML;
|
2022-09-14 11:26:01 +00:00
|
|
|
}
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
destroyStyle() {
|
|
|
|
if (this.styleElement) {
|
|
|
|
document.head.removeChild(this.styleElement);
|
|
|
|
this.styleElement = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onOverlayClick(event) {
|
|
|
|
OverlayEventBus.emit('overlay-click', {
|
|
|
|
originalEvent: event,
|
|
|
|
target: this.target
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
attributeSelector() {
|
|
|
|
return UniqueComponentId();
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
|
|
|
closeAriaLabel() {
|
|
|
|
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.close : undefined;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
directives: {
|
2022-12-08 11:04:25 +00:00
|
|
|
focustrap: FocusTrap,
|
2022-09-14 11:26:01 +00:00
|
|
|
ripple: Ripple
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
components: {
|
2023-04-07 20:07:42 +00:00
|
|
|
Portal: Portal,
|
|
|
|
TimesIcon
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
</script>
|