Newer
Older
import {Component, Prop, Listen, h, State, Event, EventEmitter, Element} from '@stencil/core';
import {DonateLink} from '../../site-header/site-header';
import {SiteLogoColor, SiteLogoSize} from '../../../utils/utils';
tag: 'hy-menu',
styleUrl: 'menu.scss',
export class Menu {
@Element() el: HTMLElement;
@Prop() open = true;
@Prop() menuType: string = 'desktop';
@Prop() logoUrl: string;
@Prop() logoLabel: string;
@Prop() dataMenuDonate: string;
@Prop() dataMenuLanguage: string;
Markus Kalijärvi
committed
@Prop() menuButtonSubmenuExpand: string;
@Prop() isDemo: boolean = false;
@State() breadcrumbs: any;
@Event() menuContainerToggled: EventEmitter;
private donateLink: DonateLink[];
@Listen('removeBreadcrumb') removeBreadcrumb(data) {
let breadcrumbs = JSON.parse(JSON.stringify(this.breadcrumbs));
let currentBreadcrumb = breadcrumbs
.map(function (e) {
return e.bid;
})
.indexOf(data.detail.bid);
breadcrumbs.length = currentBreadcrumb;
for (let i = currentBreadcrumb, l = this.breadcrumbs.length; i < l; i++) {
this.menuContainerToggled.emit({triggerItem: this.breadcrumbs[i].bid, triggerType: 'remove'});
}
this.breadcrumbs = [];
this.breadcrumbs = breadcrumbs;
// Reset if home has been clicked.
if (currentBreadcrumb === 0) {
this.breadcrumbs = [];
// Trigger menu-level-container toggle for every menu level.
let elements = this.el.querySelectorAll('.hy-menu-level-container--mobile');
elements.forEach((element) => {
if (!element.classList.contains('hy-menu-level-container--level-1')) {
this.menuContainerToggled.emit({triggerItem: element.getAttribute('trigger-item'), triggerType: 'remove'});
}
});
@Listen('addBreadcrumb') addBreadcrumb(event) {
this.breadcrumbs = [
...this.breadcrumbs,
{
label: event.detail.label,
bid: event.detail.bid,
url: event.detail.url
}
];
}
componentWillLoad() {
this.breadcrumbs = [];
}
componentWillUpdate() {
this.el.children[0].setAttribute('menu-type', this.menuType);
Markus Kalijärvi
committed
this.el.children[0].setAttribute('menu-button-submenu-expand', this.menuButtonSubmenuExpand);
componentWillRender() {
const siteHeader = this.el.closest('hy-site-header');
if (siteHeader) {
this.donateLink = JSON.parse(siteHeader.getAttribute('data-menu-donate'));
}
// Set demo value to all children to provide better UI for DS users.
if (this.isDemo) {
const items = Array.from(this.el.children);
items.forEach((item) => {
item.setAttribute('is-demo', 'true');
});
}
}
switch (this.menuType) {
case 'desktop':
return (
<div class={'hy-wrapper'}>
<nav
role={'navigation'}
class={{
'hy-menu-wrapper--desktop': true,
'is-open': this.open
}}
>
<div class={'hy-menu hy-menu--desktop'}>
<slot />
</div>
</nav>
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
);
case 'mobile':
return (
<nav
role={'navigation'}
class={{
'hy-menu-wrapper--mobile': true,
'is-open': this.open
}}
>
<div class={'hy-menu__logo-container'} id={'menu-logo'}>
<hy-site-logo
size={SiteLogoSize.small}
color={SiteLogoColor.black}
url={this.logoUrl}
label={this.logoLabel}
/>
</div>
<div
id={'menu-bc-container'}
class={{'hy-menu__breadcrumbs': true, 'is-empty': this.breadcrumbs.length === 0}}
>
<hy-menu-language is-mobile={true} data-menu-language={this.dataMenuLanguage} />
{this.breadcrumbs.length
? this.breadcrumbs.map((breadcrumb, index) => {
return (
<hy-menu-mobile-breadcrumb label={breadcrumb.label} bid={breadcrumb.bid} is-first={index === 0} />
);
})
: ''}
</div>
<div
aria-hidden={this.open ? 'false' : 'true'}
class={{
'hy-menu': true,
'hy-menu--mobile': true,
'is-open': this.open,
'is-demo': this.isDemo
}}
>
<slot />
</div>
<div class={'hy-link__donate'}>
{this.donateLink
? this.donateLink.map((i) => {
return <a href={i.url}>{i.label}</a>;
})
: ''}
</div>
</nav>
);
case 'sidebar':
return (
<nav
role={'navigation'}
class={{
'hy-menu-wrapper--sidebar': true,
'is-open': this.open,
'is-demo': this.isDemo
}}
>
<div
aria-hidden={this.open ? 'false' : 'true'}
class={{
'hy-menu': true,
'hy-menu--sidebar': true,
'is-open': this.open,
'is-demo': this.isDemo
}}
>
<slot />
</div>
</nav>
);
}