2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<Portal>
|
2023-07-05 15:06:50 +00:00
|
|
|
<div v-if="containerVisible" :ref="maskRef" @mousedown="onMaskClick" :class="cx('mask')" :style="sx('mask', true, { position })" v-bind="ptm('mask')">
|
2023-08-02 12:02:43 +00:00
|
|
|
<transition name="p-sidebar" @enter="onEnter" @after-enter="onAfterEnter" @before-leave="onBeforeLeave" @leave="onLeave" @after-leave="onAfterLeave" appear v-bind="ptm('transition')">
|
2023-05-29 20:37:34 +00:00
|
|
|
<div v-if="visible" :ref="containerRef" v-focustrap :class="cx('root')" role="complementary" :aria-modal="modal" @keydown="onKeydown" v-bind="{ ...$attrs, ...ptm('root') }">
|
2023-09-19 10:23:58 +00:00
|
|
|
<slot v-if="$slots.container" name="container" :onClose="hide"></slot>
|
|
|
|
<template v-else>
|
|
|
|
<div :ref="headerContainerRef" :class="cx('header')" v-bind="ptm('header')">
|
|
|
|
<div v-if="$slots.header" :class="cx('headerContent')" v-bind="ptm('headerContent')">
|
|
|
|
<slot name="header"></slot>
|
|
|
|
</div>
|
|
|
|
<button v-if="showCloseIcon" :ref="closeButtonRef" v-ripple autofocus type="button" :class="cx('closeButton')" :aria-label="closeAriaLabel" @click="hide" v-bind="ptm('closeButton')" data-pc-group-section="iconcontainer">
|
|
|
|
<slot name="closeicon" :class="cx('closeIcon')">
|
|
|
|
<component :is="closeIcon ? 'span' : 'TimesIcon'" :class="[cx('closeIcon'), closeIcon]" v-bind="ptm('closeIcon')"></component>
|
|
|
|
</slot>
|
|
|
|
</button>
|
2023-01-03 14:29:54 +00:00
|
|
|
</div>
|
2023-09-19 10:23:58 +00:00
|
|
|
<div :ref="contentRef" :class="cx('content')" v-bind="ptm('content')">
|
|
|
|
<slot></slot>
|
|
|
|
</div>
|
|
|
|
</template>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
2023-01-03 14:29:54 +00:00
|
|
|
</transition>
|
|
|
|
</div>
|
2022-09-06 12:03:37 +00:00
|
|
|
</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 Portal from 'primevue/portal';
|
2022-12-08 11:04:25 +00:00
|
|
|
import Ripple from 'primevue/ripple';
|
|
|
|
import { DomHandler, ZIndexUtils } from 'primevue/utils';
|
2023-06-09 09:25:46 +00:00
|
|
|
import BaseSidebar from './BaseSidebar.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Sidebar',
|
2023-05-29 20:37:34 +00:00
|
|
|
extends: BaseSidebar,
|
2023-01-12 11:46:46 +00:00
|
|
|
inheritAttrs: false,
|
2023-01-13 14:38:43 +00:00
|
|
|
emits: ['update:visible', 'show', 'hide', 'after-hide'],
|
2023-01-16 09:29:51 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
containerVisible: this.visible
|
|
|
|
};
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
container: null,
|
2023-01-13 14:38:43 +00:00
|
|
|
mask: null,
|
2022-12-08 11:04:25 +00:00
|
|
|
content: null,
|
|
|
|
headerContainer: null,
|
|
|
|
closeButton: null,
|
2023-01-03 14:29:54 +00:00
|
|
|
outsideClickListener: null,
|
2023-01-13 14:38:43 +00:00
|
|
|
updated() {
|
|
|
|
if (this.visible) {
|
|
|
|
this.containerVisible = this.visible;
|
2023-01-03 14:29:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeUnmount() {
|
2023-01-16 09:29:51 +00:00
|
|
|
this.disableDocumentSettings();
|
|
|
|
|
|
|
|
if (this.mask && this.autoZIndex) {
|
|
|
|
ZIndexUtils.clear(this.mask);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
this.container = null;
|
2023-01-16 09:29:51 +00:00
|
|
|
this.mask = null;
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
hide() {
|
|
|
|
this.$emit('update:visible', false);
|
|
|
|
},
|
2023-01-03 14:29:54 +00:00
|
|
|
onEnter() {
|
2022-09-06 12:03:37 +00:00
|
|
|
this.$emit('show');
|
2023-01-13 14:38:43 +00:00
|
|
|
this.focus();
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
if (this.autoZIndex) {
|
2023-01-13 14:38:43 +00:00
|
|
|
ZIndexUtils.set('modal', this.mask, this.baseZIndex || this.$primevue.config.zIndex.modal);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2023-01-13 14:38:43 +00:00
|
|
|
},
|
2023-01-16 06:46:20 +00:00
|
|
|
onAfterEnter() {
|
2023-01-16 09:29:51 +00:00
|
|
|
this.enableDocumentSettings();
|
2023-01-16 06:46:20 +00:00
|
|
|
},
|
2023-01-13 14:38:43 +00:00
|
|
|
onBeforeLeave() {
|
2023-01-16 06:46:20 +00:00
|
|
|
if (this.modal) {
|
2023-06-09 12:44:26 +00:00
|
|
|
!this.isUnstyled && DomHandler.addClass(this.mask, 'p-component-overlay-leave');
|
2023-01-16 06:46:20 +00:00
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
onLeave() {
|
2023-01-03 14:29:54 +00:00
|
|
|
this.$emit('hide');
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2023-01-03 14:29:54 +00:00
|
|
|
onAfterLeave() {
|
2022-09-06 12:03:37 +00:00
|
|
|
if (this.autoZIndex) {
|
2023-01-03 14:29:54 +00:00
|
|
|
ZIndexUtils.clear(this.mask);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2023-01-10 07:10:07 +00:00
|
|
|
|
2023-01-13 14:38:43 +00:00
|
|
|
this.containerVisible = false;
|
2023-01-16 09:29:51 +00:00
|
|
|
this.disableDocumentSettings();
|
2023-01-13 14:38:43 +00:00
|
|
|
this.$emit('after-hide');
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2023-01-13 14:38:43 +00:00
|
|
|
onMaskClick(event) {
|
|
|
|
if (this.dismissable && this.modal && this.mask === event.target) {
|
|
|
|
this.hide();
|
2023-01-03 14:35:38 +00:00
|
|
|
}
|
2023-01-03 14:29:54 +00:00
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
focus() {
|
2022-12-08 11:04:25 +00:00
|
|
|
const findFocusableElement = (container) => {
|
2023-05-10 06:25:59 +00:00
|
|
|
return container && container.querySelector('[autofocus]');
|
2022-12-08 11:04:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let focusTarget = this.$slots.default && findFocusableElement(this.content);
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-12-08 11:04:25 +00:00
|
|
|
if (!focusTarget) {
|
|
|
|
focusTarget = this.$slots.header && findFocusableElement(this.headerContainer);
|
|
|
|
|
|
|
|
if (!focusTarget) {
|
|
|
|
focusTarget = findFocusableElement(this.container);
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-12-08 11:04:25 +00:00
|
|
|
|
|
|
|
focusTarget && focusTarget.focus();
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2023-01-16 09:29:51 +00:00
|
|
|
enableDocumentSettings() {
|
|
|
|
if (this.dismissable && !this.modal) {
|
|
|
|
this.bindOutsideClickListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.blockScroll) {
|
|
|
|
DomHandler.addClass(document.body, 'p-overflow-hidden');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disableDocumentSettings() {
|
|
|
|
this.unbindOutsideClickListener();
|
|
|
|
|
|
|
|
if (this.blockScroll) {
|
|
|
|
DomHandler.removeClass(document.body, 'p-overflow-hidden');
|
|
|
|
}
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
onKeydown(event) {
|
|
|
|
if (event.code === 'Escape') {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
containerRef(el) {
|
|
|
|
this.container = el;
|
2022-12-08 11:04:25 +00:00
|
|
|
},
|
2023-01-13 14:38:43 +00:00
|
|
|
maskRef(el) {
|
|
|
|
this.mask = el;
|
|
|
|
},
|
2022-12-08 11:04:25 +00:00
|
|
|
contentRef(el) {
|
|
|
|
this.content = el;
|
|
|
|
},
|
|
|
|
headerContainerRef(el) {
|
|
|
|
this.headerContainer = el;
|
|
|
|
},
|
|
|
|
closeButtonRef(el) {
|
|
|
|
this.closeButton = el;
|
2023-01-03 14:29:54 +00:00
|
|
|
},
|
|
|
|
bindOutsideClickListener() {
|
|
|
|
if (!this.outsideClickListener) {
|
|
|
|
this.outsideClickListener = (event) => {
|
2023-01-16 09:29:51 +00:00
|
|
|
if (this.isOutsideClicked(event)) {
|
2023-01-03 14:29:54 +00:00
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('click', this.outsideClickListener);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbindOutsideClickListener() {
|
|
|
|
if (this.outsideClickListener) {
|
|
|
|
document.removeEventListener('click', this.outsideClickListener);
|
|
|
|
this.outsideClickListener = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isOutsideClicked(event) {
|
|
|
|
return this.container && !this.container.contains(event.target);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
fullScreen() {
|
|
|
|
return this.position === 'full';
|
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:36:13 +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>
|