All files / src/display Tip.ts

91.47% Statements 311/340
68.75% Branches 22/32
90% Functions 9/10
91.47% Lines 311/340

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 268 269 270 271 272 273 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 301 302 303 304 305 306 307 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 34192x 120x 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 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 1582x 1582x 1582x 1582x 1582x 1582x 92x 1582x 4x 92x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 92x 12x 92x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x     4x                                     92x                   92x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 92x 92x 92x 92x 64x 92x 92x 92x 92x 92x 92x 92x 92x 92x 64x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x  
import { css, html, PropertyValues, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { styleMap } from 'lit-html/directives/style-map.js';
import { RapidElement } from '../RapidElement';
import { getClasses, getMiddle, getCenter } from '../utils';
 
export class Tip extends RapidElement {
  static get styles() {
    return css`
      .tip {
        transition: opacity 120ms cubic-bezier(0.2, 0, 0, 1);
        margin: 0px;
        position: fixed;
        opacity: 0;
        background: #fff;
        padding: 4px 8px;
        pointer-events: none;
        border-radius: var(--curvature-widget);
        box-shadow:
          0 1px 10px 10px rgba(0, 0, 0, 0.035),
          0 1px 3px 0px rgba(0, 0, 0, 0.1),
          0 1px 2px 0 rgba(0, 0, 0, 0.06);
        font-size: 14px;
        z-index: 2147483647;
        color: #333;
        white-space: nowrap;
      }
 
      .tip kbd {
        display: inline;
        padding: 0;
        border: none;
        border-radius: 0;
        background: transparent;
        color: #8b98ab;
        font-size: 11px;
        font-weight: 500;
        line-height: 1;
        letter-spacing: 0.01em;
        font-family: inherit;
      }
 
      .tip.hide-on-change {
        transition: none;
      }
 
      .show {
        opacity: 1;
        z-index: 2147483647;
      }
 
      .slot {
        display: flex;
        flex-direction: column;
      }
 
      .arrow {
        position: absolute;
        color: #fff;
        font-size: 10px;
        line-height: 0px;
      }
 
      .◀ {
        text-shadow: -1px 2px 2px rgba(0, 0, 0, 0.1);
      }
 
      .▶ {
        text-shadow: 1px 2px 2px rgba(0, 0, 0, 0.1);
      }
 
      .▼ {
        text-shadow: 0px 3px 3px rgba(0, 0, 0, 0.1);
      }
 
      .▲ {
        text-shadow: 0px -1px 1px rgba(0, 0, 0, 0.1);
      }
    `;
  }
 
  @property({ type: String })
  text: string;
 
  @property({ attribute: false })
  content: TemplateResult | string | null = null;
 
  @property({ type: Boolean })
  visible = false;
 
  @property({ type: String })
  position = 'auto';
 
  @property({ type: Boolean })
  hideOnChange: boolean;
 
  top: number;
  left: number;
 
  @property({ type: Number, attribute: false })
  width: number;
 
  @property({ type: Boolean, attribute: false })
  poppedTop: boolean;
 
  @property({ type: Number })
  delay = 350;
 
  @property({ type: Number })
  hideDelay = 40;
 
  arrow: string;
  arrowTop: number;
  arrowLeft: number;
  arrowDirection: string;
 
  showTimer = 0;
  hideTimer = 0;
 
  public willUpdate(changed: PropertyValues): void {
    super.willUpdate(changed);
    if (changed.has('text') && this.hideOnChange) {
      this.visible = false;
    }
  }
 
  public updated(changed: Map<string, any>) {
    if ((changed.has('visible') || changed.has('text')) && this.visible) {
      this.calculatePosition();
    }
  }
 
  private calculatePosition() {
    if (this.visible) {
      const tipEl = this.getDiv('.tip') as HTMLElement;
      const arrowEl = this.getDiv('.arrow') as HTMLElement;
      const tipBounds = tipEl.getBoundingClientRect();
      const anchorBounds = this.getDiv('.slot').getBoundingClientRect();
 
      const viewportPadding = 8;
      const side = this.chooseSide(
        this.position,
        anchorBounds,
        tipBounds,
        viewportPadding
      );
 
      this.arrowLeft = 0;
      this.arrowTop = 0;
 
      if (side === 'left') {
        this.left = anchorBounds.left - tipBounds.width - 10;
        this.top = getMiddle(anchorBounds, tipBounds);
 
        // position our arrow
        this.arrowTop = tipBounds.height / 2;
        this.arrowLeft = tipBounds.width + 2;
        this.arrow = '▶';
      } else if (side === 'right') {
        this.left = anchorBounds.right + 12;
        this.top = getMiddle(anchorBounds, tipBounds);
 
        this.arrowTop = tipBounds.height / 2;
        this.arrowLeft = -8;
        this.arrow = '◀';
      } else if (side === 'top') {
        this.top = anchorBounds.top - tipBounds.height - 12;
        this.left = getCenter(anchorBounds, tipBounds);
 
        this.arrowTop = tipBounds.height + 2;
        this.arrowLeft = tipBounds.width / 2 - 4;
        this.arrow = '▼';
      } else if (side === 'bottom') {
        this.top = anchorBounds.bottom + 10;
        this.left = getCenter(anchorBounds, tipBounds);
 
        this.arrowTop = -2;
        this.arrowLeft = tipBounds.width / 2 - 3;
        this.arrow = '▲';
      }
 
      const minLeft = viewportPadding;
      const maxLeft = window.innerWidth - tipBounds.width - viewportPadding;
      const minTop = viewportPadding;
      const maxTop = window.innerHeight - tipBounds.height - viewportPadding;
 
      const unclampedLeft = this.left;
      const unclampedTop = this.top;
      this.left = this.clamp(this.left, minLeft, maxLeft);
      this.top = this.clamp(this.top, minTop, maxTop);
 
      const leftDelta = this.left - unclampedLeft;
      const topDelta = this.top - unclampedTop;
 
      if (side === 'top' || side === 'bottom') {
        this.arrowLeft -= leftDelta;
        this.arrowLeft = this.clamp(this.arrowLeft, 8, tipBounds.width - 14);
      } else {
        this.arrowTop -= topDelta;
        this.arrowTop = this.clamp(this.arrowTop, 8, tipBounds.height - 8);
      }
 
      // round to avoid sub-pixel rendering differences
      this.top = Math.round(this.top);
      this.left = Math.round(this.left);
      this.arrowTop = Math.round(this.arrowTop);
      this.arrowLeft = Math.round(this.arrowLeft);
 
      // directly update DOM to avoid scheduling another update
      tipEl.style.top = `${this.top}px`;
      tipEl.style.left = `${this.left}px`;
      arrowEl.style.top = `${this.arrowTop}px`;
      arrowEl.style.left = `${this.arrowLeft}px`;
      arrowEl.textContent = this.arrow;
      arrowEl.className = `arrow ${this.arrow}`;
    }
  }
 
  private clamp(value: number, min: number, max: number): number {
    if (max < min) {
      return min;
    }
    return Math.min(Math.max(value, min), max);
  }
 
  private chooseSide(
    preferred: string,
    anchorBounds: DOMRect,
    tipBounds: DOMRect,
    viewportPadding: number
  ): string {
    const gap = 12;
    const spaces = {
      left: anchorBounds.left - viewportPadding,
      right: window.innerWidth - anchorBounds.right - viewportPadding,
      top: anchorBounds.top - viewportPadding,
      bottom: window.innerHeight - anchorBounds.bottom - viewportPadding
    };
 
    const required = {
      left: tipBounds.width + gap,
      right: tipBounds.width + gap,
      top: tipBounds.height + gap,
      bottom: tipBounds.height + gap
    };
 
    const fits = (side: string) => spaces[side] >= required[side];
    const opposite: Record<string, string> = {
      left: 'right',
      right: 'left',
      top: 'bottom',
      bottom: 'top'
    };
 
    if (preferred !== 'auto') {
      if (fits(preferred)) {
        return preferred;
      }
      const fallback = opposite[preferred];
      if (fallback && fits(fallback)) {
        return fallback;
      }
      return preferred;
    }
 
    const order = ['bottom', 'right', 'left', 'top'];
    let bestSide = order[0];
    let bestSpace = -1;
 
    for (const side of order) {
      if (fits(side)) {
        return side;
      }
      if (spaces[side] > bestSpace) {
        bestSpace = spaces[side];
        bestSide = side;
      }
    }

    return bestSide;
  }

  private handleMouseEnter() {
    window.clearTimeout(this.hideTimer);
    window.clearTimeout(this.showTimer);
    this.showTimer = window.setTimeout(() => {
      this.visible = true;
    }, this.delay);
  }

  private handleMouseLeave() {
    window.clearTimeout(this.showTimer);
    window.clearTimeout(this.hideTimer);
    if (this.hideDelay > 0) {
      this.hideTimer = window.setTimeout(() => {
        this.visible = false;
      }, this.hideDelay);
      return;
    }
    this.visible = false;
  }

  public render(): TemplateResult {
    const tipStyle: any = {
      top: this.top ? `${this.top}px` : '0px',
      left: this.left ? `${this.left}px` : '0px'
    };
 
    const arrowStyle: any = {
      top: this.arrowTop ? `${this.arrowTop}px` : '0px',
      left: this.arrowLeft ? `${this.arrowLeft}px` : '0px'
    };
 
    if (this.width) {
      tipStyle.width = `${this.width}px`;
    }
 
    const classes = getClasses({
      tip: true,
      show: this.visible,
      top: this.poppedTop,
      'hide-on-change': this.hideOnChange
    });
 
    return html`
      <div
        class="slot"
        @click=${this.handleMouseLeave}
        @mouseenter=${this.handleMouseEnter}
        @mouseleave=${this.handleMouseLeave}
      >
        <slot></slot>
      </div>
      <div class="${classes}" style=${styleMap(tipStyle)}>
        ${this.content ?? this.text}
        <div class="arrow" style=${styleMap(arrowStyle)}></div>
      </div>
    `;
  }
}