All files / lib/tab-folder tab-folder.component.ts

96.29% Statements 728/756
89.58% Branches 129/144
97.61% Functions 41/42
96.29% Lines 728/756

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 7571x 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 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 526x 1x 675x 675x 675x 675x 675x 675x 675x 675x 675x 675x 675x 675x 675x 675x 675x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 1x 1x 1x 213x 213x 213x 1x 1x 68x 68x 68x 1x 1x 1x 1x 1x 63033x 63033x 63033x 1x 1x 159x 159x 159x 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 135x 135x 135x 135x 193x 135x 135x 1x 1x 1x 1x 1x 135x 135x 135x 135x 1x 1x 1x 135x 135x 135x 135x 135x 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 186x 186x 186x 186x   186x 1x 1x 1x 5x 5x 5x 1x 1x 1x 1x 1x 37x 37x 37x 37x 1x 1x 1x 1x 1x 21x 21x 21x 21x 21x 6x 6x 6x 13x 3x 3x 3x 21x 1x 1x 1x 1x 1x 1x 1x 21x 10x 21x 21x 21x 11x 11x 21x 21x 21x 43x 21x 22x 22x 21x 21x 21x 21x 21x 21x 21x 1x 1x 1x 3x 5x 5x 5x 3x 3x 3x 3x 3x 3x 1x 1x 1x 3x 3x 9x 9x 3x 3x 3x 3x 3x 3x 1x 1x 1x 8180x 8180x 1x 1x 1x 25x 16x 16x 16x 25x 25x 25x 10x 25x 10x 10x 8x 8x 4x 4x 2x 2x 1x 1x 10x 10x 15x 15x 11x 11x 25x 25x 1x 1x 1x 149x 149x 149x 149x 149x 149x 149x 149x 1x 1x 1x 6x   6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 399x 399x 399x 399x 399x 399x 399x 399x 1x 1x 1x 1x 1x 399x 399x 399x 399x 399x 399x 399x 399x 399x 1x 1x 1x 1x 1x 96x 96x 96x 96x 72x 96x 96x 1x 1x 1x 1x 1x 416x 325x 325x 325x 325x 325x 325x 404x 91x 416x 416x 416x 13x 13x 13x 4x 4x 9x 9x 9x 9x 13x 13x 13x 80x 80x 32x 32x 32x 32x 32x 32x 32x 416x 416x 416x 32x 32x 32x 6x 26x 12x 12x 12x 32x 32x 416x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 1x 1x 32x 32x   32x 32x 32x 32x 1x 1x 1x 399x 399x 399x     399x 1x 1x 1x 1x 1x 6x 6x 6x   6x 6x 6x 1x 1x 1x 1x 1x 3x 3x 3x   3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 186x 186x 186x 186x   186x 1x 1x 1x 1x 1x 294x 294x 294x 294x 159x 159x 159x 294x 294x 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 267x 267x 267x 267x 213x 213x 213x 213x 213x 213x 267x 267x 267x 267x 267x 267x 267x 267x 267x 185x 49x 49x 164x 15x 149x 149x 267x 267x 267x 213x 267x 267x 1x 1x 1x 1x 1x 213x 47x 47x 47x 47x 47x 35x 12x 12x 213x 213x 1x 1x 1x 6x 6x 5x 5x 5x 5x 5x 5x 6x 6x 6x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterContentInit,
  AfterViewInit,
  Component,
  ContentChildren,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnDestroy,
  Output,
  QueryList,
  TemplateRef,
  ViewChild,
  ViewChildren,
  forwardRef,
} from '@angular/core';
import { Subscription } from 'rxjs';
 
import { Maybe, observeSize } from '../utils/internal-utils';
import {
  Callback,
  FocusableElementOwner,
  Item,
  Key,
  WidgetKeySet,
} from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
import { TabPickerComponent } from './tab-picker.component';
import { TabComponent } from './tab.component';
 
export type TabListMode = 'default' | 'scroll' | 'picker';
type Overflow = 'both' | 'left' | 'right';
 
interface Rect {
  left: number;
  top: number;
  right: number;
  bottom: number;
  width: number;
  height: number;
}
 
function relativeRect(element: HTMLElement): Rect {
  const rect = element.getBoundingClientRect();
  const parent = element.parentElement;
  if (parent) {
    const parentRect = parent.getBoundingClientRect();
    return createRect(
      Math.floor(rect.x - parentRect.x),
      Math.floor(rect.y - parentRect.y),
      Math.ceil(rect.width),
      Math.ceil(rect.height),
    );
  }
  return createRect(rect.x, rect.y, rect.width, rect.height);
}
 
