All files / src/display Options.ts

94.91% Statements 822/866
86.36% Branches 133/154
83.33% Functions 20/24
94.91% Lines 822/866

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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867125x 194x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 186x 186x 186x 125x 125x 125x 125x 125x 125x 81x 81x 81x 81x 81x 81x 81x 81x 1086x 188x 188x 188x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 1206x 1109x 1109x 1109x 1109x 1109x 1109x 1109x 1109x 1109x 1109x 1109x 1109x 31x 31x 31x 1109x 1206x 1206x 1206x 1206x 1206x 8x 125x 1206x 1109x 31x 1206x 1206x 650x 650x 650x 1206x 1206x 125x     125x 2707x 2707x 2707x 2707x 2707x 177x 22x 22x 19x 22x 2707x 2707x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 759x 759x 1603x 889x 889x 889x 889x 875x 777x 98x 98x 98x 98x 875x 2707x 2707x 2707x 1104x 1104x 2707x 916x 916x 916x 916x 916x 916x 2707x 2707x 2707x 67x 67x 67x 125x 2707x 2707x 2707x 1085x 2707x 2707x 2707x 2707x 758x 758x 758x 741x 741x 741x 741x 686x 686x 686x 686x 758x 758x 758x 758x 2707x 2707x 1623x 1623x 1623x 2707x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1603x 1515x 1515x 1603x 1484x 1603x 164x 150x 150x 164x 164x 1603x 1603x 1603x 398x 385x 385x 385x 125x 46x 46x 46x 46x 46x 46x 46x 46x 46x 1x 1x 1x 10x 10x 10x 10x 10x 46x 2x 2x 2x 2x 36x 36x 125x 125x 46x 2x 2x 2x 2x 2x 2x 2x 2x 2x 46x                 46x 125x 10x 10x 10x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 125x 90x 90x 90x 90x 90x 90x 90x 90x 90x 19x 19x 19x 19x 19x 19x 125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x   125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x 20026x 20026x 20026x 20026x 20026x 20026x 20026x 11x 11x 11x   11x 11x 11x 10x 10x 20026x 10x 10x 1x 1x 1x 1x 1x 1x       10x 9x 9x           9x 9x 9x 3x 3x 3x 3x 3x 3x 9x 6x     125x                               125x 2289x 2289x 969x 969x 969x 969x 969x 969x 969x 969x 969x 969x 2289x 2289x 2289x 2289x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x             60x 60x 60x 60x 125x 1206x 1206x 1206x 1206x 1206x 1206x 1206x 1206x 1206x 1206x 1206x 125x 15x 15x   15x 15x 15x 7x 7x 7x 7x 7x 7x 7x 7x 15x 125x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 125x 125x 1286x 1581x 125x 80x 1581x 207x 260x 260x 260x 260x 260x 260x 125x 125x 125x 275x 124x 1581x 124x 275x 275x 275x 124x 1178x 124x 124x 1581x 1581x 1581x 1581x 1473x 125x 125x 125x 125x 125x 125x 125x 128x 108x 108x 124x 124x 124x 124x 124x 124x 124x 124x 124x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x  
import { TemplateResult, html, css, PropertyValues } from 'lit';
import { property } from 'lit/decorators.js';
import { CustomEventType } from '../interfaces';
import { RapidElement, EventHandler } from '../RapidElement';
import { styleMap } from 'lit-html/directives/style-map.js';
import { getClasses, getScrollParent, throttle } from '../utils';
import { msg } from '@lit/localize';
import { designTokens } from '../styles/designTokens';
 
