All files / src/display Dropdown.ts

100% Statements 303/303
96.29% Branches 26/27
100% Functions 9/9
100% Lines 303/303

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 30492x 96x 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 90x 90x 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 12x 12x 12x 12x 12x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 92x 13x 13x 13x 13x 13x 1x 1x 1x 1x 12x 13x 92x 92x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 92x 12x 12x 12x 12x 12x 12x 12x 12x 12x 76x 76x 76x 76x 13x 13x 13x 92x 92x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x 1x 16x 16x 16x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x 1x 1x 1x 16x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 18x 92x 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  
import { css, html, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { RapidElement } from '../RapidElement';
import { getClasses } from '../utils';
import { CustomEventType } from '../interfaces';
 
import { styleMap } from 'lit-html/directives/style-map.js';
 
export class Dropdown extends RapidElement {
  static get styles() {
    return css`
      :host {
      }
 
      .wrapper {
        position: relative;
      }
 
      .toggle {
        cursor: pointer;
      }
 
      .dropdown-wrapper {
        position: relative;
        overflow: auto;
      }
 
      .dropdown:focus {
      }
 
      .dropdown.dormant {
        height: 0;
        overflow: hidden;
      }
 
      .dropdown {
        position: fixed;
        z-index: 2;
        padding: 0;
        opacity: 0;
        border-radius: calc(var(--curvature) * 1.5);
        background: #fff;
        transition:
          opacity calc(0.8 * var(--transition-speed)) var(--bounce),
          transform calc(0.8 * var(--transition-speed)) var(--bounce);
        user-select: none;
        margin-top: 0px;
        margin-left: 0px;
        transform: translateY(0) scale(1);
        box-shadow: var(--dropdown-shadow);
      }
 
      .dropdown:focus {
        outline: none;
      }
 
      .arrow {
        content: '';
        width: 0px;
        height: 0;
        z-index: 10;
        position: absolute;
        border-left: 6px solid transparent;
        border-right: 6px solid transparent;
        border-bottom: 6px solid white;
      }
 
      .open .dropdown {
        opacity: 1;
        transform: translateY(0.5em) scale(1);
      }
 
      .mask {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        opacity: 0.5;
        transition: opacity var(--transition-speed) ease-in-out;
        pointer-events: none;
        z-index: 1;
      }
 
      .mask.open {
        opacity: 1;
        pointer-events: auto;
      }
 
      .right {
        right: 0;
      }
    `;
  }
 
  @property({ type: Boolean })
  open = false;
 
  @property({ type: Boolean })
  dormant = true;
 
  @property({ type: Number })
  arrowSize = 8;
 
  @property({ type: Number })
  margin = 10;
 
  @property({ type: Boolean })
  mask = false;
 
  dropdownStyle: Record<string, string> = {};
 
  arrowStyle: Record<string, string> = {};
 
  constructor() {
    super();
    this.calculatePosition = this.calculatePosition.bind(this);
  }
 
  private activeFocus: any;
  private blurHandler: any;
 
  private resetBlurHandler() {
    const dropdown = this.shadowRoot.querySelector(
      '.dropdown'
    ) as HTMLDivElement;
 
    if (this.activeFocus) {
      this.activeFocus.removeEventListener('blur', this.blurHandler);
    }
 
    this.activeFocus = dropdown;
    this.blurHandler = this.handleBlur.bind(this);
    this.activeFocus.addEventListener('blur', this.blurHandler);
  }
 
  private handleBlur(event: FocusEvent) {
    const newTarget = event.relatedTarget as any;
 
    if (this.contains(newTarget)) {
      newTarget.addEventListener('blur', this.blurHandler);
      this.activeFocus = newTarget;
    } else {
      this.closeDropdown();
    }
  }
 
  private openDropdown() {
    // reset position so calculatePosition() sees the natural bounds
    this.dropdownStyle = {};
    this.arrowStyle = {};
 
    this.open = true;
    this.dormant = false;
    this.resetBlurHandler();
 
    const dropdown = this.shadowRoot.querySelector(
      '.dropdown'
    ) as HTMLDivElement;
    dropdown.focus();
    dropdown.click();
 
    this.fireCustomEvent(CustomEventType.Opened);
  }
 
  private closeDropdown() {
    this.activeFocus.removeEventListener('blur', this.blurHandler);
    this.open = false;
 
    window.setTimeout(() => {
      this.dormant = true;
    }, 250);
 
    this.blur();
  }
 
  public updated(changedProperties: Map<string, any>) {
    super.updated(changedProperties);
 
    if (changedProperties.has('open') && this.open) {
      // defer to avoid scheduling an update during the current cycle;
      // the dropdown transitions from opacity 0 so this is not visible
      setTimeout(() => this.calculatePosition(), 0);
    }
  }
 
  public calculatePosition() {
    const dropdown = this.shadowRoot.querySelector(
      '.dropdown'
    ) as HTMLDivElement;
    const toggle = this.querySelector('*[slot="toggle"]');
 
    const arrow = this.shadowRoot.querySelector('.arrow') as HTMLDivElement;
 
    let bumpedUp = false;
    let bumpedLeft = false;
 
    if (dropdown && toggle) {
      const dropdownBounds = dropdown.getBoundingClientRect();
      const toggleBounds = toggle.getBoundingClientRect();
      const arrowBounds = arrow.getBoundingClientRect();
 
      if (!toggle) {
        return;
      }
 
      const dropdownStyle = {
        border: '1px solid rgba(0,0,0,0.1)',
        marginTop: '0.5em'
      };
 
      // if off the the right, bump it left
      if (dropdownBounds.right > window.innerWidth) {
        dropdownStyle['left'] =
          toggleBounds.right - dropdownBounds.width + 'px';
        delete dropdownStyle['right'];
        bumpedLeft = true;
      }
 
      // if off to the bottom, bump it up
      if (dropdownBounds.bottom > window.innerHeight) {
        dropdownStyle['top'] = toggleBounds.top - dropdownBounds.height + 'px';
        dropdownStyle['marginTop'] = '-0.5em';
        bumpedUp = true;
      }
 
      // if our arrow is aligned with the left of the dropdown, scootch
      // the dropdown left a pinch so our arrow still overlaps properly
      let arrowLeft = toggleBounds.width / 2 - arrowBounds.width / 2;
      if (arrowLeft <= 0) {
        dropdownStyle['marginLeft'] = '-10px';
        arrowLeft = 10;
      }
 
      const arrowStyle = {
        left: arrowLeft + 'px',
        borderWidth: this.arrowSize + 'px',
        top: '-' + this.arrowSize + 'px'
      };
 
      if (bumpedUp) {
        // rotate our arrow 180 degrees
        arrowStyle['transform'] = 'rotate(180deg)';
 
        // and place it at the bottom of the dropdown
        arrowStyle['top'] = 'auto';
        arrowStyle['bottom'] = '-' + this.arrowSize + 'px';
      }
 
      if (bumpedLeft) {
        arrowStyle['right'] =
          toggleBounds.width / 2 - arrowBounds.width / 2 + 'px';
        delete arrowStyle['left'];
      }
 
      this.arrowStyle = arrowStyle;
      this.dropdownStyle = dropdownStyle;
    }
    this.requestUpdate();
  }
 
  public handleToggleClicked(event: MouseEvent): void {
    event.preventDefault();
    event.stopPropagation();
    if (!this.open && this.dormant) {
      this.openDropdown();
    }
  }
 
  public render(): TemplateResult {
    return html`
      ${this.mask
        ? html`<div class="mask  ${this.open ? 'open' : ''}" />`
        : null}
 
      <div
        class="wrapper ${getClasses({
          open: this.open
        })}"
      >
        <slot
          name="toggle"
          class="toggle"
          @click=${this.handleToggleClicked}
        ></slot>
        <div
          class="${getClasses({
            dropdown: true,
            dormant: this.dormant
          })}"
          style=${styleMap(this.dropdownStyle)}
          tabindex="0"
        >
          <div class="arrow" style=${styleMap(this.arrowStyle)}></div>
          <div class="dropdown-wrapper">
            <slot name="dropdown" tabindex="1"></slot>
          </div>
        </div>
      </div>
    `;
  }
}