All files / src/layout Resizer.ts

100% Statements 122/122
100% Branches 6/6
100% Functions 5/5
100% Lines 122/122

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 12392x 93x 92x 92x 92x 92x 92x 92x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 92x 13x 1x 1x 1x 13x 13x 13x 13x 13x 13x 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  
import { PropertyValueMap, css, html } from 'lit';
import { getClasses } from '../utils';
import { property } from 'lit/decorators.js';
import { CustomEventType } from '../interfaces';
import { ResizeElement } from '../ResizeElement';
 
export class Resizer extends ResizeElement {
  static styles = css`
    :host {
      display: block;
      position: relative;
      width: var(--box-width, 200px);
      --resizer-handle-size: 15px;
    }
 
    .resizer {
      position: absolute;
      right: calc(var(--resizer-handle-size) * -1);
      height: 100%;
      cursor: col-resize;
      padding: 0 calc(var(--resizer-handle-size) / 2);
      z-index: 1;
    }
 
    .resizer-handle {
      position: relative;
      width: 4px;
      background: rgba(0, 0, 0, 0);
      height: 100%;
    }
 
    .resizer:hover .resizer-handle {
      background: rgba(0, 0, 0, 0.05);
      width: 3px;
      margin-right: -1px;
    }
 
    .resizing .resizer-handle {
      background: rgba(0, 0, 0, 0.1) !important;
      width: 3px;
      margin-right: -1px;
    }
 
    slot {
      margin-right: var(--resizer-handle-size);
      background: red;
    }
  `;
 
  initialX: number;
  boxWidth: number;
 
  @property({ type: Number })
  minWidth = 200;
 
  @property({ type: Number })
  maxWidth = 2000;
 
  @property({ type: Boolean })
  resizing = false;
 
  @property({ type: Number })
  currentWidth: number;
 
  constructor() {
    super();
    this.startResize = this.startResize.bind(this);
    this.resize = this.resize.bind(this);
    this.stopResize = this.stopResize.bind(this);
  }
 
  protected updated(
    _changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(_changedProperties);
    if (_changedProperties.has('currentWidth')) {
      this.style.setProperty('--box-width', `${this.currentWidth}px`);
    }
  }
 
  public setWidth(width: number) {
    const newWidth = Math.min(Math.max(width, this.minWidth), this.maxWidth);
    this.currentWidth = newWidth;
  }
 
  startResize(e: MouseEvent) {
    this.initialX = e.x;
    this.boxWidth = this.offsetWidth;
    document.body.style.userSelect = 'none';
    this.resizing = true;
    window.addEventListener('mousemove', this.resize);
    window.addEventListener('mouseup', this.stopResize);
    this.requestUpdate();
  }
 
  resize(event: MouseEvent) {
    const dx = event.x - this.initialX;
    this.setWidth(this.boxWidth + dx);
  }
 
  stopResize() {
    document.body.style.userSelect = 'initial';
    window.removeEventListener('mousemove', this.resize);
    window.removeEventListener('mouseup', this.stopResize);
    this.requestUpdate();
    this.resizing = false;
 
    this.fireCustomEvent(CustomEventType.Resized, { width: this.currentWidth });
  }
 
  render() {
    return html`
      <div
        class=${getClasses({ resizer: true, resizing: this.resizing })}
        @mousedown="${this.startResize}"
      >
        <div class=${getClasses({ 'resizer-handle': true })}></div>
      </div>
      <slot></slot>
    `;
  }
}