Newer
Older
Markus Kalijärvi
committed
import {ComponentLabels} from '../../site-header/site-header';
export interface MenuLanguage {
langCode: string;
url: string;
abbr: string;
label: string;
isActive?: boolean;
}
import {Component, h, Prop, Host, Watch, Element, Listen, State} from '@stencil/core';
@Component({
tag: 'hy-menu-language',
styleUrl: 'menu-language.scss',
shadow: true,
})
export class MenuLanguage {
@Element() el: HTMLElement;
@Prop() dataMenuLanguage: MenuLanguage[] | string;
@Prop() isMobile: boolean = false;
@Prop() labels?: ComponentLabels[] | string;
@State() isMenuOpen: boolean = false;
private _dataMenuLanguage: MenuLanguage[];
private _labels: ComponentLabels[];
@Watch('dataMenuLanguage') dataMenuLanguageWatcher(data: MenuLanguage[] | string) {
this._dataMenuLanguage = typeof data === 'string' ? JSON.parse(data) : data;
}
@Watch('labels') labelsWatcher(data: ComponentLabels[] | string) {
this._labels = typeof data === 'string' ? JSON.parse(data) : data;
}
@Listen('languageMenuToggle') languageMenuToggle() {
this.isMenuOpen = !this.isMenuOpen;
Ekaterina Kondareva
committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
let hyHeader = this.el.closest('.hy-site-header');
let hyBackdropDiv = (hyHeader as HTMLElement).children[0];
if (hyBackdropDiv) {
if (this.isMenuOpen) {
(hyBackdropDiv as HTMLElement).classList.add('is-active');
(hyBackdropDiv as HTMLElement).style.top = '90px';
} else {
(hyBackdropDiv as HTMLElement).classList.remove('is-active');
(hyBackdropDiv as HTMLElement).style.top = '0';
}
}
}
@Listen('click')
handleComponentClick(event) {
event.stopPropagation();
}
@Listen('click', {target: 'window'})
handleClick(event) {
if (this.isMenuOpen) {
const target = event.target as HTMLTextAreaElement;
const targetElement = target.tagName.toLowerCase();
if (targetElement !== 'hy-menu-language') {
const hyHeader = this.el.closest('.hy-site-header');
const hyBackdropDiv = (hyHeader as HTMLElement).children[0];
if (hyBackdropDiv && hyBackdropDiv.classList.contains('is-active')) {
(hyBackdropDiv as HTMLElement).classList.remove('is-active');
(hyBackdropDiv as HTMLElement).style.top = '0';
this.isMenuOpen = !this.isMenuOpen;
}
}
}
event.stopPropagation();
}
componentWillRender() {
this.dataMenuLanguageWatcher(this.dataMenuLanguage);
this.labelsWatcher(this.labels);
}
render() {
const black = 'var(--brand-main-nearly-black)';
Ekaterina Kondareva
committed
const menuLanguageContainerClass = ['menu--language'].join(' ');
return this.isMobile ? (
<Host class={'menu--language'}>
{this._dataMenuLanguage.map((item) => {
return (
<hy-menu-language-item
lang-code={item.langCode}
url={item.url}
label={item.label}
abbr={item.abbr}
is-active={item.isActive}
is-mobile={this.isMobile}
/>
);
})}
</Host>
) : (
Ekaterina Kondareva
committed
<Host class={menuLanguageContainerClass}>
<button
onClick={() => this.languageMenuToggle()}
class={{
'menu--language__toggle': true,
'is-open': this.isMenuOpen,
}}
aria-label={this.isMenuOpen ? this._labels['close'] : this._labels['open']}
>
<hy-icon class={'menu--language__globe-icon'} icon={'hy-icon-globe'} size={14} fill={black} />
<span>
{this._dataMenuLanguage.map((item) => {
if (item.isActive) {
return item.langCode;
}
})}
</span>
<hy-icon class={'menu--language__toggle__caret'} icon={'hy-icon-caret-down'} size={8} fill={black} />
</button>
<div
class={{
'menu--language__dropdown': true,
'is-open': this.isMenuOpen,
}}
>
{this._dataMenuLanguage.map((item) => {
return (
<hy-menu-language-item
lang-code={item.langCode}
url={item.url}
label={item.label}
abbr={item.abbr}
is-active={item.isActive}
is-mobile={this.isMobile}
/>
);
})}
</div>
</Host>
);
}
}