All files / common/services scrollbar-helper.service.ts

100% Statements 17/17
100% Branches 0/0
100% Functions 1/1
100% Lines 16/16

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                      1x 8x             8x     8x 8x 8x 8x 8x   8x 8x   8x 8x 8x   8x 8x   8x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { inject, Injectable, DOCUMENT } from '@angular/core';
 
/**
 * Gets the width of the scrollbar.  Nesc for windows
 * http://stackoverflow.com/a/13382873/888165
 */
@Injectable({ providedIn: 'root' })
export class ScrollbarHelper {
  private document = inject(DOCUMENT);
 
  /**
   * The width of the scrollbar.
   *
   * @defaultValue this.getWidth()
   */
  readonly width: number = this.getWidth();
 
  private getWidth(): number {
    const outer = this.document.createElement('div');
    outer.style.visibility = 'hidden';
    outer.style.width = '100px';
    (outer.style as any).msOverflowStyle = 'scrollbar';
    this.document.body.appendChild(outer);
 
    const widthNoScroll = outer.offsetWidth;
    outer.style.overflow = 'scroll';
 
    const inner = this.document.createElement('div');
    inner.style.width = '100%';
    outer.appendChild(inner);
 
    const widthWithScroll = inner.offsetWidth;
    outer.parentNode?.removeChild(outer);
 
    return widthNoScroll - widthWithScroll;
  }
}