function createRect(
  left: number,
  top: number,
  width: number,
  height: number,
): Rect {
  return {
    left,
    top,
    width,
    height,
    right: left + width,
    bottom: top + height,
  };
}
 
/**
 * A Tab Folder component that contains a Tab component or multiple Tab components and displays the content of the active one.
 */
@Component({
  selector: 'gc-tab-folder',
  templateUrl: './tab-folder.component.html',
  styleUrls: [
    './../utils/styles/reset-button.css',
    './tab-folder.component.css',
  ],
  standalone: false,
})
export class TabFolderComponent
  implements AfterContentInit, AfterViewInit, OnDestroy, FocusableElementOwner
{
  /**
   * A label text to be used for aria-label attribute.
   */
  @Input()
  public label = '';
 
  /**
   * Decides if the tab buttons will appear rounded or not.
   */
  @Input()
  public variant: 'default' | 'rounded' = 'default';
 
  /**
   * Sets the surface color.
   */
  @Input()
  public surface: 'default' | 'contrast' = 'default';
 
  /**
   * User-initiated tab switch will be only performed when the callback evaluates with value `true`. Leaving this
   * empty (`undefined`) is equivalent to always resolving to `true`.
   */
  @Input()
  public permitTabKeyChange?: (key: Key | symbol) => Promise<boolean> | boolean;
 
  /**
   * Optional template to render icon
   */
  @Input()
  public iconProvider?: TemplateRef<unknown>;
 
  /**
   * Set of item keys that will be rendered as disabled items.
   */
  @Input()
  public disabledItems: ReadonlySet<string> = new Set();
 
  /**
   * Informs about the changes of the active tab key.
   */
  @Output()
  public readonly activeTabKeyChange: EventEmitter<string> =
    new EventEmitter<string>(true);
 
  /** @ignore */
  @ContentChildren(forwardRef(() => TabComponent))
  protected _tabs?: QueryList<TabComponent>;
 
  /** @ignore */
  @HostBinding('style.--gc-tab-header-inset.px')
  private _tabHeaderInset: number | null = null;
 
  /**
   * @ignore
   */
  @ViewChildren('tabButton')
  private tabButtons: QueryList<ElementRef<HTMLButtonElement>> =
    new QueryList();
 
  /**
   * @ignore
   */
  @ViewChild('tabFolder')
  private tabFolder!: ElementRef<HTMLDivElement>;
 
  /**
   * @ignore
   */
  @ViewChild('tabList')
  private tabList!: ElementRef<HTMLDivElement>;
 
  /** @ignore */
  @ViewChild('picker', { read: TabPickerComponent })
  private picker?: TabPickerComponent<Item>;
 
  @Input()
  public get tabHeaderInset(): number | null {
    return this._tabHeaderInset;
  }
 
  public set tabHeaderInset(value: number | null) {
    this._tabHeaderInset = value;
    this.handleResize();
  }
 
  /**
   * Key of the active tab.
   */
  @Input()
  public get activeTabKey(): string {
    return this._activeTabKey;
  }
 
  public set activeTabKey(key: string) {
    this._activeTabKey = key;
    this.selectTabButtonByActiveTabKey();
  }
 
  /** @ignore */
  protected readonly widgetKeys: WidgetKeySet =
    createWidgetKeySet('gc-tab-folder');
 
  /**
   * @ignore
   */
  protected _overflow: Overflow = 'both';
 
  /**
   * @ignore
   */
  protected _mode: TabListMode = 'default';
 
  /** @ignore */
  protected pickerTabs: TabComponent[] = [];
 
  /**
   * @ignore
   */
  private _focusedTabIndex = 0;
 
  /**
   * @ignore
   */
  private _activeTabKey = '';
 
  /**
   * @ignore
   */
  private currentDelta = 0;
 
  /**
   * @ignore
   */
  private _resizeUpdate?: ReturnType<typeof setTimeout>;
 
  /**
   * @ignore
   */
  private _resizeDestroy?: Callback;
 
  /** @ignore */
  private tabsSubscription?: Subscription;
 
  /** @ignore */
  private lastActiveItemIndex = 0;
 
  constructor(
    private el: ElementRef<HTMLElement>,
    zone: NgZone,
  ) {
    this._resizeDestroy = observeSize(el, zone, () => {
      this.handleResize();
    });
  }
 
  /**
   * @ignore
   */
  ngAfterViewInit(): void {
    setTimeout(() => {
      this.selectTabButtonByActiveTabKey();
    });
  }
 
  /** @ignore */
  public ngAfterContentInit(): void {
    this.pickerTabs = this._tabs ? this._tabs.toArray() : [];
    this.tabsSubscription = this._tabs?.changes.subscribe(() =>
      this.updateTabs(),
    );
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    if (this._resizeDestroy) {
      this._resizeDestroy();
      this._resizeDestroy = undefined;
    }
    if (this._resizeUpdate) {
      clearTimeout(this._resizeUpdate);
      this._resizeUpdate = undefined;
    }
    if (this.tabsSubscription) {
      this.tabsSubscription.unsubscribe();
    }
  }
 
  public focusChild(): boolean {
    const activeTabIndex = this._tabs
      ?.toArray()
      .findIndex(tab => tab.key === this.activeTabKey);
    const activeTabButton = activeTabIndex
      ? this.tabButtons.get(activeTabIndex)
      : undefined;
    const firstTabButton = this.tabButtons.get(0);
 
    if (this._mode === 'picker' && this.picker) {
      this.picker.focusChild();
      return true;
    } else if (activeTabButton) {
      activeTabButton.nativeElement.focus();
      return true;
    } else if (firstTabButton) {
      firstTabButton.nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /**
   * @ignore
   */
  protected _selectTab(tab: TabComponent): void {
    this.setActiveTabKey(tab);
    this.moveSelectedTabIntoView();
    this.lastActiveItemIndex = this._tabs
      ? this._tabs.toArray().indexOf(tab)
      : 0;
  }
 
  /** @ignore */
  protected _handlePickerChange(tab: TabComponent): void {
    this._selectTab(tab);
    this.activeTabKeyChange.emit(this.activeTabKey);
  }
 
  /**
   * @ignore
   */
  protected _onFocus(tab: TabComponent): void {
    if (this._tabs) {
      this._focusedTabIndex = this._tabs.toArray().indexOf(tab);
    }
  }
 
  /**
   * @ignore
   */
  protected _onKeyDown(event: KeyboardEvent): void {
    const key = event.key;
    let flag = false;
 
    switch (key) {
      case 'ArrowRight':
        flag = true;
        this.moveToNextTabIndex();
        break;
      case 'ArrowLeft':
        flag = true;
        this.moveToPrevTabIndex();
        break;
      case 'Home':
        flag = true;
        this.moveToFirstTabIndex();
        break;
      case 'End':
        flag = true;
        this.moveToLastTabIndex();
        break;
      default:
        break;
    }
 
    if (flag) {
      event.stopPropagation();
      event.preventDefault();
    }
 
    const tabToFocus = this.tabButtons.toArray().find((el, idx) => {
      if (idx === this._focusedTabIndex) {
        return el;
      } else {
        return null;
      }
    });
 
    if (tabToFocus) {
      tabToFocus.nativeElement.focus();
    }
  }
 
  /** @ignore */
  protected _handleScrollLeft(): void {
    const element = this.tabButtons.find(b => {
      const rect = relativeRect(b.nativeElement);
      const viewPort = this._getTabsViewPort();
      return viewPort.left > rect.left && viewPort.left <= rect.right;
    });
    if (element) {
      const rect = relativeRect(element.nativeElement);
      this.moveIntoView(rect, element.nativeElement);
    }
  }
 
  /** @ignore */
  protected _handleScrollRight(): void {
    const viewPort = this._getTabsViewPort();
    const element = this.tabButtons.find(b => {
      const rect = relativeRect(b.nativeElement);
      return rect.left >= viewPort.left && rect.right > viewPort.right;
    });
    if (element) {
      const rect = relativeRect(element.nativeElement);
      this.moveIntoView(rect, element.nativeElement);
    }
  }
 
  /** @ignore */
  protected _activeTab(): TabComponent | undefined {
    return this._tabs?.find(tab => tab.key == this.activeTabKey);
  }
 
  /** @ignore */
  protected async tabChangeHandler(tab: TabComponent) {
    const doChange = () => {
      this._selectTab(tab);
      this.activeTabKeyChange.emit(this.activeTabKey);
    };
 
    if (
      this.permitTabKeyChange !== undefined &&
      this.activeTabKey !== tab.key
    ) {
      const permit = this.permitTabKeyChange(tab.key);
      if (typeof permit !== 'boolean') {
        // permit is of type Promise<boolean>
        const proceed = await permit;
        if (proceed) {
          doChange();
        }
      } else if (permit) {
        // permit is of type boolean
        doChange();
      }
    } else if (
      this.permitTabKeyChange === undefined &&
      this.activeTabKey !== tab.key
    ) {
      doChange();
    }
  }
 
  /** @ignore */
  private _getTabsViewPort(): Rect {
    const tabRect = this.tabList.nativeElement.getBoundingClientRect();
    return createRect(
      Math.ceil(Math.abs(this.currentDelta)),
      0,
      Math.ceil(tabRect.width),
      Math.ceil(tabRect.height),
    );
  }
 
  /** @ignore */
  private schedulResizeUpdate() {
    if (this._resizeUpdate) {
      clearTimeout(this._resizeUpdate);
    }
 
    this._resizeUpdate = setTimeout(() => {
      this._resizeUpdate = undefined;
      this.handleResize();
    });
  }
 
  /**
   * @ignore
   */
  private moveSelectedTabIntoView(): void {
    if (this._activeTabKey) {
      this.findTabByKey(this.activeTabKey).then(b => {
        const rect = relativeRect(b);
        this.updateIndicator(rect);
        this.moveIntoView(rect, b);
      });
    }
  }
 
  /**
   * @ignore
   */
  private updateIndicator(rect: Rect) {
    this.el.nativeElement.style.setProperty(
      '--gc-selection-start',
      rect.left.toString() + 'px',
    );
    this.el.nativeElement.style.setProperty(
      '--gc-selection-end',
      rect.right.toString() + 'px',
    );
  }
 
  /**
   * @ignore
   */
  private isFullyVisible(b: HTMLButtonElement) {
    const rect = relativeRect(b);
    const width = this._getTabsViewPort().width;
    return !(
      this.currentDelta + rect.left < 0 ||
      this.currentDelta + rect.right > width
    );
  }
 
  /**
   * @ignore
   */
  private moveIntoView(rect: Rect, b: HTMLButtonElement) {
    if (this._mode === 'default') {
      this.currentDelta = 0;
      this.el.nativeElement.style.setProperty(
        '--gc-shift-x',
        this.currentDelta.toString() + 'px',
      );
      return;
    } else if (this._mode === 'picker') {
      return;
    }
 
    if (!this.isFullyVisible(b)) {
      const viewPort = this._getTabsViewPort();
 
      if (viewPort.left > rect.left) {
        // left out of view
        this.currentDelta = -rect.left;
      } else if (viewPort.right < rect.right) {
        // right out of view
        this.currentDelta = Math.ceil(
          viewPort.width - (rect.left + rect.width + 1),
        );
      }
    }
 
    this.currentDelta = this.constraintDelta(this.currentDelta);
 
    this.el.nativeElement.style.setProperty(
      '--gc-shift-x',
      this.currentDelta.toString() + 'px',
    );
 
    const first = this.tabButtons.get(0)?.nativeElement;
    const last = this.tabButtons.get(this.tabButtons.length - 1)?.nativeElement;
 
    if (first && last) {
      const firstVisible = this.isFullyVisible(first);
      const lastVisible = this.isFullyVisible(last);
      if (!firstVisible && !lastVisible) {
        this._overflow = 'both';
      } else if (!firstVisible) {
        this._overflow = 'left';
      } else {
        this._overflow = 'right';
      }
    }
  }
 
  /**
   * @ignore
   */
  private maxDelta() {
    const buttonWidths = this.tabButtons.map(
      b => b.nativeElement.getBoundingClientRect().width,
    );
    const totalButtonsWidths = Math.ceil(
      buttonWidths.reduce((a, b) => a + b, 0),
    );
    const tabsWidth = this._getTabsViewPort().width;
    return Math.ceil(tabsWidth - totalButtonsWidths);
  }
 
  /**
   * @ignore
   */
  private constraintDelta(delta: number) {
    const minDelta = this.maxDelta();
    if (minDelta >= 0) {
      return 0;
    } else {
      return Math.ceil(Math.max(delta, minDelta));
    }
  }
 
  /** @ignore */
  private findTabByKey(key: string): Maybe<HTMLButtonElement> {
    const idx = this._tabs?.toArray().findIndex(tab => tab.key == key);
    if (idx !== undefined && idx !== -1) {
      return Maybe.maybe(this.tabButtons.get(idx)?.nativeElement);
    }
    return Maybe.none();
  }
 
  /**
   * @ignore
   */
  private moveToNextTabIndex(): void {
    this._focusedTabIndex += 1;
 
    if (this._tabs && this._focusedTabIndex >= this._tabs.length) {
      this._focusedTabIndex = 0;
    }
    this.moveFocusIntoView();
  }
 
  /**
   * @ignore
   */
  private moveToPrevTabIndex(): void {
    this._focusedTabIndex -= 1;
 
    if (this._tabs && this._focusedTabIndex < 0) {
      this._focusedTabIndex = this._tabs.length - 1;
    }
    this.moveFocusIntoView();
  }
 
  /**
   * @ignore
   */
  private moveToFirstTabIndex(): void {
    this._focusedTabIndex = 0;
    this.moveFocusIntoView();
  }
 
  /**
   * @ignore
   */
  private moveToLastTabIndex(): void {
    if (this._tabs) {
      this._focusedTabIndex = this._tabs.length - 1;
    }
    this.moveFocusIntoView();
  }
 
  /**
   * @ignore
   */
  private moveFocusIntoView() {
    const b = this.tabButtons.get(this._focusedTabIndex)?.nativeElement;
    if (b) {
      const rect = relativeRect(b);
      this.moveIntoView(rect, b);
    }
  }
 
  /**
   * @ignore
   */
  private setActiveTabKey(tab: TabComponent): void {
    const foundTab = this._tabs?.find(i => i.key === tab.key);
    this._activeTabKey =
      foundTab?.key && !this.disabledItems.has(foundTab.key)
        ? foundTab.key
        : this.activeTabKey;
  }
 
  /**
   * @ignore
   */
  private selectTabButtonByActiveTabKey(): void {
    const tab = this._tabs?.find(i => i.key == this.activeTabKey);
    const indexOfTab =
      this._tabs && tab ? this._tabs.toArray().indexOf(tab) : 0;
    const tabButton = this.tabButtons.get(indexOfTab)?.nativeElement;
 
    if (tabButton && tab) {
      this._selectTab(tab);
    }
  }
 
  /**
   * @ignore
   */
  private adjustTabSelectionOnRemoval(): void {
    let activeTabKeyIndex = this.lastActiveItemIndex;
    if (this._tabs) {
      if (activeTabKeyIndex >= this._tabs.length) {
        activeTabKeyIndex -= 1;
      }
      if (activeTabKeyIndex >= 0) {
        const targetTab = this._tabs.get(activeTabKeyIndex);
        if (targetTab) {
          setTimeout(() => {
            this._selectTab(targetTab);
            this.activeTabKeyChange.emit(this.activeTabKey);
          });
        }
      }
    }
  }
 
  /**
   * @ignore
   */
  private handleResize() {
    if (this.tabButtons.length == 0) {
      return;
    }
    const buttonWidths = this.tabButtons.map(
      b => b.nativeElement.getBoundingClientRect().width,
    );
    const totalButtonsWidths = Math.ceil(
      buttonWidths.reduce((a, b) => a + b, 0),
    );
    const maxButtonWidth = Math.max(...buttonWidths);
    const inset = this.tabHeaderInset ?? 0;
    const availableWidth = Math.floor(
      this.el.nativeElement.getBoundingClientRect().width - inset * 2,
    );
 
    const leftRightButtons = 40 * 2;
 
    if (
      maxButtonWidth + leftRightButtons >= availableWidth ||
      availableWidth < 288
    ) {
      this.updateMode('picker');
    } else if (totalButtonsWidths > availableWidth) {
      this.updateMode('scroll');
    } else {
      this.updateMode('default');
    }
 
    if (this._activeTabKey) {
      this.moveSelectedTabIntoView();
    }
  }
 
  /**
   * @ignore
   */
  private updateMode(mode: TabListMode) {
    if (this._mode == mode) {
      return;
    }
 
    this._mode = mode;
    if (this._mode === 'picker') {
      this.tabFolder.nativeElement.setAttribute('aria-hidden', 'true');
    } else {
      this.tabFolder.nativeElement.removeAttribute('aria-hidden');
    }
  }
 
  /** @ignore */
  private updateTabs() {
    if (!this._tabs?.find(t => t.key === this.activeTabKey)) {
      this.adjustTabSelectionOnRemoval();
    } else {
      const tabToSelect = this._tabs.find(t => t.key === this.activeTabKey);
      if (tabToSelect) {
        this._selectTab(tabToSelect);
      }
    }
    this.schedulResizeUpdate();
    this.pickerTabs = this._tabs ? this._tabs.toArray() : [];
  }
}