export class Options extends RapidElement {
  static get styles() {
    return css`
      ${designTokens}
 
      :host {
        --transition-speed: 0;
      }
 
      .options-container {
        background: var(--color-options-bg);
        user-select: none;
        border-radius: var(--curvature-widget);
        overflow: hidden;
        display: flex;
        flex-direction: column;
        transition:
          transform var(--transition-speed) cubic-bezier(0.71, 0.18, 0.61, 1.33),
          opacity var(--transition-speed) cubic-bezier(0.71, 0.18, 0.61, 1.33);
        opacity: 0;
        z-index: 1000;
        pointer-events: none;
        position: relative;
      }
 
      .shadow {
        box-shadow: var(--options-shadow);
      }
 
      .anchored {
        position: fixed;
      }
 
      :host([block]) .options-container {
        flex-grow: 1;
        height: 100%;
        border: none;
        /* Block-mode is inline (ticket sidebar, etc.) — drop the
           z-index so floating popups (selects, dropdowns) opened over
           the block list always stack above its options. */
        z-index: auto;
      }
 
      :host([block]) .options-scroll {
        height: 100%;
        visibility: visible;
        overflow-y: auto;
        flex-grow: 1;
        -webkit-mask-image: -webkit-radial-gradient(white, black);
      }
 
      :host([block]) {
        border-radius: var(--curvature);
        display: block;
        height: 100%;
      }
 
      :host([block]) .shadow {
        box-shadow: var(--options-block-shadow);
      }
 
      .bordered {
        border: 1px solid var(--color-widget-border) !important;
      }
 
      :host([block]) .options {
        margin-bottom: 1.5em;
      }
 
      .options-scroll {
        display: flex;
        flex-direction: column;
      }
 
      :host([collapsed]) temba-icon {
        flex-grow: 1;
        margin-right: 0px !important;
        padding-top: 0.25em;
        padding-bottom: 0.25em;
      }
 
      :host([collapsed]) .name {
        display: none;
      }
 
      :host([collapsed]) .count {
        display: none;
      }
 
      .options {
        border-radius: var(--curvature-widget);
        overflow-y: auto;
        max-height: 200px;
        border: none;
      }
 
      /* When shown, the popup is opaque and clickable. Keep the
         high z-index from .options-container so the popup stacks
         above neighboring widgets — e.g. embedded prefix labels of
         later form fields, which sit absolutely positioned at the
         top edge of their host element. */
      .show {
        opacity: 1;
        pointer-events: auto;
        margin-top: var(--options-margin-top);
      }
 
      /* Floating popup border uses --focus-muted (not --color-focus)
         so a parent field's error state — which overrides
         --color-focus to red via .has-error — doesn't turn the
         popup red. The popup stays blue regardless. Single source
         of truth: --focus in designTokens. No halo here — the
         dropdown is an attached panel, not its own focus indicator
         (the parent select keeps its own halo). Block-mode renders
         inline and skips this rule entirely. */
      :host(:not([block])) .show {
        border: 1px solid var(--temba-options-focus-border, var(--focus-muted));
      }
 
      /* Each option is a DS-style list row: flat, fixed height,
         tight padding, no inter-item margin. Full-bleed background on
         hover/focus → no border-radius (rounded corners would leave
         visible gaps at the dropdown edges). */
      .option {
        font-size: var(--temba-options-font-size, 13.5px);
        padding: var(--temba-options-option-padding, 0 var(--pad));
        border-radius: var(--temba-options-option-radius, 0);
        margin: var(--temba-options-option-margin, 0);
        min-height: var(--temba-options-option-min-height, 32px);
        display: flex;
        align-items: center;
        cursor: pointer;
        color: var(--text-1);
        scroll-margin: 5px 0px;
        text-align: left;
      }
 
      /* A single wrapping renderOption child stretches to fill the row
         so custom templates (e.g. ones using justify-content:
         space-between to right-align trailing badges) get the full
         width to lay out in. Scoped to :only-child so a renderOption
         that emits multiple top-level siblings keeps its natural
         layout instead of getting an equal-width flex partition. */
      .option > :only-child {
        flex: 1;
        min-width: 0;
      }
 
      /* Block-mode (inline, always-visible lists like the ticket
         sidebar): inset every option uniformly from the container
         and from each other (padding + flex gap), and re-add the
         radius so the focused/active wash reads as a rounded pill
         instead of a stripe. Rich custom renderOption content
         (multi-line ticket cards, etc.) also wants more vertical
         padding than the dropdown's compact rows. */
      :host([block]) .options-scroll {
        padding: 4px;
        gap: 4px;
      }
      :host([block]) .option {
        margin: 0;
        padding: 8px var(--pad);
        border-radius: var(--r-sm);
      }
 
      .option * {
        user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        overflow-wrap: break-word;
        word-wrap: break-word;
        -ms-word-break: break-all;
        word-break: break-word;
        -ms-hyphens: auto;
        -moz-hyphens: auto;
        -webkit-hyphens: auto;
        hyphens: auto;
      }
 
      .option .detail {
        font-size: 85%;
        color: rgba(0, 0, 0, 0.4);
      }
 
      code {
        background: rgba(0, 0, 0, 0.05);
        padding: 1px 5px;
        border-radius: var(--curvature-widget);
      }
 
      :host([block]) {
        position: relative;
      }
 
      :host([block]) .options {
        overflow-y: initial;
      }
 
      temba-loading {
        align-self: center;
        margin-top: 0.025em;
      }
 
      .loader-bar {
        pointer-events: none;
        align-items: center;
        background: #eee;
        max-height: 0;
        transition: max-height var(--transition-speed) ease-in-out;
        border-bottom-left-radius: var(--curvature-widget);
        border-bottom-right-radius: var(--curvature-widget);
        display: flex;
        overflow: hidden;
      }
 
      .loading .loader-bar {
        max-height: 1.1em;
      }
 
      /* The component syncs cursorIndex to pointer position on
         mousemove, so .focused already follows the mouse. A
         separate :hover rule would just create a second highlight
         that flickers between rows on transition — keep only the
         single focused/active state. */
      .option.focused {
        background: var(
          --temba-options-option-focus-bg,
          var(--color-selection)
        );
        color: var(--temba-options-option-focus-text, var(--accent-700));
        --icon-color: var(--accent-700);
      }
 
      .option.no-options {
        pointer-events: none;
      }
 
      .option.no-options:hover {
        background: transparent;
        color: var(--color-text-dark-secondary);
      }
    `;
  }
 
