Skip to content
Snippets Groups Projects
hy-general-list-item.tsx 2.71 KiB
Newer Older
import {Component, Element, h, Prop} from '@stencil/core';
druid's avatar
druid committed

@Component({
  tag: 'hy-general-list-item',
  styleUrl: 'hy-general-list-item.scss',
  shadow: true,
})
export class HyGeneralListItem {
Ekaterina Kondareva's avatar
Ekaterina Kondareva committed
  @Prop() hasSidebar: boolean = true;
druid's avatar
druid committed
  @Prop() description?: string;
  @Prop() imageAlt: string = '';
  @Prop() imageUrl: string = null;
  @Prop() disableLazy: boolean = false;
  @Prop() itemTitle?: string;
druid's avatar
druid committed
  @Prop() label?: string;
Tuukka Turu's avatar
Tuukka Turu committed
  @Prop() subLabel: string = '';
  @Prop() date: string = '';
  @Prop() tag: string = '';
druid's avatar
druid committed
  @Prop() type?: string;
  @Prop() url?: string;
Tuukka Turu's avatar
Tuukka Turu committed
  @Prop() listStyle?: string;
  @Prop() target: boolean = true;
druid's avatar
druid committed

  @Element() el: HTMLElement;

druid's avatar
druid committed
  render() {
Tuukka Turu's avatar
Tuukka Turu committed
    const classAttributes = ['hy-general-list-item', `hy-general-list-item--style-${this.listStyle}`].join(' ');
druid's avatar
druid committed
    const contentClassAttributes = ['hy-general-list-item__text-container'].join(' ');
    const imageClassAttributes = ['hy-general-list-item__image-container'].join(' ');

Ekaterina Kondareva's avatar
Ekaterina Kondareva committed
    const sidebarClass = this.hasSidebar ? 'common' : 'large';
    const titleClassAttributes = ['hy-general-list-item__title', `hy-general-list-item__title__${sidebarClass}`].join(
      ' '
    );
    const descriptionClassAttributes = [
      'hy-general-list-item__description',
      `hy-general-list-item__description__${sidebarClass}`,
    ].join(' ');

    const aspectRatioWidth = 16;
    const aspectRatioHeight = 10;
druid's avatar
druid committed
    const aspect = (aspectRatioHeight / aspectRatioWidth) * 100;
    const aspectRatio = {
      '--aspectRatio': `${aspect}%` as 'aspectRatio',
    };

    return (
      <article class={classAttributes}>
Tuukka Turu's avatar
Tuukka Turu committed
        <a class="hy-general-list-item__link" href={this.url} target={this.target ? '_blank' : '_self'}>
          {this.type && (
            <div class="hy-general-list-item__type">
Ekaterina Kondareva's avatar
Ekaterina Kondareva committed
              <span class="hy-general-list-item__type__major">{this.label}</span>
Tuukka Turu's avatar
Tuukka Turu committed
              <span class="hy-general-list-item__type__sub">{this.subLabel}</span>
            </div>
          )}
          <figure class={imageClassAttributes} style={aspectRatio}>
            <img
              loading={this.disableLazy ? 'eager' : 'lazy'}
              alt={this.imageAlt}
              class="hy-general-list-item__image"
              src={this.imageUrl}
            />
          </figure>
          <div class={contentClassAttributes}>
Tuukka Turu's avatar
Tuukka Turu committed
            {(this.tag || this.date) && (
              <span class="hy-genenral-list-item__meta">
                {this.tag}
                <span class="hy-genenral-list-item__meta__date">| {this.date}</span>
              </span>
            )}
Ekaterina Kondareva's avatar
Ekaterina Kondareva committed
            <h3 class={titleClassAttributes}>{this.itemTitle}</h3>
            <p class={descriptionClassAttributes}>{this.description}</p>
          </div>
        </a>
      </article>
    );
druid's avatar
druid committed
  }
}