Skip to content

Commit 7c5ef9c

Browse files
committed
feat(vhelper-scrollbar): update implementation
1 parent 3d01370 commit 7c5ef9c

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

projects/igniteui-angular/src/lib/core/styles/components/drop-down/_drop-down-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
-webkit-overflow-scrolling: touch;
244244
position: relative;
245245

246-
igx-display-container.has-scrollbar {
246+
.igx-display-container--scrollbar {
247247
padding-inline-end: var(--vhelper-scrollbar-size);
248248
}
249249
}

projects/igniteui-angular/src/lib/directives/for-of/base.helper.component.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import {
66
Directive,
77
AfterViewInit,
88
Inject,
9-
NgZone
9+
NgZone,
10+
Renderer2,
11+
PLATFORM_ID
1012
} from '@angular/core';
1113
import { DOCUMENT } from '@angular/common';
1214
import { Subject } from 'rxjs';
1315
import { takeUntil, throttleTime } from 'rxjs/operators';
1416
import { resizeObservable, PlatformUtil } from '../../core/utils';
17+
import { isPlatformBrowser } from '@angular/common';
1518

1619
@Directive({
1720
selector: '[igxVirtualHelperBase]',
@@ -34,6 +37,9 @@ export class VirtualHelperBaseDirective implements OnDestroy, AfterViewInit {
3437
protected _zone: NgZone,
3538
@Inject(DOCUMENT) public document: any,
3639
protected platformUtil: PlatformUtil,
40+
private renderer: Renderer2,
41+
@Inject(PLATFORM_ID) private platformId: Object,
42+
private ngZone: NgZone
3743
) {
3844
this._scrollNativeSize = this.calculateScrollNativeSize();
3945
}
@@ -104,25 +110,31 @@ export class VirtualHelperBaseDirective implements OnDestroy, AfterViewInit {
104110
return this.document.body.contains(this.nativeElement);
105111
}
106112

113+
private toggleClass(element: HTMLElement, className: string, add: boolean): void {
114+
if (!element) return;
115+
add ? this.renderer.addClass(element, className) : this.renderer.removeClass(element, className);
116+
}
117+
107118
private updateScrollbarClass() {
108-
requestAnimationFrame(() => {
109-
const hasScrollbar = this.nativeElement.scrollHeight > this.nativeElement.clientHeight;
110-
const prevSibling = this.nativeElement.previousElementSibling as HTMLElement | null;
111-
112-
if (hasScrollbar) {
113-
this.nativeElement.classList.add('has-scrollbar');
114-
if (prevSibling?.tagName.toLowerCase() === 'igx-display-container') {
115-
prevSibling.classList.add('has-scrollbar');
116-
}
117-
} else {
118-
this.nativeElement.classList.remove('has-scrollbar');
119-
if (prevSibling?.tagName.toLowerCase() === 'igx-display-container') {
120-
prevSibling.classList.remove('has-scrollbar');
121-
}
119+
if (!isPlatformBrowser(this.platformId)) {
120+
return;
121+
}
122+
123+
this.ngZone.runOutsideAngular(() => {
124+
requestAnimationFrame(() => {
125+
const el = this.nativeElement;
126+
const hasScrollbar = el.scrollHeight > el.clientHeight;
127+
const prevSibling = el.previousElementSibling as HTMLElement | null;
128+
const scrollbarClass = 'igx-display-container--scrollbar';
129+
130+
if (prevSibling?.tagName.toLowerCase() === 'igx-display-container') {
131+
this.toggleClass(prevSibling, scrollbarClass, hasScrollbar);
122132
}
133+
});
123134
});
124135
}
125136

137+
126138
protected handleMutations(event) {
127139
const hasSize = !(event[0].contentRect.height === 0 && event[0].contentRect.width === 0);
128140
if (!hasSize && !this.isAttachedToDom) {

projects/igniteui-angular/src/lib/directives/for-of/horizontal.virtual.helper.component.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ElementRef, HostBinding, Input, ViewChild, ViewContainerRef, ChangeDetectorRef, Inject, NgZone } from '@angular/core';
1+
import { Component, ElementRef, HostBinding, Input, ViewChild, ViewContainerRef, ChangeDetectorRef, Inject, NgZone, Renderer2, PLATFORM_ID } from '@angular/core';
22
import { VirtualHelperBaseDirective } from './base.helper.component';
33
import { DOCUMENT } from '@angular/common';
44
import { PlatformUtil } from '../../core/utils';
@@ -19,8 +19,17 @@ export class HVirtualHelperComponent extends VirtualHelperBaseDirective {
1919
@HostBinding('class')
2020
public cssClasses = 'igx-vhelper--horizontal';
2121

22-
constructor(elementRef: ElementRef, cdr: ChangeDetectorRef, zone: NgZone, @Inject(DOCUMENT) document, platformUtil: PlatformUtil) {
23-
super(elementRef, cdr, zone, document, platformUtil);
22+
constructor(
23+
elementRef: ElementRef,
24+
cdr: ChangeDetectorRef,
25+
zone: NgZone,
26+
@Inject(DOCUMENT) document: any,
27+
platformUtil: PlatformUtil,
28+
renderer: Renderer2,
29+
@Inject(PLATFORM_ID) platformId: Object,
30+
ngZone: NgZone
31+
) {
32+
super(elementRef, cdr, zone, document, platformUtil, renderer, platformId, ngZone);
2433
}
2534

2635
protected override restoreScroll() {

projects/igniteui-angular/src/lib/directives/for-of/virtual.helper.component.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, ElementRef, HostBinding, Input, ViewChild, ViewContainerRef,
2-
ChangeDetectorRef, OnDestroy, OnInit, Inject, NgZone } from '@angular/core';
2+
ChangeDetectorRef, OnDestroy, OnInit, Inject, NgZone, Renderer2,
3+
PLATFORM_ID} from '@angular/core';
34
import { VirtualHelperBaseDirective } from './base.helper.component';
45
import { DOCUMENT } from '@angular/common';
56
import { PlatformUtil } from '../../core/utils';
@@ -21,8 +22,17 @@ export class VirtualHelperComponent extends VirtualHelperBaseDirective implement
2122
@HostBinding('class')
2223
public cssClasses = 'igx-vhelper--vertical';
2324

24-
constructor(elementRef: ElementRef, cdr: ChangeDetectorRef, zone: NgZone, @Inject(DOCUMENT) document, platformUtil: PlatformUtil) {
25-
super(elementRef, cdr, zone, document, platformUtil);
25+
constructor(
26+
elementRef: ElementRef,
27+
cdr: ChangeDetectorRef,
28+
zone: NgZone,
29+
@Inject(DOCUMENT) document: any,
30+
platformUtil: PlatformUtil,
31+
renderer: Renderer2,
32+
@Inject(PLATFORM_ID) platformId: Object,
33+
ngZone: NgZone
34+
) {
35+
super(elementRef, cdr, zone, document, platformUtil, renderer, platformId, ngZone);
2636
}
2737

2838
public ngOnInit() {

0 commit comments

Comments
 (0)