  @property({ type: Number })
  top: number;
 
  @property({ type: Number })
  left: number;
 
  @property({ type: Number })
  width: number;
 
  @property({ type: Number, attribute: 'min-width' })
  minWidth: number;
 
  @property({ type: Number, attribute: 'static-width' })
  staticWidth: number;
 
  @property({ type: Boolean, attribute: 'anchor-right' })
  anchorRight: boolean;
 
  @property({ type: Number })
  marginHorizontal = 0;
 
  @property({ type: Number })
  marginVertical = 7;
 
  @property({ type: Object })
  anchorTo: HTMLElement;
 
  @property({ type: Boolean })
  visible: boolean;
 
  @property({ type: Boolean })
  block: boolean;
 
  // if we allow the focus to follow the cursor
  @property({ type: Boolean })
  cursorHover: boolean;
 
  // fire selection events when cursor changes
  @property({ type: Boolean })
  cursorSelection: boolean;
 
  @property({ type: Number })
  scrollPct = 75;
 
  @property({ type: Number })
  cursorIndex = -1;
 
  @property({ type: Boolean })
  internalFocusDisabled = false;
 
  @property({ type: Array })
  options: any[];
 
  @property({ type: Array })
  tempOptions: any[];
 
  @property({ type: Boolean })
  poppedTop: boolean;
 
  @property({ type: Boolean })
  spaceSelect: boolean;
 
  @property({ type: String })
  nameKey = 'name';
 
  @property({ type: Boolean })
  loading = false;
 
  @property({ type: Boolean })
  collapsed: boolean;
 
