Newer
Older
Markus Kalijärvi
committed
import {Component, Element, Prop, Host, Listen, Event, h, EventEmitter} from '@stencil/core';
// import {MenuItemVariants} from '../../utils/utils';
Markus Kalijärvi
committed
@Component({
tag: 'hy-menu-item',
styleUrl: 'menu-item.scss',
shadow: true
})
export class MenuItem {
@Element() el: HTMLElement;
@Prop() inActiveTrail: boolean = false;
@Prop() isActive: boolean = false;
@Prop() isHeading: boolean = false;
@Prop() isDemo: boolean = false;
Markus Kalijärvi
committed
@Prop() menuItemAlternative: boolean = false;
@Prop() menuLinkId: string = '';
Markus Kalijärvi
committed
@Prop() menuButtonSubmenuExpand: string = '';
Markus Kalijärvi
committed
@Prop({mutable: true}) url: string = '';
@Prop({mutable: true}) label: string = '';
@Prop({mutable: true}) ariaExpanded: boolean = false;
@Prop({mutable: true, reflect: true}) depth: number = 0;
@Prop({mutable: true}) hasChildren: boolean = null;
@Prop({mutable: true, reflect: true}) parentExpanded: boolean = false;
@Prop({mutable: true}) parentAsHeading: string = '';
@Event() routeClicked: EventEmitter;
@Event() menuContainerToggled: EventEmitter;
@Event() menuContainerActiveTrail: EventEmitter;
@Event() addBreadcrumb: EventEmitter;
@Listen('handleClick', {capture: true}) handleClick() {
this.menuContainerToggled.emit({triggerItem: this.menuLinkId, triggerType: 'add'});
Markus Kalijärvi
committed
const currentParent = this.el.parentNode;
this.addBreadcrumb.emit({
url: this.url,
label: currentParent.parentElement.getAttribute('label'),
bid: this.menuLinkId
});
}
componentWillLoad() {
Markus Kalijärvi
committed
// If is-active class is added by system, add it to menu component as well.
if (this.el.classList.contains('is-active')) {
this.isActive = true;
}
Markus Kalijärvi
committed
// Notify breadcrumbs if item is in active trail.
if (this.inActiveTrail && !this.isActive) {
const currentParent = this.el.parentNode;
this.addBreadcrumb.emit({
url: this.url,
label: currentParent.parentElement.getAttribute('label'),
bid: this.menuLinkId
});
}
// Trigger all parent menuLevelContainer elements in the same active-trail
// to open the menu.
if (this.isActive) {
const getParents = (elem) => {
let parents = [];
while (elem.parentNode && elem.parentNode.nodeName.toLowerCase() != 'hy-menu') {
Markus Kalijärvi
committed
elem = elem.parentNode;
parents.push(elem);
}
return parents;
};
const parents = getParents(this.el);
parents.forEach((element) => {
if (element.tagName.toLowerCase() === 'hy-menu-item') {
this.menuContainerActiveTrail.emit({triggerItem: element.getAttribute('menu-link-id')});
}
});
}
}
componentWillRender() {
Markus Kalijärvi
committed
this.hasChildren = this.el.getElementsByTagName('hy-menu-level-container').length >= 1;
Markus Kalijärvi
committed
let parentMenu = this.el.closest('hy-menu-item');
let nextParentMenu;
this.depth = 0;
while (parentMenu) {
nextParentMenu = parentMenu.parentElement.closest('hy-menu-item');
if (nextParentMenu === parentMenu) {
break;
} else {
if (nextParentMenu !== null) {
this.parentAsHeading = nextParentMenu;
}
parentMenu = nextParentMenu;
this.depth = this.depth + 1;
}
}
}
render() {
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
switch (this.menuType) {
case 'desktop':
return (
<Host
class={{
'is-active': this.isActive,
'hy-menu-item': true,
'hy-menu-item--alternative': this.menuItemAlternative,
'hy-menu-item--desktop': true,
'is-demo': this.isDemo
}}
>
<a
aria-current={this.isHeading.toString()}
href={this.url}
class={{
'is-active': this.isActive,
'in-active-trail': this.inActiveTrail,
'is-heading': this.isHeading,
'hy-menu-item--desktop': true,
'is-demo': this.isDemo
}}
>
<span class={'hy-menu-item__label'}>{this.label}</span>
</a>
</Host>
);
case 'mobile':
return (
<Host
class={{
'is-active': this.isActive,
'hy-menu-item': true,
'hy-menu-item--alternative': this.menuItemAlternative,
'hy-menu-item--mobile': true,
'is-demo': this.isDemo
}}
>
<a
aria-current={this.isHeading.toString()}
href={this.url}
class={{
'is-active': this.isActive,
'in-active-trail': this.inActiveTrail,
'is-heading': this.isHeading,
'hy-menu-item--mobile': true,
'is-demo': this.isDemo
}}
>
<span class={'hy-menu-item__label'}>{this.label}</span>
</a>
{this.hasChildren && (
<button
aria-haspopup={'true'}
aria-label={`Open submenu for ${this.label}`}
onClick={() => this.handleClick()}
class={'hy-menu-item__button'}
>
<hy-icon icon={'hy-icon-caret-right'} size={12} />
</button>
)}
Markus Kalijärvi
committed
{this.hasChildren && <slot />}
</Host>
);
case 'sidebar':
return (
<Host
class={{
'is-active': this.isActive,
'hy-menu-item': true,
'hy-menu-item--alternative': this.menuItemAlternative,
'hy-menu-item--sidebar': true,
'is-demo': this.isDemo
}}
Markus Kalijärvi
committed
>
<a
aria-current={this.isHeading.toString()}
href={this.url}
class={{
'is-active': this.isActive,
'in-active-trail': this.inActiveTrail,
'is-heading': this.isHeading,
'hy-menu-item--sidebar': true,
'is-demo': this.isDemo
}}
>
<span class={'hy-menu-item__label'}>{this.label}</span>
</a>
{this.hasChildren && (
<button
aria-haspopup={'true'}
aria-label={`${this.menuButtonSubmenuExpand} ${this.label}`}
onClick={() => this.handleClick()}
class={'hy-menu-item__button'}
>
<hy-icon icon={'hy-icon-caret-right'} size={12} />
</button>
)}
Markus Kalijärvi
committed
{this.hasChildren && <slot />}
</Host>
);
}