primevue-mirror/components/sidebar/Sidebar.vue

350 lines
8.7 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<Portal>
<transition name="p-sidebar" @enter="onEnter" @leave="onLeave" @after-leave="onAfterLeave" appear>
2022-12-08 11:04:25 +00:00
<div v-if="visible" :ref="containerRef" v-focustrap :class="containerClass" role="complementary" :aria-modal="modal" @keydown="onKeydown" v-bind="$attrs">
<div :ref="headerContainerRef" class="p-sidebar-header">
2022-09-14 11:26:01 +00:00
<div v-if="$slots.header" class="p-sidebar-header-content">
2022-09-06 12:03:37 +00:00
<slot name="header"></slot>
</div>
2022-12-08 11:04:25 +00:00
<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">
<span :class="['p-sidebar-close-icon', closeIcon]" />
2022-09-06 12:03:37 +00:00
</button>
</div>
2022-12-08 11:04:25 +00:00
<div :ref="contentRef" class="p-sidebar-content">
2022-09-06 12:03:37 +00:00
<slot></slot>
</div>
</div>
</transition>
</Portal>
</template>
<script>
2022-12-08 11:04:25 +00:00
import FocusTrap from 'primevue/focustrap';
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,
2022-09-14 11:26:01 +00:00
emits: ['update:visible', 'show', '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: 'pi pi-times'
},
2022-09-06 12:03:37 +00:00
modal: {
type: Boolean,
default: true
}
},
mask: null,
maskClickListener: null,
container: null,
2022-12-08 11:04:25 +00:00
content: null,
headerContainer: null,
closeButton: null,
2022-09-06 12:03:37 +00:00
beforeUnmount() {
this.destroyModal();
if (this.container && this.autoZIndex) {
ZIndexUtils.clear(this.container);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.container = null;
},
methods: {
hide() {
this.$emit('update:visible', false);
},
onEnter(el) {
this.$emit('show');
if (this.autoZIndex) {
ZIndexUtils.set('modal', el, this.baseZIndex || this.$primevue.config.zIndex.modal);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.focus();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.modal && !this.fullScreen) {
this.enableModality();
}
},
onLeave() {
this.$emit('hide');
if (this.modal) {
2022-09-06 12:03:37 +00:00
this.disableModality();
}
},
onAfterLeave(el) {
if (this.autoZIndex) {
ZIndexUtils.clear(el);
}
},
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
},
enableModality() {
if (!this.mask) {
this.mask = document.createElement('div');
this.mask.setAttribute('class', 'p-sidebar-mask p-component-overlay p-component-overlay-enter');
this.mask.style.zIndex = String(parseInt(this.container.style.zIndex, 10) - 1);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.dismissable) {
this.bindMaskClickListener();
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
document.body.appendChild(this.mask);
DomHandler.addClass(document.body, 'p-overflow-hidden');
}
},
disableModality() {
if (this.mask) {
DomHandler.addClass(this.mask, 'p-component-overlay-leave');
this.mask.addEventListener('animationend', () => {
this.destroyModal();
});
}
},
bindMaskClickListener() {
if (!this.maskClickListener) {
this.maskClickListener = () => {
this.hide();
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.mask.addEventListener('click', this.maskClickListener);
}
},
2022-12-08 11:04:25 +00:00
onKeydown(event) {
if (event.code === 'Escape') {
this.hide();
}
},
2022-09-06 12:03:37 +00:00
unbindMaskClickListener() {
if (this.maskClickListener) {
this.mask.removeEventListener('click', this.maskClickListener);
this.maskClickListener = null;
}
},
destroyModal() {
if (this.mask) {
this.unbindMaskClickListener();
document.body.removeChild(this.mask);
DomHandler.removeClass(document.body, 'p-overflow-hidden');
this.mask = null;
}
},
containerRef(el) {
this.container = el;
2022-12-08 11:04:25 +00:00
},
contentRef(el) {
this.content = el;
},
headerContainerRef(el) {
this.headerContainer = el;
},
closeButtonRef(el) {
this.closeButton = el;
2022-09-06 12:03:37 +00:00
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
'p-sidebar p-component p-sidebar-' + this.position,
{
'p-sidebar-active': this.visible,
'p-input-filled': this.$primevue.config.inputStyle === 'filled',
'p-ripple-disabled': this.$primevue.config.ripple === false
}
];
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;
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: {
2022-09-14 11:26:01 +00:00
Portal: Portal
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>
.p-sidebar {
position: fixed;
2022-09-14 11:26:01 +00:00
transition: transform 0.3s;
2022-09-06 12:03:37 +00:00
display: flex;
flex-direction: column;
}
.p-sidebar-content {
position: relative;
overflow-y: auto;
}
.p-sidebar-header {
display: flex;
align-items: center;
justify-content: flex-end;
}
.p-sidebar-icon {
display: flex;
align-items: center;
justify-content: center;
position: relative;
overflow: hidden;
}
.p-sidebar-left {
top: 0;
left: 0;
width: 20rem;
height: 100%;
}
.p-sidebar-right {
top: 0;
right: 0;
width: 20rem;
height: 100%;
}
.p-sidebar-top {
top: 0;
left: 0;
width: 100%;
height: 10rem;
}
.p-sidebar-bottom {
bottom: 0;
left: 0;
width: 100%;
height: 10rem;
}
.p-sidebar-full {
width: 100%;
height: 100%;
top: 0;
left: 0;
-webkit-transition: none;
transition: none;
}
.p-sidebar-left.p-sidebar-enter-from,
.p-sidebar-left.p-sidebar-leave-to {
transform: translateX(-100%);
}
.p-sidebar-right.p-sidebar-enter-from,
.p-sidebar-right.p-sidebar-leave-to {
transform: translateX(100%);
}
.p-sidebar-top.p-sidebar-enter-from,
.p-sidebar-top.p-sidebar-leave-to {
transform: translateY(-100%);
}
.p-sidebar-bottom.p-sidebar-enter-from,
.p-sidebar-bottom.p-sidebar-leave-to {
transform: translateY(100%);
}
.p-sidebar-full.p-sidebar-enter-from,
.p-sidebar-full.p-sidebar-leave-to {
opacity: 0;
}
.p-sidebar-full.p-sidebar-enter-active,
.p-sidebar-full.p-sidebar-leave-active {
transition: opacity 400ms cubic-bezier(0.25, 0.8, 0.25, 1);
}
.p-sidebar-left.p-sidebar-sm,
.p-sidebar-right.p-sidebar-sm {
width: 20rem;
}
.p-sidebar-left.p-sidebar-md,
.p-sidebar-right.p-sidebar-md {
width: 40rem;
}
.p-sidebar-left.p-sidebar-lg,
.p-sidebar-right.p-sidebar-lg {
width: 60rem;
}
.p-sidebar-top.p-sidebar-sm,
.p-sidebar-bottom.p-sidebar-sm {
height: 10rem;
}
.p-sidebar-top.p-sidebar-md,
.p-sidebar-bottom.p-sidebar-md {
height: 20rem;
}
.p-sidebar-top.p-sidebar-lg,
.p-sidebar-bottom.p-sidebar-lg {
height: 30rem;
}
@media screen and (max-width: 64em) {
.p-sidebar-left.p-sidebar-lg,
.p-sidebar-left.p-sidebar-md,
.p-sidebar-right.p-sidebar-lg,
.p-sidebar-right.p-sidebar-md {
width: 20rem;
}
}
</style>