  @property({ type: Boolean })
  hideShadow = false;
 
  @property({ attribute: false })
  getName: { (option: any): string } = function (option: any) {
    return option[this.nameKey || 'name'];
  };
 
  @property({ attribute: false })
  renderInputOption: { (): TemplateResult } = function () {
    return null;
  };
 
  @property({ attribute: false })
  renderOption: { (option: any, selected: boolean): TemplateResult };
 
  // renders between rows (and above the first) when the data crosses a
  // grouping boundary - the result sits outside the option rows so it never
  // picks up their hover or selection treatment. Returning null means the
  // rows aren't a boundary and nothing is rendered between them.
  @property({ attribute: false })
  renderDivider: { (prev: any, option: any): TemplateResult | null };
 
  @property({ attribute: false })
  renderOptionName: { (option: any, selected: boolean): TemplateResult };
 
  @property({ attribute: false })
  renderOptionDetail: { (option: any, selected: boolean): TemplateResult };
 
  @property({ type: Number })
  scrollHeight = 0;
 
  @property({ type: Boolean })
  triggerScroll = false;
 
  @property({ type: Boolean })
  showEmptyMessage = false;
 
  scrollParent: HTMLElement = null;
 
  resolvedRenderOption: { (option: any, selected: boolean): TemplateResult };
 
  public firstUpdated(changed: Map<string, any>) {
    super.firstUpdated(changed);
    if (!this.block) {
      this.scrollParent = getScrollParent(this);
      this.calculatePosition = this.calculatePosition.bind(this);
      if (this.scrollParent) {
        this.scrollParent.addEventListener('scroll', this.calculatePosition);
      }
    }
 
    this.resolvedRenderOption = (
      this.renderOption || this.renderOptionDefault
    ).bind(this);
  }
 
  public disconnectedCallback() {
    if (!this.block) {
      if (this.scrollParent) {
        this.scrollParent.removeEventListener('scroll', this.calculatePosition);
      }
    }
 
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
  }
 
  private isFocused(): boolean {
    // TODO: this really doesn't seem right
    const focused =
      this.closestElement(document.activeElement.tagName) ===
      document.activeElement;
 
    return focused;
  }
 
  private resizeObserver: ResizeObserver;
 
  public willUpdate(changed: PropertyValues) {
    super.willUpdate(changed);
 
    if (changed.has('visible') && changed.has('options')) {
      if (!this.visible && this.options.length == 0) {
        this.tempOptions = changed.get('options');
        window.setTimeout(() => {
          this.tempOptions = [];
        }, 200);
      }
    }
 
    if (changed.has('options')) {
      // allow scrolls to trigger again
      this.triggerScroll = true;

      const prevOptions = changed.get('options');
      const previousCount = prevOptions ? prevOptions.length : 0;
      const newCount = this.options ? this.options.length : 0;
 
      if (
        this.cursorIndex === -1 ||
        newCount < previousCount ||
        (previousCount === 0 && newCount > 0 && !changed.has('cursorIndex'))
      ) {
        if (!this.internalFocusDisabled) {
          if (!this.block) {
            this.cursorIndex = 0;
          } else {
            if (this.cursorIndex >= newCount) {
              this.cursorIndex = newCount - 1;
            }
          }
        }
      }
    }
 
    // pre-sync width so adjustWidth() in updated() won't trigger a warning
    if ((changed.has('options') || changed.has('anchorTo')) && this.anchorTo) {
      const anchorBounds = this.anchorTo.getBoundingClientRect();
      this.width =
        this.staticWidth > 0
          ? this.staticWidth
          : anchorBounds.width - 2 - this.marginHorizontal * 2;
    }
 
    // pre-sync position so calculatePosition() in updated() won't trigger a warning
    if (
      (changed.has('options') || changed.has('visible')) &&
      this.visible &&
      !this.block &&
      this.anchorTo
    ) {
      const anchorBounds = this.anchorTo.getBoundingClientRect();
      this.top = anchorBounds.bottom;
      this.left = anchorBounds.left;
    }
  }
 
