Newer
Older
export interface DesktopLinks {
label: string;
url: string;
menuLinkId: string;
}
export interface DonateLink {
url: string;
label: string;
}
Markus Kalijärvi
committed
export interface ComponentLabels {
open: string;
close: string;
expand?: string;
label?: string;
}
import {Component, Element, Event, EventEmitter, h, Listen, Prop, State} from '@stencil/core';
import ResizeObserver from 'resize-observer-polyfill';
import {MenuType, ColorVariant, SiteLogoSize} from '../../utils/utils';
@Component({
tag: 'hy-site-header',
styleUrl: 'site-header.scss',
shadow: true,
})
export class SiteHeader {
@Element() el: HTMLElement;
@Prop() dataMenuLanguage: string;
@Prop() dataMenuDonate: string;
Markus Kalijärvi
committed
@Prop() dataSiteHeaderLabels: string;
@Prop() logoUrl?: string;
@Prop() logoLabel?: string;
@Prop() menuLabel: string = 'Menu';
@Prop() menuLabelOpen?: string;
@Prop() menuLabelClose?: string;
@Prop({reflect: true}) menuType: MenuType = MenuType.default;
/*
First level menu links to be displayed on Desktop screens.
* */
@Prop() dataDesktopLinks: DesktopLinks[] | string;
@Prop() dataMainMenuLinks: DesktopLinks[] | string;
@Prop() dataSearchTools: DesktopLinks[] | string;
@State() isMobile: boolean;
@State() isMenuOpen: boolean = false;
@State() isDesktopMenuOpen: boolean = false;
@Event() headerScrollUp: EventEmitter;
private ro: ResizeObserver;
private donateLink: DonateLink[];
Markus Kalijärvi
committed
private menuLabels: ComponentLabels[];
private searchLabels: ComponentLabels[];
private languageLabels: ComponentLabels[];
@State() lastScrollTop = 0;
@State() delta = 5;
@State() navbarHeight = 0;
@State() didScroll = false;
@State() intervalId;
// Listener for toggling mobile menu panel on or off.
@Listen('mobileMenuToggle') mobileMenuToggle() {
this.isMenuOpen = !this.isMenuOpen;
@Listen('scroll', {target: 'window'})
handleScroll() {
if (this.el.getAttribute('menu-type') === 'desktop') {
this.didScroll = true;
}
// Set the browser resize observer to gather information about browser width.
this.ro = new ResizeObserver((entries) => {
for (const entry of entries) {
this.applySizeClasses(entry.contentRect.width);
}
});
this.ro.observe(document.body);
// Pass the dataMenuLanguage prop to menu component.
this.el.children[0].setAttribute('data-menu-language', this.dataMenuLanguage);
this.navbarHeight = this.el.getBoundingClientRect().height;
// Check for site header labels and set them to variables accordingly.
if (this.dataSiteHeaderLabels) {
const labels = JSON.parse(this.dataSiteHeaderLabels);
this.menuLabels = labels.menu_labels;
this.languageLabels = labels.language_labels;
this.searchLabels = labels.search_labels;
this.el.children[0].setAttribute('menu-button-breadcrumb-home', this.menuLabels['home']);
this.el.children[0].setAttribute('menu-button-breadcrumb-main', this.menuLabels['main']);
this.el.children[0].setAttribute('menu-button-breadcrumb-logourl', this.logoUrl);
}
// Check for the donation link information and set it to variables accordingly.
// Also pass the donation link data to menu-component.
if (this.dataMenuDonate) {
this.donateLink = JSON.parse(this.dataMenuDonate);
this.el.children[0].setAttribute('data-menu-donate', this.dataMenuDonate);
}
// Pass the necessary information to menu component. These props will be
// used in mobile menu.
this.el.children[0].setAttribute('open', this.isMenuOpen.toString());
this.el.children[0].setAttribute('logo-label', this.logoLabel);
this.el.children[0].setAttribute('logo-url', this.logoUrl);
Markus Kalijärvi
committed
this.el.children[0].setAttribute('menu-button-submenu-expand', this.menuLabels['expand']);
this.el.children[0].setAttribute('menu-button-breadcrumb-return', this.menuLabels['return']);
this.el.children[0].setAttribute('menu-button-breadcrumb-home', this.menuLabels['home']);
this.el.children[0].setAttribute('menu-button-breadcrumb-main', this.menuLabels['main']);
this.el.children[0].setAttribute('menu-button-breadcrumb-logourl', this.logoUrl);
Markus Kalijärvi
committed
this.el.children[0].setAttribute('menu-language-label-open', this.languageLabels['open']);
this.el.children[0].setAttribute('menu-language-label-close', this.languageLabels['close']);
this.el.children[0].setAttribute('label-front-page', this.menuLabels['front_page']);
this.intervalId = setInterval(() => {
this.timer();
}, 250);
}
timer() {
if (this.didScroll) {
this.hasScrolled();
this.didScroll = false;
}
}
hasScrolled() {
let topOffset = window.pageYOffset;
const bodyElementClasses = document.querySelector('body').classList;
bodyElementClasses.contains('hy-menu-sidepanel__no-scroll') ||
bodyElementClasses.contains('hy-menu-sidebar__no-scroll')
if (Math.abs(this.lastScrollTop - topOffset) <= this.delta) {
return;
}
let hySiteHeader = this.el.shadowRoot.querySelector('.hy-site-header') as HTMLElement;
// If current position > last position AND scrolled past navbar...
if (topOffset > this.lastScrollTop && topOffset > this.navbarHeight) {
// Scroll Down
hySiteHeader.classList.remove('hy-site-header--sticky-visible');
hySiteHeader.classList.remove('has-toolbar', 'has-toolbar--large');
// Close Search panel
//CludoSayt.hide();
this.headerScrollDown.emit();
if (topOffset < this.el.offsetTop + this.navbarHeight) {
hySiteHeader.classList.remove('hy-site-header--sticky-active');
hySiteHeader.classList.remove('hy-site-header--sticky-visible');
hySiteHeader.classList.remove('hy-site-header--sticky-hidden');
hySiteHeader.classList.remove('has-toolbar', 'has-toolbar--large');
// Close desktop menu panel if it's open.
} else {
hySiteHeader.classList.add('hy-site-header--sticky-active');
hySiteHeader.classList.remove('hy-site-header--sticky-hidden');
hySiteHeader.classList.add('hy-site-header--sticky-visible');
if (bodyElementClasses.contains('toolbar-horizontal')) {
hySiteHeader.classList.add('has-toolbar');
if (bodyElementClasses.contains('toolbar-tray-open')) {
hySiteHeader.classList.add('has-toolbar--large');
}
}
this.lastScrollTop = topOffset;
}
componentDidUnload() {
this.ro.disconnect();
}
applySizeClasses(size: number) {
// Set the menu-type based on the width of the browser.
Ekaterina Kondareva
committed
if (size <= 1200 && size > 960) {
this.menuType = MenuType.tablet;
} else if (size <= 960) {
this.menuType = MenuType.mobile;
this.menuType = MenuType.desktop;
// Pass the menu type to menu component.
this.isMobile = this.menuType === (MenuType.mobile || MenuType.tablet);
const menuAttribute = this.menuType === MenuType.tablet ? MenuType.mobile : this.menuType;
this.el.children[0].setAttribute('menu-type', menuAttribute);
const logoSize = this.isMobile ? SiteLogoSize.small : SiteLogoSize.big;
let classAttributes = ['hy-site-header', 'hy-site-header--' + this.menuType];
switch (this.menuType) {
case MenuType.desktop:
<header class={classAttributes.join(' ')}>
Ekaterina Kondareva
committed
<div class={{'hy-backdrop': true, 'is-active': this.isMenuOpen}} />
{this.isGroup && (
<div class="hy-site-header__content-group">
<div class={'hy-site-header__logo-container'}>
<hy-site-logo size={logoSize} color={logoColor} url={this.logoUrl} label={this.logoLabel} />
</div>
<hy-menu-main-group
//className={'menu--secondary__item is-first'}
is-mobile={false}
data-main-menu={this.dataMainMenuLinks}
/>
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<div class={'menu--secondary'}>
<hy-menu-language
class={'menu--secondary__item is-first'}
is-mobile={false}
data-menu-language={this.dataMenuLanguage}
labels={this.languageLabels}
/>
<hy-site-search
class={'menu--secondary__item'}
size={14}
color={ColorVariant.black}
show-label={true}
labels={this.searchLabels}
search-labels={this.dataSiteSearchLabels}
search-tools={this.dataSearchTools}
/>
{this.donateLink.map((i) => {
return (
<a class={'menu--secondary__item hy-link__donate'} href={i.url}>
<hy-icon icon={'hy-icon-heart-support'} size={14} fill={ColorVariant.black} />
<span class={'hy-link__donate__label'}>{i.label}</span>
</a>
);
})}
</div>
</div>
)}
<div class="hy-site-header__content">
<div class={'hy-site-header__logo-container'}>
{this.isGroup ? (
<div>{this.logoLabel}</div>
) : (
<hy-site-logo size={logoSize} color={logoColor} url={this.logoUrl} label={this.logoLabel} />
)}
<hy-desktop-menu-links data-desktop-links={this.dataDesktopLinks}></hy-desktop-menu-links>
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
{!this.isGroup && (
<div class={'menu--secondary'}>
<hy-menu-language
class={'menu--secondary__item is-first'}
is-mobile={false}
data-menu-language={this.dataMenuLanguage}
labels={this.languageLabels}
/>
<hy-site-search
class={'menu--secondary__item'}
size={14}
color={ColorVariant.black}
show-label={true}
labels={this.searchLabels}
search-labels={this.dataSiteSearchLabels}
search-tools={this.dataSearchTools}
/>
{this.donateLink.map((i) => {
return (
<a class={'menu--secondary__item hy-link__donate'} href={i.url}>
<hy-icon icon={'hy-icon-heart-support'} size={14} fill={ColorVariant.black} />
<span class={'hy-link__donate__label'}>{i.label}</span>
</a>
);
})}
</div>
)}
case MenuType.tablet:
return (
<header class={classAttributes.join(' ')}>
<div class={{'hy-backdrop': true, 'is-active': this.isMenuOpen}} />
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<div class="hy-site-header__content">
<div class={'hy-site-header__logo-container'}>
<hy-site-logo size={logoSize} color={logoColor} url={this.logoUrl} label={this.logoLabel} />
</div>
<div class={'menu--secondary'}>
<hy-menu-language
class={'menu--secondary__item is-first'}
is-mobile={false}
data-menu-language={this.dataMenuLanguage}
labels={this.languageLabels}
/>
<hy-site-search
class={'menu--secondary__item'}
size={14}
color={ColorVariant.black}
show-label={true}
labels={this.searchLabels}
search-labels={this.dataSiteSearchLabels}
search-tools={this.dataSearchTools}
/>
{this.donateLink.map((i) => {
return (
<a class={'menu--secondary__item hy-link__donate'} href={i.url}>
<hy-icon icon={'hy-icon-heart-support'} size={14} fill={ColorVariant.black} />
<span class={'hy-link__donate__label'}>{i.label}</span>
</a>
);
})}
</div>
<div class={'hy-site-header__menu-container'}>
<span
class={{
'hy-site-header__menu-label': true,
'is-visible': this.isMenuOpen,
}}
>
{this.menuLabel}
</span>
<button
onClick={() => this.mobileMenuToggle()}
class={{
'hy-site-header__panel-toggle': true,
'is-open': this.isMenuOpen,
}}
aria-label={this.isMenuOpen ? this.menuLabels['close'] : this.menuLabels['open']}
>
{this.isMenuOpen ? (
<span class="hy-site-header__panel-toggle__label">
{this.menuLabelClose}
<hy-icon icon={'hy-icon-remove'} size={20} fill={ColorVariant.black} />
</span>
) : (
<span class="hy-site-header__panel-toggle__label">
{this.menuLabelOpen}
<hy-icon icon={'hy-icon-hamburger'} size={20} fill={ColorVariant.black} />
</span>
)}
</button>
<div
class={{
'is-open': this.isMenuOpen,
'hy-site-header__panel': true,
}}
>
<slot name={'menu'} />
</div>
</div>
</div>
</header>
);
case MenuType.mobile:
return (
<header class={classAttributes.join(' ')}>
<div class={{'hy-backdrop': true, 'is-active': this.isMenuOpen}} />
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<div class="hy-site-header__content">
<div class={'hy-site-header__logo-container'}>
<hy-site-logo size={logoSize} color={logoColor} url={this.logoUrl} label={this.logoLabel} />
</div>
<div class={'hy-site-header__menu-container'}>
<hy-site-search
class={'menu--secondary__item'}
size={14}
color={ColorVariant.black}
show-label={true}
labels={this.searchLabels}
search-labels={this.dataSiteSearchLabels}
search-tools={this.dataSearchTools}
/>
<button
onClick={() => this.mobileMenuToggle()}
class={{
'hy-site-header__panel-toggle': true,
'is-open': this.isMenuOpen,
}}
aria-label={this.isMenuOpen ? this.menuLabels['close'] : this.menuLabels['open']}
>
{this.isMenuOpen ? (
<span class="hy-site-header__panel-toggle__label">
{this.menuLabelClose}
<hy-icon icon={'hy-icon-remove'} size={20} fill={ColorVariant.black} />
</span>
) : (
<span class="hy-site-header__panel-toggle__label">
{this.menuLabelOpen}
<hy-icon icon={'hy-icon-hamburger'} size={20} fill={ColorVariant.black} />
</span>
)}
</button>
<div
class={{
'is-open': this.isMenuOpen,
'hy-site-header__panel': true,
}}
>
<slot name={'menu'} />
</div>
</div>
</div>
</header>
);
}