primevue-mirror/components/menubar/Menubar.vue

164 lines
3.9 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="containerClass">
2022-09-14 11:26:01 +00:00
<div v-if="$slots.start" class="p-menubar-start">
2022-09-06 12:03:37 +00:00
<slot name="start"></slot>
</div>
<a ref="menubutton" tabindex="0" class="p-menubar-button" @click="toggle($event)">
<i class="pi pi-bars" />
</a>
2022-09-14 11:26:01 +00:00
<MenubarSub ref="rootmenu" :model="model" :root="true" :mobileActive="mobileActive" @leaf-click="onLeafClick" :template="$slots.item" :exact="exact" />
<div v-if="$slots.end" class="p-menubar-end">
2022-09-06 12:03:37 +00:00
<slot name="end"></slot>
</div>
</div>
</template>
<script>
import MenubarSub from './MenubarSub.vue';
2022-09-14 11:26:01 +00:00
import { ZIndexUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'Menubar',
props: {
2022-09-14 11:26:01 +00:00
model: {
2022-09-06 12:03:37 +00:00
type: Array,
default: null
},
exact: {
type: Boolean,
default: true
}
},
outsideClickListener: null,
data() {
return {
mobileActive: false
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
beforeUnmount() {
this.mobileActive = false;
this.unbindOutsideClickListener();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.$refs.rootmenu && this.$refs.rootmenu.$el) {
ZIndexUtils.clear(this.$refs.rootmenu.$el);
}
},
methods: {
toggle(event) {
if (this.mobileActive) {
this.mobileActive = false;
ZIndexUtils.clear(this.$refs.rootmenu.$el);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.mobileActive = true;
ZIndexUtils.set('menu', this.$refs.rootmenu.$el, this.$primevue.config.zIndex.menu);
}
this.bindOutsideClickListener();
event.preventDefault();
},
bindOutsideClickListener() {
if (!this.outsideClickListener) {
this.outsideClickListener = (event) => {
2022-09-14 11:26:01 +00:00
if (this.mobileActive && this.$refs.rootmenu.$el !== event.target && !this.$refs.rootmenu.$el.contains(event.target) && this.$refs.menubutton !== event.target && !this.$refs.menubutton.contains(event.target)) {
2022-09-06 12:03:37 +00:00
this.mobileActive = 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;
}
},
onLeafClick() {
this.mobileActive = false;
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return ['p-menubar p-component', { 'p-menubar-mobile-active': this.mobileActive }];
2022-09-06 12:03:37 +00:00
}
},
components: {
2022-09-14 11:26:01 +00:00
MenubarSub: MenubarSub
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-menubar {
display: flex;
align-items: center;
}
.p-menubar ul {
margin: 0;
padding: 0;
list-style: none;
}
.p-menubar .p-menuitem-link {
cursor: pointer;
display: flex;
align-items: center;
text-decoration: none;
overflow: hidden;
position: relative;
}
.p-menubar .p-menuitem-text {
line-height: 1;
}
.p-menubar .p-menuitem {
position: relative;
}
.p-menubar-root-list {
display: flex;
align-items: center;
}
.p-menubar-root-list > li ul {
display: none;
z-index: 1;
}
.p-menubar-root-list > .p-menuitem-active > .p-submenu-list {
display: block;
}
.p-menubar .p-submenu-list {
display: none;
position: absolute;
z-index: 1;
}
2022-09-14 11:26:01 +00:00
.p-menubar .p-submenu-list > .p-menuitem-active > .p-submenu-list {
2022-09-06 12:03:37 +00:00
display: block;
left: 100%;
top: 0;
}
.p-menubar .p-submenu-list .p-menuitem-link .p-submenu-icon {
margin-left: auto;
}
.p-menubar .p-menubar-custom,
.p-menubar .p-menubar-end {
margin-left: auto;
align-self: center;
}
.p-menubar-button {
display: none;
cursor: pointer;
align-items: center;
justify-content: center;
text-decoration: none;
}
</style>