  public updated(changed: Map<string, any>) {
    super.updated(changed);
 
    if (changed.has('anchorTo') && this.anchorTo) {
      const optionsContainer =
        this.shadowRoot.querySelector('.options-container');
 
      if (!this.resizeObserver) {
        this.resizeObserver = new ResizeObserver((entries) => {
          window.requestAnimationFrame((): void | undefined => {
            if (!Array.isArray(entries) || !entries.length) {
              return;
            }
            this.calculatePosition();
            this.adjustWidth();
          });
        });
        this.resizeObserver.observe(optionsContainer);
      }
    }
 
    // if our cursor changed, lets make sure our scrollbox is showing it
    if (!this.internalFocusDisabled && changed.has('cursorIndex')) {
      this.fireCustomEvent(CustomEventType.CursorChanged, {
        index: this.cursorIndex
      });
    }
 
    if (changed.has('options')) {
      this.adjustWidth();
      this.calculatePosition();
 
      const prevOptions = changed.get('options');
      const previousCount = prevOptions ? prevOptions.length : 0;
      const newCount = this.options ? this.options.length : 0;
 
      if (
        this.cursorIndex === -1 ||
        newCount < previousCount ||
        (previousCount === 0 && newCount > 0 && !changed.has('cursorIndex'))
      ) {
        if (!this.internalFocusDisabled) {
          if (this.cursorSelection) {
            this.handleSelection(false);
          }
        }
      }
 
      // if on initial load we don't have enough options to load, trigger a scroll
      // threshold event in case the page size is smaller than our control height
      const scrollBox = this.shadowRoot.querySelector('.options');
      if (scrollBox) {
        if (scrollBox.scrollHeight == scrollBox.clientHeight) {
          this.fireCustomEvent(CustomEventType.ScrollThreshold);
        }
      }
    }
  }
 
  private renderOptionDefault(option: any, selected: boolean): TemplateResult {
    const renderOptionName = (
      this.renderOptionName || this.renderOptionNameDefault
    ).bind(this);
    const renderOptionDetail = (
      this.renderOptionDetail || this.renderOptionDetailDefault
    ).bind(this);
 
    if (selected) {
      return html`
        <div class="name">${renderOptionName(option, selected)}</div>
        <div class="detail">${renderOptionDetail(option, selected)}</div>
      `;
    } else {
      return html`
        <div class="name">${renderOptionName(option, selected)}</div>
      `;
    }
  }
 
  private renderOptionNameDefault(option: any): TemplateResult {
    return html`<div style="display:flex; align-items:flex-start">
      ${option.icon
        ? html`<temba-icon
            name="${option.icon}"
            style="margin-right:0.5em; fill: var(--color-text-dark);"
          ></temba-icon>`
        : null}
      <div style="flex-grow:1">${option.prefix}${this.getName(option)}</div>
    </div>`;
  }
 
  private renderOptionDetailDefault(option: any): TemplateResult {
    return html` ${option.detail} `;
  }
 
  public getSelection() {
    return this.options[this.cursorIndex];
  }
 
  private handleSelection(tabbed = false, index = -1) {
    if (!this.internalFocusDisabled) {
      if (index === -1) {
        index = this.cursorIndex;
      }
    }
 
    if (index === -1) {
      return;
    }
 
    const selected = this.options[index];
    this.fireCustomEvent(CustomEventType.Selection, {
      selected,
      tabbed,
      index
    });
  }

  private moveCursor(direction: number): void {
    if (!this.internalFocusDisabled) {
      const newIndex = Math.max(
        Math.min(this.cursorIndex + direction, this.options.length - 1),
        0
      );
      this.setCursor(newIndex);
    }
  }
 
