Refactor #3965 - For Menubar

pull/4004/head
Tuğçe Küçükoğlu 2023-05-29 17:06:26 +03:00
parent b85d2b5536
commit df4994ea68
4 changed files with 206 additions and 164 deletions

View File

@ -0,0 +1,158 @@
<script>
import BaseComponent from 'primevue/basecomponent';
import { useStyle } from 'primevue/usestyle';
const styles = `
.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;
flex-wrap: wrap;
}
.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;
}
.p-menubar .p-submenu-list > .p-menuitem-active > .p-submenu-list {
display: block;
left: 100%;
top: 0;
}
.p-menubar .p-submenu-list .p-menuitem .p-menuitem-content .p-menuitem-link .p-submenu-icon {
margin-left: auto;
}
.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;
}
`;
const inlineStyles = {
submenu: ({ context, processedItem }) => ({ display: context.isItemActive(processedItem) ? 'block' : 'none' })
};
const classes = {
root: ({ instance }) => [
'p-menubar p-component',
{
'p-menubar-mobile-active': instance.mobileActive
}
],
start: 'p-menubar-start',
button: 'p-menubar-button',
menu: 'p-menubar-root-list',
menuitem: ({ context, processedItem }) => [
'p-menuitem',
context.getItemProp(processedItem, 'class'),
{
'p-menuitem-active p-highlight': context.isItemActive(processedItem),
'p-focus': context.isItemFocused(processedItem),
'p-disabled': context.isItemDisabled(processedItem)
}
],
content: 'p-menuitem-content',
action: ({ context, isActive, isExactActive }) => [
'p-menuitem-link',
{
'router-link-active': isActive,
'router-link-active-exact': context.exact && isExactActive
}
],
icon: ({ context, processedItem }) => ['p-menuitem-icon', context.getItemProp(processedItem, 'icon')],
label: 'p-menuitem-text',
submenuIcon: 'p-submenu-icon',
submenu: 'p-submenu-list',
separator: ({ context, processedItem }) => ['p-menuitem-separator', context.getItemProp(processedItem, 'class')],
end: 'p-menubar-end'
};
const { load: loadStyle, unload: unloadStyle } = useStyle(styles, { id: 'primevue_menubar_style', manual: true });
export default {
name: 'BaseMenubar',
extends: BaseComponent,
props: {
model: {
type: Array,
default: null
},
exact: {
type: Boolean,
default: true
},
buttonProps: {
type: null,
default: null
},
'aria-labelledby': {
type: String,
default: null
},
'aria-label': {
type: String,
default: null
}
},
css: {
classes,
inlineStyles
},
watch: {
isUnstyled: {
immediate: true,
handler(newValue) {
!newValue && loadStyle();
}
}
}
};
</script>

View File

