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; } componentWillRender() { this.dataMenuLanguageWatcher(this.dataMenuLanguage); this.labelsWatcher(this.labels); } render() { const black = 'var(--brand-main-nearly-black)'; 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> ) : ( <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> ); } }