Skip to content

Commit 8911970

Browse files
authored
fix(vhelper): fix padding only when vhelper scrollbar is visible (#15972)
1 parent 13bf65c commit 8911970

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
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
@@ -259,7 +259,7 @@
259259
-webkit-overflow-scrolling: touch;
260260
position: relative;
261261

262-
igx-display-container {
262+
.igx-display-container--scrollbar {
263263
padding-inline-end: var(--vhelper-scrollbar-size);
264264
}
265265
}

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

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

1619
@Directive({
1720
selector: '[igxVirtualHelperBase]',
@@ -27,13 +30,16 @@ export class VirtualHelperBaseDirective implements OnDestroy, AfterViewInit {
2730
private _afterViewInit = false;
2831
private _scrollNativeSize: number;
2932
private _detached = false;
33+
protected renderer = inject(Renderer2);
34+
protected platformId = inject(PLATFORM_ID);
35+
protected ngZone = inject(NgZone);
3036

3137
constructor(
3238
public elementRef: ElementRef<HTMLElement>,
3339
public cdr: ChangeDetectorRef,
3440
protected _zone: NgZone,
3541
@Inject(DOCUMENT) public document: any,
36-
protected platformUtil: PlatformUtil,
42+
protected platformUtil: PlatformUtil
3743
) {
3844
this._scrollNativeSize = this.calculateScrollNativeSize();
3945
}
@@ -104,6 +110,34 @@ export class VirtualHelperBaseDirective implements OnDestroy, AfterViewInit {
104110
return this.document.body.contains(this.nativeElement);
105111
}
106112

113+
private toggleClass(element: HTMLElement, className: string, shouldHaveClass: boolean): void {
114+
if (shouldHaveClass) {
115+
this.renderer.addClass(element, className);
116+
} else {
117+
this.renderer.removeClass(element, className);
118+
}
119+
}
120+
121+
private updateScrollbarClass() {
122+
if (!isPlatformBrowser(this.platformId)) {
123+
return;
124+
}
125+
126+
this.ngZone.runOutsideAngular(() => {
127+
requestAnimationFrame(() => {
128+
const el = this.nativeElement;
129+
const hasScrollbar = el.scrollHeight > el.clientHeight;
130+
const prevSibling = el.previousElementSibling as HTMLElement | null;
131+
const scrollbarClass = 'igx-display-container--scrollbar';
132+
133+
if (prevSibling?.tagName.toLowerCase() === 'igx-display-container') {
134+
this.toggleClass(prevSibling, scrollbarClass, hasScrollbar);
135+
}
136+
});
137+
});
138+
}
139+
140+
107141
protected handleMutations(event) {
108142
const hasSize = !(event[0].contentRect.height === 0 && event[0].contentRect.width === 0);
109143
if (!hasSize && !this.isAttachedToDom) {
@@ -113,6 +147,8 @@ export class VirtualHelperBaseDirective implements OnDestroy, AfterViewInit {
113147
// attached back now.
114148
this.restoreScroll();
115149
}
150+
151+
this.updateScrollbarClass();
116152
}
117153

118154
protected restoreScroll() {}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Component, ElementRef, HostBinding, Input, ViewChild, ViewContainerRef, ChangeDetectorRef, Inject, NgZone, DOCUMENT } from '@angular/core';
1+
import { Component, ElementRef, HostBinding, Input, ViewChild, ViewContainerRef, ChangeDetectorRef, Inject, NgZone } from '@angular/core';
22
import { VirtualHelperBaseDirective } from './base.helper.component';
3+
import { DOCUMENT } from '@angular/common';
34
import { PlatformUtil } from '../../core/utils';
45

56
/**
@@ -18,7 +19,13 @@ export class HVirtualHelperComponent extends VirtualHelperBaseDirective {
1819
@HostBinding('class')
1920
public cssClasses = 'igx-vhelper--horizontal';
2021

21-
constructor(elementRef: ElementRef, cdr: ChangeDetectorRef, zone: NgZone, @Inject(DOCUMENT) document, platformUtil: PlatformUtil) {
22+
constructor(
23+
elementRef: ElementRef,
24+
cdr: ChangeDetectorRef,
25+
zone: NgZone,
26+
@Inject(DOCUMENT) document: any,
27+
platformUtil: PlatformUtil
28+
) {
2229
super(elementRef, cdr, zone, document, platformUtil);
2330
}
2431

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, ElementRef, HostBinding, Input, ViewChild, ViewContainerRef,
2-
ChangeDetectorRef, OnDestroy, OnInit, Inject, NgZone, DOCUMENT } from '@angular/core';
2+
ChangeDetectorRef, OnDestroy, OnInit, Inject, NgZone} from '@angular/core';
33
import { VirtualHelperBaseDirective } from './base.helper.component';
4+
import { DOCUMENT } from '@angular/common';
45
import { PlatformUtil } from '../../core/utils';
56

67
@Component({
@@ -20,7 +21,13 @@ export class VirtualHelperComponent extends VirtualHelperBaseDirective implement
2021
@HostBinding('class')
2122
public cssClasses = 'igx-vhelper--vertical';
2223

23-
constructor(elementRef: ElementRef, cdr: ChangeDetectorRef, zone: NgZone, @Inject(DOCUMENT) document, platformUtil: PlatformUtil) {
24+
constructor(
25+
elementRef: ElementRef,
26+
cdr: ChangeDetectorRef,
27+
zone: NgZone,
28+
@Inject(DOCUMENT) document: any,
29+
platformUtil: PlatformUtil,
30+
) {
2431
super(elementRef, cdr, zone, document, platformUtil);
2532
}
2633

0 commit comments

Comments
 (0)