  private setCursor: (index: number) => void = throttle(function (
    index: number
  ) {
    if (!this.internalFocusDisabled) {
      if (index !== this.cursorIndex) {
        this.cursorIndex = index;
      }
    }
  }, 50);
 
  public scrollToTop() {
    const scrollBox = this.shadowRoot.querySelector('.options-scroll');
    scrollBox.scrollTop = 0;
  }
 
  private ensureOptionVisible() {
    const focusedOption = this.shadowRoot.querySelector(
      `div[data-option-index="${this.cursorIndex}"]`
    ) as HTMLDivElement;
    if (focusedOption) {
      focusedOption.scrollIntoView({
        block: 'nearest',
        inline: 'start'
      });
    }
  }
  private handleKeyDown(evt: KeyboardEvent) {
    if (this.internalFocusDisabled || (this.block && !this.isFocused())) {
      return;
    }
 
    if (this.offsetParent === null) {
      return;
    }
 
    // Don't intercept keys when the popup isn't visible to the user. The
    // element can still be in the layout tree (so offsetParent is non-null)
    // while being hidden via opacity/pointer-events, in which case handling
    // keys would steal them from the underlying input.
    if (!this.block && !this.visible) {
      return;
    }
 
    // Only intercept keys when the event originated within our owning component.
    // Without this, any temba-options with populated options would swallow arrow
    // keys document-wide, breaking cursor movement in unrelated text editors.
    // All current usages render temba-options inside another custom element's
    // shadow root, so scoping by host correctly distinguishes between editors.
    if (!this.block) {
      const root = this.getRootNode();
      const host = root instanceof ShadowRoot ? root.host : null;
      if (host && !evt.composedPath().includes(host)) {
        return;
      }
    }
 
    if (this.options && this.options.length > 0) {
      if ((evt.ctrlKey && evt.key === 'n') || evt.key === 'ArrowDown') {
        this.moveCursor(1);
        evt.preventDefault();
        evt.stopPropagation();
        if (this.cursorSelection) {
          this.handleSelection(false);
        }
        this.ensureOptionVisible();
      } else if ((evt.ctrlKey && evt.key === 'p') || evt.key === 'ArrowUp') {
        this.moveCursor(-1);
        evt.preventDefault();
        if (this.cursorSelection) {
          this.handleSelection(false);
        }
        this.ensureOptionVisible();
      } else if (
        evt.key === 'Enter' ||
        evt.key === 'Tab' ||
        (this.spaceSelect && evt.key === ' ')
      ) {
        evt.preventDefault();
        evt.stopPropagation();
        this.handleSelection(evt.key === 'Tab');
      } else if (evt.key === 'Escape') {
        this.fireCustomEvent(CustomEventType.Canceled);
      }
    }
  }
 
  private handleInnerScroll(evt: Event) {
    const scrollbox = evt.target as HTMLDivElement;

    // scroll height has changed, enable scroll trigger
    if (scrollbox.scrollHeight > this.scrollHeight) {
      this.scrollHeight = scrollbox.scrollHeight;
      this.triggerScroll = true;
    }

    if (this.triggerScroll) {
      const scrollPct =
        scrollbox.scrollTop / (scrollbox.scrollHeight - scrollbox.clientHeight);
      if (scrollPct * 100 > this.scrollPct) {
        this.fireCustomEvent(CustomEventType.ScrollThreshold);
        this.triggerScroll = false;
      }
    }
  }
 
  private adjustWidth() {
    if (this.anchorTo) {
      const anchorBounds = this.anchorTo.getBoundingClientRect();
      this.width =
        this.staticWidth > 0
          ? this.staticWidth
          : anchorBounds.width - 2 - this.marginHorizontal * 2;
    }
  }

