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; @State() isMenuOpen: boolean = false; @Prop() dataMenuLanguage: MenuLanguage[] | string; @Prop() isMobile: boolean = false; private _dataMenuLanguage: MenuLanguage[]; @Watch('dataMenuLanguage') arrayDataWatcher(data: MenuLanguage[] | string) { if (typeof data === 'string') { this._dataMenuLanguage = JSON.parse(data); } else { this._dataMenuLanguage = data; } } @Listen('languageMenuToggle') languageMenuToggle() { this.isMenuOpen = !this.isMenuOpen; } componentWillRender() { this.arrayDataWatcher(this.dataMenuLanguage); } render() { const black = 'var(--brand-main-nearly-black)'; 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> ) : ( <Host class={'menu--language'}> <button onClick={() => this.languageMenuToggle()} class={{ 'menu--language__toggle': true, 'is-open': this.isMenuOpen }} > <hy-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> ); } }