primevue-mirror/components/lib/sidebar/Sidebar.vue

211 lines
7.1 KiB
Vue
Raw Normal View History

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')">
2024-04-08 15:26:46 +00:00
<transition name="p-drawer" @enter="onEnter" @after-enter="onAfterEnter" @before-leave="onBeforeLeave" @leave="onLeave" @after-leave="onAfterLeave" appear v-bind="ptm('transition')">
2024-02-11 23:48:31 +00:00
<div v-if="visible" :ref="containerRef" v-focustrap :class="cx('root')" role="complementary" :aria-modal="modal" v-bind="ptmi('root')">
<slot v-if="$slots.container" name="container" :onClose="hide" :closeCallback="hide"></slot>
2023-09-19 10:23:58 +00:00
<template v-else>
<div :ref="headerContainerRef" :class="cx('header')" v-bind="ptm('header')">
<slot name="header" :class="cx('title')">
<div v-if="header" :class="cx('title')" v-bind="ptm('title')">{{ header }}</div>
</slot>
<button v-if="showCloseIcon" :ref="closeButtonRef" v-ripple type="button" :class="cx('closeButton')" :aria-label="closeAriaLabel" @click="hide" v-bind="ptm('closeButton')" data-pc-group-section="iconcontainer">
2023-09-19 10:23:58 +00:00
<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';
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,
inheritAttrs: false,
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,
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,
2024-01-02 07:55:07 +00:00
documentKeydownListener: null,
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');
this.focus();
2024-01-05 07:04:17 +00:00
this.bindDocumentKeyDownListener();
2022-09-06 12:03:37 +00:00
if (this.autoZIndex) {
ZIndexUtils.set('modal', this.mask, this.baseZIndex || this.$primevue.config.zIndex.modal);
2022-09-06 12:03:37 +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
},
onBeforeLeave() {
2023-01-16 06:46:20 +00:00
if (this.modal) {
!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
2024-01-02 07:55:07 +00:00
this.unbindDocumentKeyDownListener();
this.containerVisible = false;
2023-01-16 09:29:51 +00:00
this.disableDocumentSettings();
this.$emit('after-hide');
2022-09-06 12:03:37 +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.header && findFocusableElement(this.headerContainer);
2022-09-14 11:26:01 +00:00
2022-12-08 11:04:25 +00:00
if (!focusTarget) {
focusTarget = this.$slots.default && findFocusableElement(this.container);
2022-12-08 11:04:25 +00:00
if (!focusTarget) {
focusTarget = this.closeButton;
2022-12-08 11:04:25 +00:00
}
2022-09-06 12:03:37 +00:00
}
2022-12-08 11:04:25 +00:00
focusTarget && DomHandler.focus(focusTarget);
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.blockBodyScroll();
2023-01-16 09:29:51 +00:00
}
},
disableDocumentSettings() {
this.unbindOutsideClickListener();
if (this.blockScroll) {
DomHandler.unblockBodyScroll();
2023-01-16 09:29:51 +00:00
}
},
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
},
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
},
2024-01-02 07:55:07 +00:00
bindDocumentKeyDownListener() {
if (!this.documentKeydownListener) {
this.documentKeydownListener = this.onKeydown;
document.addEventListener('keydown', this.documentKeydownListener);
}
},
unbindDocumentKeyDownListener() {
if (this.documentKeydownListener) {
document.removeEventListener('keydown', this.documentKeydownListener);
this.documentKeydownListener = null;
}
},
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: {
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>