  private calculatePosition() {
    if (this.visible && !this.block) {
      const optionsBounds = this.shadowRoot
        .querySelector('.options-container')
        .getBoundingClientRect();

      if (this.anchorTo) {
        const anchorBounds = this.anchorTo.getBoundingClientRect();
        this.top = anchorBounds.bottom;
        if (anchorBounds.bottom + optionsBounds.height > window.innerHeight) {
          this.top =
            anchorBounds.bottom -
            (optionsBounds.height + anchorBounds.height + 10);
        }
        this.left = anchorBounds.left;
      }
    }
  }
 
  public getEventHandlers(): EventHandler[] {
    return [
      {
        event: 'keydown',
        method: this.handleKeyDown,
        isDocument: true
      },
      {
        event: 'scroll',
        method: this.calculatePosition,
        isDocument: true
      }
    ];
  }
 
  private handleMouseMove(evt: MouseEvent) {
    if (!this.block || this.cursorHover) {
      if (Math.abs(evt.movementX) + Math.abs(evt.movementY) > 0) {
        const index = (evt.currentTarget as HTMLElement).getAttribute(
          'data-option-index'
        );
        this.setCursor(parseInt(index));
      }
    }
  }

  private handleOptionClick(evt: MouseEvent) {
    const index = (evt.currentTarget as HTMLElement).getAttribute(
      'data-option-index'
    );
 
    if (index) {
      const newIndex = parseInt(index);
      this.setCursor(newIndex);
      this.handleSelection(false, newIndex);
    }
  }
 
  public render(): TemplateResult {
    if (!this.resolvedRenderOption) {
      return null;
    }
 
    let vertical = this.block ? 0 : this.marginVertical;
    if (this.poppedTop) {
      vertical *= -1;
    }

    const containerStyle = this.visible
      ? {
          'margin-left': `${this.marginHorizontal}px`,
          'margin-top': `${vertical}px`
        }
      : {};
 
    if (this.top) {
      containerStyle['top'] = `${this.top}px`;
    }
 
    if (this.left) {
      containerStyle['left'] = `${this.left}px`;
    }
 
    const optionsStyle = {};
    if (this.width) {
      optionsStyle['width'] = `${this.width}px`;
    }
    if (this.minWidth) {
      optionsStyle['min-width'] = `${this.minWidth}px`;
    }
 
    const classes = getClasses({
      'options-container': true,
      show: this.visible,
      top: this.poppedTop,
      anchored: !this.block && !!this.anchorTo,
      loading: this.loading,
      shadow: !this.hideShadow,
      bordered: this.hideShadow
    });
 
    const classesInner = getClasses({
      options: true
    });
 
    let options = this.options || [];
    if (
      options.length == 0 &&
      this.tempOptions &&
      this.tempOptions.length > 0
    ) {
      options = this.tempOptions;
    }
 
    return html`
      <div class=${classes} style=${styleMap(containerStyle)}>
        <div class="options-scroll" @scroll=${this.handleInnerScroll}>
          <div class="${classesInner}" style=${styleMap(optionsStyle)}>
            ${options.length > 0
              ? options.map((option, index) => {
                  return html`${this.renderDivider
                      ? this.renderDivider(
                          index > 0 ? options[index - 1] : null,
                          option
                        )
                      : null}
                    <div
                      data-option-index="${index}"
                      @mousemove=${this.handleMouseMove}
                      @mousedown=${this.handleOptionClick}
                      class="option ${index === this.cursorIndex &&
                      !this.internalFocusDisabled
                        ? 'focused'
                        : ''}"
                    >
                      ${this.resolvedRenderOption(
                        option,
                        index === this.cursorIndex
                      )}
                    </div>`;
                })
              : this.visible && this.showEmptyMessage
                ? html`<div
                    class="option no-options"
                    style="color: var(--color-text-dark-secondary); cursor: default;"
                  >
                    ${msg('No options')}
                  </div>`
                : null}
            ${this.block ? html`<div style="height:0.1em"></div>` : null}
          </div>
          <slot></slot>
        </div>
 
        <div class="loader-bar">
          <temba-loading></temba-loading>
        </div>
      </div>
    `;
  }
}