@ -59,6 +59,10 @@ export interface MenubarPassThroughOptions {
* Uses to pass attributes to the submenu icon's DOM element. * Uses to pass attributes to the submenu icon's DOM element.
*/ */
submenuIcon?: MenubarPassThroughOptionType; submenuIcon?: MenubarPassThroughOptionType;
/**
* Uses to pass attributes to the submenu's DOM element.
*/
submenu?: MenubarPassThroughOptionType;
/** /**
* Uses to pass attributes to the separator's DOM element. * Uses to pass attributes to the separator's DOM element.
*/ */
@ -182,6 +186,11 @@ export interface MenubarProps {
* @type {MenubarPassThroughOptions} * @type {MenubarPassThroughOptions}
*/ */
pt?: MenubarPassThroughOptions; pt?: MenubarPassThroughOptions;
/**
* When enabled, it removes component related styles in the core.
* @defaultValue false
*/
unstyled?: boolean;
} }
/** /**

View File

@ -1,6 +1,6 @@
<template> <template>
<div :ref="containerRef" :class="containerClass" v-bind="ptm('root')"> <div :ref="containerRef" :class="cx('root')" v-bind="ptm('root')">
<div v-if="$slots.start" class="p-menubar-start" v-bind="ptm('start')"> <div v-if="$slots.start" :class="cx('start')" v-bind="ptm('start')">
<slot name="start"></slot> <slot name="start"></slot>
</div> </div>
<a <a
@ -8,7 +8,7 @@
ref="menubutton" ref="menubutton"
role="button" role="button"
tabindex="0" tabindex="0"
class="p-menubar-button" :class="cx('button')"
:aria-haspopup="model.length && model.length > 0 ? true : false" :aria-haspopup="model.length && model.length > 0 ? true : false"
:aria-expanded="mobileActive" :aria-expanded="mobileActive"
:aria-controls="id" :aria-controls="id"
@ -24,7 +24,6 @@
<MenubarSub <MenubarSub
:ref="menubarRef" :ref="menubarRef"
:id="id" :id="id"
class="p-menubar-root-list"
role="menubar" role="menubar"
:items="processedItems" :items="processedItems"
:templates="$slots" :templates="$slots"
@ -46,44 +45,22 @@
@item-click="onItemClick" @item-click="onItemClick"
@item-mouseenter="onItemMouseEnter" @item-mouseenter="onItemMouseEnter"
/> />
<div v-if="$slots.end" class="p-menubar-end" v-bind="ptm('end')"> <div v-if="$slots.end" :class="cx('end')" v-bind="ptm('end')">
<slot name="end"></slot> <slot name="end"></slot>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import BaseComponent from 'primevue/basecomponent';
import BarsIcon from 'primevue/icons/bars'; import BarsIcon from 'primevue/icons/bars';
import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils'; import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
import BaseMenubar from './BaseMenubar.vue';
import MenubarSub from './MenubarSub.vue'; import MenubarSub from './MenubarSub.vue';
export default { export default {
name: 'Menubar', name: 'Menubar',
extends: BaseComponent, extends: BaseMenubar,
emits: ['focus', 'blur'], emits: ['focus', 'blur'],
props: {
model: {
type: Array,
default: null
},
exact: {
type: Boolean,
default: true
},
buttonProps: {
type: null,
default: null
},
'aria-labelledby': {
type: String,
default: null
},
'aria-label': {
type: String,
default: null
}
},
data() { data() {
return { return {
id: this.$attrs.id, id: this.$attrs.id,
@ -405,7 +382,7 @@ export default {
onEnterKey(event) { onEnterKey(event) {
if (this.focusedItemInfo.index !== -1) { if (this.focusedItemInfo.index !== -1) {
const element = DomHandler.findSingle(this.menubar, `li[id="${`${this.focusedItemId}`}"]`); const element = DomHandler.findSingle(this.menubar, `li[id="${`${this.focusedItemId}`}"]`);
const anchorElement = element && DomHandler.findSingle(element, '.p-menuitem-link'); const anchorElement = element && DomHandler.findSingle(element, 'a[data-pc-section="action"]');
anchorElement ? anchorElement.click() : element && element.click(); anchorElement ? anchorElement.click() : element && element.click();
@ -595,9 +572,6 @@ export default {
} }
}, },
computed: { computed: {
containerClass() {
return ['p-menubar p-component', { 'p-menubar-mobile-active': this.mobileActive }];
},
processedItems() { processedItems() {
return this.createProcessedItems(this.model || []); return this.createProcessedItems(this.model || []);
}, },
@ -616,77 +590,3 @@ export default {
} }
}; };
</script> </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;
flex-wrap: wrap;
}
.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;
}
.p-menubar .p-submenu-list > .p-menuitem-active > .p-submenu-list {
display: block;
left: 100%;
top: 0;
}
.p-menubar .p-submenu-list .p-menuitem .p-menuitem-content .p-menuitem-link .p-submenu-icon {
margin-left: auto;
}
.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>

View File

@ -1,11 +1,11 @@
<template> <template>
<ul v-bind="ptm('menu')"> <ul :class="level === 0 ? getCXOptions('menu') : getCXOptions('submenu')" v-bind="level === 0 ? ptm('menu') : ptm('submenu')">
<template v-for="(processedItem, index) of items" :key="getItemKey(processedItem)"> <template v-for="(processedItem, index) of items" :key="getItemKey(processedItem)">
<li <li
v-if="isItemVisible(processedItem) && !getItemProp(processedItem, 'separator')" v-if="isItemVisible(processedItem) && !getItemProp(processedItem, 'separator')"
:id="getItemId(processedItem)" :id="getItemId(processedItem)"
:style="getItemProp(processedItem, 'style')" :style="getItemProp(processedItem, 'style')"
:class="getItemClass(processedItem)" :class="getCXOptions('menuitem', { processedItem })"
role="menuitem" role="menuitem"
:aria-label="getItemLabel(processedItem)" :aria-label="getItemLabel(processedItem)"
:aria-disabled="isItemDisabled(processedItem) || undefined" :aria-disabled="isItemDisabled(processedItem) || undefined"
@ -15,32 +15,26 @@
:aria-setsize="getAriaSetSize()" :aria-setsize="getAriaSetSize()"
:aria-posinset="getAriaPosInset(index)" :aria-posinset="getAriaPosInset(index)"
v-bind="getPTOptions(processedItem, 'menuitem')" v-bind="getPTOptions(processedItem, 'menuitem')"
:data-p-highlight="isItemActive(processedItem)"
:data-p-focused="isItemFocused(processedItem)"
:data-p-disabled="isItemDisabled(processedItem)"
> >
<div class="p-menuitem-content" @click="onItemClick($event, processedItem)" @mouseenter="onItemMouseEnter($event, processedItem)" v-bind="getPTOptions(processedItem, 'content')"> <div :class="getCXOptions('content')" @click="onItemClick($event, processedItem)" @mouseenter="onItemMouseEnter($event, processedItem)" v-bind="getPTOptions(processedItem, 'content')">
<template v-if="!templates.item"> <template v-if="!templates.item">
<router-link v-if="getItemProp(processedItem, 'to') && !isItemDisabled(processedItem)" v-slot="{ navigate, href, isActive, isExactActive }" :to="getItemProp(processedItem, 'to')" custom> <router-link v-if="getItemProp(processedItem, 'to') && !isItemDisabled(processedItem)" v-slot="{ navigate, href, isActive, isExactActive }" :to="getItemProp(processedItem, 'to')" custom>
<a v-ripple :href="href" :class="getItemActionClass(processedItem, { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)" v-bind="getPTOptions(processedItem, 'action')"> <a v-ripple :href="href" :class="getCXOptions('action', { isActive, isExactActive })" tabindex="-1" aria-hidden="true" @click="onItemActionClick($event, navigate)" v-bind="getPTOptions(processedItem, 'action')">
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getItemIconClass(processedItem)" /> <component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getCXOptions('icon', { processedItem })" />
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" v-bind="getPTOptions(processedItem, 'icon')" /> <span v-else-if="getItemProp(processedItem, 'icon')" :class="getCXOptions('icon', { processedItem })" v-bind="getPTOptions(processedItem, 'icon')" />
<span class="p-menuitem-text" v-bind="getPTOptions(processedItem, 'label')">{{ getItemLabel(processedItem) }}</span> <span :class="getCXOptions('label')" v-bind="getPTOptions(processedItem, 'label')">{{ getItemLabel(processedItem) }}</span>
</a> </a>
</router-link> </router-link>
<a <a v-else v-ripple :href="getItemProp(processedItem, 'url')" :class="getCXOptions('action')" :target="getItemProp(processedItem, 'target')" tabindex="-1" aria-hidden="true" v-bind="getPTOptions(processedItem, 'action')">
v-else <component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getCXOptions('icon', { processedItem })" />
v-ripple <span v-else-if="getItemProp(processedItem, 'icon')" :class="getCXOptions('icon', { processedItem })" v-bind="getPTOptions(processedItem, 'icon')" />
:href="getItemProp(processedItem, 'url')" <span :class="getCXOptions('label')" v-bind="getPTOptions(processedItem, 'label')">{{ getItemLabel(processedItem) }}</span>
:class="getItemActionClass(processedItem)"
:target="getItemProp(processedItem, 'target')"
tabindex="-1"
aria-hidden="true"
v-bind="getPTOptions(processedItem, 'action')"
>
<component v-if="templates.itemicon" :is="templates.itemicon" :item="processedItem.item" :class="getItemIconClass(processedItem)" />
<span v-else-if="getItemProp(processedItem, 'icon')" :class="getItemIconClass(processedItem)" v-bind="getPTOptions(processedItem, 'icon')" />
<span class="p-menuitem-text" v-bind="getPTOptions(processedItem, 'label')">{{ getItemLabel(processedItem) }}</span>
<template v-if="getItemProp(processedItem, 'items')"> <template v-if="getItemProp(processedItem, 'items')">
<component v-if="templates.submenuicon" :is="templates.submenuicon" :root="root" :active="isItemActive(processedItem)" class="p-submenu-icon" /> <component v-if="templates.submenuicon" :is="templates.submenuicon" :root="root" :active="isItemActive(processedItem)" :class="getCXOptions('submenuIcon')" />
<component v-else :is="root ? 'AngleDownIcon' : 'AngleRightIcon'" class="p-submenu-icon" v-bind="getPTOptions(processedItem, 'submenuIcon')" /> <component v-else :is="root ? 'AngleDownIcon' : 'AngleRightIcon'" :class="getCXOptions('submenuIcon')" v-bind="getPTOptions(processedItem, 'submenuIcon')" />
</template> </template>
</a> </a>
</template> </template>
@ -50,7 +44,7 @@
v-if="isItemVisible(processedItem) && isItemGroup(processedItem)" v-if="isItemVisible(processedItem) && isItemGroup(processedItem)"
:menuId="menuId" :menuId="menuId"
role="menu" role="menu"
class="p-submenu-list" :style="getSXOptions('submenu', { processedItem })"
:focusedItemId="focusedItemId" :focusedItemId="focusedItemId"
:items="processedItem.items" :items="processedItem.items"
:mobileActive="mobileActive" :mobileActive="mobileActive"
@ -67,7 +61,7 @@
v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')" v-if="isItemVisible(processedItem) && getItemProp(processedItem, 'separator')"
:id="getItemId(processedItem)" :id="getItemId(processedItem)"
:style="getItemProp(processedItem, 'style')" :style="getItemProp(processedItem, 'style')"
:class="getSeparatorItemClass(processedItem)" :class="getCXOptions('separator', { processedItem })"
role="separator" role="separator"
v-bind="ptm('separator')" v-bind="ptm('separator')"
></li> ></li>
@ -76,15 +70,15 @@
</template> </template>
<script> <script>
import BaseComponent from 'primevue/basecomponent';
import AngleDownIcon from 'primevue/icons/angledown'; import AngleDownIcon from 'primevue/icons/angledown';
import AngleRightIcon from 'primevue/icons/angleright'; import AngleRightIcon from 'primevue/icons/angleright';
import Ripple from 'primevue/ripple'; import Ripple from 'primevue/ripple';
import { ObjectUtils } from 'primevue/utils'; import { ObjectUtils } from 'primevue/utils';
import BaseMenubar from './BaseMenubar.vue';
export default { export default {
name: 'MenubarSub', name: 'MenubarSub',
extends: BaseComponent, extends: BaseMenubar,
emits: ['item-mouseenter', 'item-click'], emits: ['item-mouseenter', 'item-click'],
props: { props: {
items: { items: {
@ -150,6 +144,18 @@ export default {
} }
}); });
}, },
getCXOptions(key, params) {
return this.cx(key, {
...params,
context: this
});
},
getSXOptions(key, params) {
return this.sx(key, true, {
...params,
context: this
});
},
isItemActive(processedItem) { isItemActive(processedItem) {
return this.activeItemPath.some((path) => path.key === processedItem.key); return this.activeItemPath.some((path) => path.key === processedItem.key);
}, },
@ -180,37 +186,6 @@ export default {
}, },
getAriaPosInset(index) { getAriaPosInset(index) {
return index - this.items.slice(0, index).filter((processedItem) => this.isItemVisible(processedItem) && this.getItemProp(processedItem, 'separator')).length + 1; return index - this.items.slice(0, index).filter((processedItem) => this.isItemVisible(processedItem) && this.getItemProp(processedItem, 'separator')).length + 1;
},
getItemClass(processedItem) {
return [
'p-menuitem',
this.getItemProp(processedItem, 'class'),
{
'p-menuitem-active p-highlight': this.isItemActive(processedItem),
'p-focus': this.isItemFocused(processedItem),
'p-disabled': this.isItemDisabled(processedItem)
}
];
},
getItemActionClass(processedItem, routerProps) {
return [
'p-menuitem-link',
{
'router-link-active': routerProps && routerProps.isActive,
'router-link-active-exact': this.exact && routerProps && routerProps.isExactActive
}
];
},
getItemIconClass(processedItem) {
return ['p-menuitem-icon', this.getItemProp(processedItem, 'icon')];
},
getSeparatorItemClass(processedItem) {
return ['p-menuitem-separator', this.getItemProp(processedItem, 'class')];
}
},
computed: {
containerClass() {
return { 'p-submenu-list': !this.root, 'p-menubar-root-list': this.root };
} }
}, },
components: { components: {