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

432 lines
11 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<Portal>
<div v-if="containerVisible" :ref="maskRef" :class="maskClass" @mousedown="onMaskClick">
2023-01-16 06:46:20 +00:00
<transition name="p-sidebar" @enter="onEnter" @after-enter="onAfterEnter" @before-leave="onBeforeLeave" @leave="onLeave" @after-leave="onAfterLeave" appear>
2023-01-10 07:10:07 +00:00
<div v-if="visible" :ref="containerRef" v-focustrap :class="containerClass" role="complementary" :aria-modal="modal" @keydown="onKeydown" v-bind="$attrs">
2023-01-03 14:29:54 +00:00
<div :ref="headerContainerRef" class="p-sidebar-header">
<div v-if="$slots.header" class="p-sidebar-header-content">
<slot name="header"></slot>
</div>
<button v-if="showCloseIcon" :ref="closeButtonRef" v-ripple autofocus type="button" class="p-sidebar-close p-sidebar-icon p-link" :aria-label="closeAriaLabel" @click="hide">
<slot name="closeicon">
<component :is="closeIcon ? 'span' : 'TimesIcon'" :class="['p-sidebar-close-icon ', closeIcon]"></component>
</slot>
2023-01-03 14:29:54 +00:00
</button>
</div>
<div :ref="contentRef" class="p-sidebar-content">
<slot></slot>
2022-09-06 12:03:37 +00:00
</div>
</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';
2022-09-06 12:03:37 +00:00
export default {
name: 'Sidebar',
inheritAttrs: false,
emits: ['update:visible', 'show', 'hide', 'after-hide'],
2022-09-06 12:03:37 +00:00
props: {
visible: {
type: Boolean,
default: false
},
position: {
type: String,
default: 'left'
},
baseZIndex: {
type: Number,
default: 0
},
autoZIndex: {
type: Boolean,
default: true
},
dismissable: {
type: Boolean,
default: true
},
showCloseIcon: {
type: Boolean,
default: true
},
2022-12-08 11:04:25 +00:00
closeIcon: {
type: String,
default: undefined
2022-12-08 11:04:25 +00:00
},
2022-09-06 12:03:37 +00:00
modal: {
type: Boolean,
default: true
2023-01-03 14:29:54 +00:00
},
blockScroll: {
type: Boolean,
2023-01-13 08:32:18 +00:00
default: false
2022-09-06 12:03:37 +00:00
}
},
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,
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();
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) {
DomHandler.addClass(this.mask, 'p-component-overlay-leave');
}
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
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) => {
return container.querySelector('[autofocus]');
};
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
},
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
},
getPositionClass() {
const positions = ['left', 'right', 'top', 'bottom'];
const pos = positions.find((item) => item === this.position);
return pos ? `p-sidebar-${pos}` : '';
},
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: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
2023-01-03 14:29:54 +00:00
'p-sidebar p-component',
2022-09-14 11:26:01 +00:00
{
'p-input-filled': this.$primevue.config.inputStyle === 'filled',
2023-01-03 14:29:54 +00:00
'p-ripple-disabled': this.$primevue.config.ripple === false,
'p-sidebar-full': this.fullScreen
2022-09-14 11:26:01 +00:00
}
];
2022-09-06 12:03:37 +00:00
},
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;
2023-01-03 14:29:54 +00:00
},
maskClass() {
2023-01-03 14:29:54 +00:00
return [
'p-sidebar-mask',
this.getPositionClass(),
{
'p-component-overlay p-component-overlay-enter': this.modal,
'p-sidebar-mask-scrollblocker': this.blockScroll,
'p-sidebar-visible': this.containerVisible,
2023-01-03 14:29:54 +00:00
'p-sidebar-full': this.fullScreen
}
];
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>
<style>
2023-01-03 14:29:54 +00:00
.p-sidebar-mask {
2022-09-06 12:03:37 +00:00
position: fixed;
2023-01-03 14:29:54 +00:00
top: 0;
left: 0;
width: 100%;
height: 100%;
display: none;
justify-content: center;
align-items: center;
pointer-events: none;
background-color: transparent;
transition-property: background-color;
}
.p-sidebar-mask.p-component-overlay {
pointer-events: auto;
}
.p-sidebar-visible {
display: flex;
}
2023-01-03 14:29:54 +00:00
.p-sidebar {
2022-09-06 12:03:37 +00:00
display: flex;
flex-direction: column;
2023-01-03 14:29:54 +00:00
pointer-events: auto;
transform: translate3d(0px, 0px, 0px);
position: relative;
transition: transform 0.3s;
2022-09-06 12:03:37 +00:00
}
.p-sidebar-content {
overflow-y: auto;
2023-01-03 14:29:54 +00:00
flex-grow: 1;
2022-09-06 12:03:37 +00:00
}
.p-sidebar-header {
display: flex;
align-items: center;
justify-content: flex-end;
2023-01-03 14:29:54 +00:00
flex-shrink: 0;
2022-09-06 12:03:37 +00:00
}
.p-sidebar-icon {
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
2023-01-03 14:29:54 +00:00
position: relative;
2022-09-06 12:03:37 +00:00
}
2023-01-03 14:29:54 +00:00
.p-sidebar-full .p-sidebar {
2022-09-06 12:03:37 +00:00
transition: none;
2023-01-03 14:29:54 +00:00
transform: none;
width: 100vw !important;
height: 100vh !important;
max-height: 100%;
top: 0px !important;
left: 0px !important;
2022-09-06 12:03:37 +00:00
}
2023-01-03 14:29:54 +00:00
/* Animation */
/* Center */
2023-01-10 07:10:07 +00:00
.p-sidebar-left .p-sidebar-enter-from,
.p-sidebar-left .p-sidebar-leave-to {
2022-09-06 12:03:37 +00:00
transform: translateX(-100%);
}
2023-01-10 07:10:07 +00:00
.p-sidebar-right .p-sidebar-enter-from,
.p-sidebar-right .p-sidebar-leave-to {
2022-09-06 12:03:37 +00:00
transform: translateX(100%);
}
2023-01-10 07:10:07 +00:00
.p-sidebar-top .p-sidebar-enter-from,
.p-sidebar-top .p-sidebar-leave-to {
2022-09-06 12:03:37 +00:00
transform: translateY(-100%);
}
2023-01-10 07:10:07 +00:00
.p-sidebar-bottom .p-sidebar-enter-from,
.p-sidebar-bottom .p-sidebar-leave-to {
2022-09-06 12:03:37 +00:00
transform: translateY(100%);
}
2023-01-10 07:10:07 +00:00
.p-sidebar-full .p-sidebar-enter-from,
.p-sidebar-full .p-sidebar-leave-to {
2022-09-06 12:03:37 +00:00
opacity: 0;
}
2023-01-10 07:10:07 +00:00
.p-sidebar-full .p-sidebar-enter-active,
.p-sidebar-full .p-sidebar-leave-active {
2022-09-06 12:03:37 +00:00
transition: opacity 400ms cubic-bezier(0.25, 0.8, 0.25, 1);
}
2023-01-03 14:29:54 +00:00
/* Position */
.p-sidebar-left {
justify-content: flex-start;
}
.p-sidebar-right {
justify-content: flex-end;
}
.p-sidebar-top {
align-items: flex-start;
}
.p-sidebar-bottom {
align-items: flex-end;
}
/* Size */
.p-sidebar-left .p-sidebar {
width: 20rem;
height: 100%;
}
.p-sidebar-right .p-sidebar {
width: 20rem;
height: 100%;
}
.p-sidebar-top .p-sidebar {
height: 10rem;
width: 100%;
}
.p-sidebar-bottom .p-sidebar {
height: 10rem;
width: 100%;
}
.p-sidebar-left .p-sidebar-sm,
.p-sidebar-right .p-sidebar-sm {
2022-09-06 12:03:37 +00:00
width: 20rem;
}
2023-01-03 14:29:54 +00:00
.p-sidebar-left .p-sidebar-md,
.p-sidebar-right .p-sidebar-md {
2022-09-06 12:03:37 +00:00
width: 40rem;
}
2023-01-03 14:29:54 +00:00
.p-sidebar-left .p-sidebar-lg,
.p-sidebar-right .p-sidebar-lg {
2022-09-06 12:03:37 +00:00
width: 60rem;
}
2023-01-03 14:29:54 +00:00
.p-sidebar-top .p-sidebar-sm,
.p-sidebar-bottom .p-sidebar-sm {
2022-09-06 12:03:37 +00:00
height: 10rem;
}
2023-01-03 14:29:54 +00:00
.p-sidebar-top .p-sidebar-md,
.p-sidebar-bottom .p-sidebar-md {
2022-09-06 12:03:37 +00:00
height: 20rem;
}
2023-01-03 14:29:54 +00:00
.p-sidebar-top .p-sidebar-lg,
.p-sidebar-bottom .p-sidebar-lg {
2022-09-06 12:03:37 +00:00
height: 30rem;
}
2023-01-03 14:29:54 +00:00
.p-sidebar-left .p-sidebar-content,
.p-sidebar-right .p-sidebar-content,
.p-sidebar-top .p-sidebar-content,
.p-sidebar-bottom .p-sidebar-content {
width: 100%;
height: 100%;
}
2022-09-06 12:03:37 +00:00
@media screen and (max-width: 64em) {
2023-01-03 14:29:54 +00:00
.p-sidebar-left .p-sidebar-lg,
.p-sidebar-left .p-sidebar-md,
.p-sidebar-right .p-sidebar-lg,
.p-sidebar-right .p-sidebar-md {
2022-09-06 12:03:37 +00:00
width: 20rem;
}
}
</style>