All files / src/display Icon.ts

100% Statements 267/267
74.28% Branches 26/35
100% Functions 6/6
100% Lines 267/267

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 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 263 264 265 266 267 26892x 149x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 91x 91x 91x 91x 91x 91x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 3477x 3477x 3477x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 3593x 3593x 3462x 3462x 3462x 3462x 3593x 125x 125x 125x 125x 125x 125x 125x 5x 5x 5x 5x 5x 5x 3593x 3593x 3593x 18x 18x 92x 3x 3x 3x 3593x 3593x 3593x 3593x 18x 17x 17x 17x 17x 6x 17x 11x 17x 17x 18x 18x 18x 18x 3593x 3593x 3593x 3593x 3593x 131x 131x 131x 131x 6x 6x 6x 6x 6x 131x 92x 92x 92x 92x 91x 91x 92x 92x 34x 91x 91x 34x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x  
import { LitElement, TemplateResult, html, css, PropertyValues } from 'lit';
import { property } from 'lit/decorators.js';
import { Icon, SVG_FINGERPRINT } from '../Icons';
import { getClasses } from '../utils';
 
export class VectorIcon extends LitElement {
  @property({ type: String })
  name: string;
 
  @property({ type: String })
  prefix: string;
 
  // same as name but without implicit coloring
  @property({ type: String })
  id: string;
 
  @property({ type: Number })
  size = 1;
 
  @property({ type: Boolean })
  spin: boolean;
 
  @property({ type: Boolean })
  clickable: boolean;
 
  @property({ type: Boolean })
  circled: boolean;
 
  @property({ type: String })
  animateChange: string;
 
  @property({ type: String })
  animateClick: string;
 
  @property({ type: Number })
  animationDuration = 200;
 
  @property({ type: String })
  src = '';
 
  @property({ type: Number, attribute: false })
  steps = 2;
 
  @property({ type: Number, attribute: false })
  animationStep: number;
 
  @property({ type: String })
  easing = 'cubic-bezier(0.68, -0.55, 0.265, 1.55)';
 
  static get styles() {
    return css`
      :host {
        align-items: center;
        align-self: center;
      }
 
      .sheet {
        color: var(--icon-color);
        transform: scale(1);
        transition:
          fill 100ms ease-in-out,
          background 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55),
          padding 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55),
          margin 200ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
      }
 
      .sheet.spin {
        transform: rotate(0deg);
      }
 
      .sheet.spin-1 {
        transform: rotate(180deg);
      }
 
      .sheet.spin-2 {
        transform: rotate(360deg);
      }
 
      .sheet.spin-3 {
        transform: rotate(0deg);
        transition-duration: 0ms !important;
      }
 
      .sheet.pulse {
        transform: scale(1);
      }
 
      .sheet.pulse-1 {
        transform: scale(1.2);
      }
 
      .clickable:hover {
        cursor: pointer;
        fill: var(--color-link-primary) !important;
        background: rgb(255, 255, 255);
      }
 
      .circled {
        background: var(--icon-color-circle);
        padding: 0.15em;
        margin: -0.15em;
        box-shadow: var(--shadow);
      }
 
      .wrapper {
        display: flex;
        flex-direction: column;
        border-radius: 999px;
        transition:
          background 200ms linear,
          transform 300ms cubic-bezier(0.68, -0.55, 0.265, 1.55),
          padding 150ms linear,
          margin 150ms linear;
      }
 
      .wrapper.clickable {
        transform: scale(1);
      }
 
      .wrapper.clickable:hover {
        --icon-circle-size: 0.35em;
        --icon-background: var(--icon-color-circle-hover);
      }
 
      .wrapper.clickable {
        padding: var(--icon-circle-size);
        margin: calc(-1 * var(--icon-circle-size));
        background: var(--icon-background);
      }
 
      .spin-forever {
        animation-name: spin;
        animation-duration: var(--test-animation-duration, 2000ms);
        animation-iteration-count: infinite;
        animation-timing-function: linear;
        animation-play-state: var(--test-animation-play-state, running);
      }
 
      @keyframes spin {
        from {
          transform: rotate(0deg);
        }
        to {
          transform: rotate(360deg);
        }
      }
    `;
  }
 
  constructor() {
    super();
  }
 
  private lastName: string;
 
  public willUpdate(changes: PropertyValues): void {
    super.willUpdate(changes);
    if (!this.hasUpdated && changes.has('animateChange')) {
      // set our default duration if we need one
      if (!changes.has('animationDuration')) {
        this.animationDuration = this.steps * this.animationDuration;
      }
 
      if (this.animateChange === 'spin') {
        this.steps = 3;
        this.animationDuration = 400;
        this.easing = 'linear';
      }
    }
 
    // clear lastName at the animation halfway point so render uses the new name
    if (changes.has('animationStep')) {
      if (this.lastName && this.animationStep >= this.steps / 2) {
        this.lastName = null;
      }
    }
  }
 
  public handleClicked() {
    if (this.animateClick) {
      this.animationStep = 1;
    }
  }
 
  public updated(changes: Map<string, any>) {
    super.updated(changes);
 
    if (changes.has('animationStep')) {
      setTimeout(() => {
        if (this.animationStep > 0 && this.animationStep < this.steps) {
          this.animationStep++;
        } else {
          this.animationStep = 0;
        }
      }, this.animationDuration / this.steps);
    }
 
    if (changes.has('name') && this.animateChange) {
      this.lastName = changes.get('name');
 
      // our name changed, defer animation start to avoid scheduling
      // an update during the current update cycle
      if (this.lastName && this.animateChange) {
        setTimeout(() => {
          this.animationStep = 1;
        }, 0);
      }
    }
  }
 
  public render(): TemplateResult {
    if (!this.name) {
      return null;
    }
 
    // let icon name mappings take precedence
    let name = this.lastName || this.name;
 
    // special case our channel icon fallback
    if (name.startsWith('channel_') && !Icon[name]) {
      name = Icon.channel_ex;
    } else {
      name = Icon[name.replace('icon.', '')] || name;
    }
 
    // referencing icons by id is explicit
    if (!name) {
      name = this.id;
    }
 
    return html`
      <div
        @click=${this.handleClicked}
        class="wrapper ${getClasses({
          clickable: this.clickable,
          circled: this.circled,
          animate: !!this.animateChange || !!this.animateClick,
          'spin-forever': this.spin
        })}"
      >
        <svg
          style="height:${this.size}em;width:${this
            .size}em;transition:transform ${this.animationDuration /
          this.steps}ms
          ${this.easing}"
          class="${getClasses({
            sheet: this.src === '',
            [this.animateChange]: !!this.animateChange,
            [this.animateChange + '-' + this.animationStep]:
              this.animationStep > 0,
            [this.animateClick]: !!this.animateClick,
            [this.animateClick + '-' + this.animationStep]:
              this.animationStep > 0
          })}"
        >
          <use
            href="${this.src
              ? this.src
              : `${
                  this.prefix || (window as any).static_url || '/static/'
                }svg/index.svg?v=${SVG_FINGERPRINT}#${name}`}"
          />
        </svg>
      </div>
    `;
  }
}