Closed hakimio closed 9 months ago
Following line causes the issue: https://github.com/MurhafSousli/ngx-scrollbar/blob/c265c3c9f9889d0c9231f121db1faf12ac114419/projects/ngx-scrollbar/src/lib/scroll-viewport.ts#L112 It should prepend existing CSS classes instead of just replacing them.
I can use viewClass
input as a workaround, but being able to set the class directly on my div would be a lot more intuitive.
@MurhafSousli you fix did not work. For some reason +=
assignment of the class name doesn't seem to work.
Here is some debugging I did. Your code with some console logs:
console.log('class name:', this.nativeElement.className);
this.nativeElement.className += `ng-native-scrollbar-hider ng-scroll-viewport ${customClassName}`;
// Check if the custom viewport has only one child and set it as the content wrapper
if (this.nativeElement.firstElementChild) {
this.contentWrapperElement = this.nativeElement.firstElementChild;
this.contentWrapperElement.classList.add('ng-scroll-content');
}
console.log('new class name:', this.nativeElement.className);
Output:
class name: my-class-name
new class name: viewportng-native-scrollbar-hider ng-scroll-viewport
If I slightly modify the code:
console.log('class name:', this.nativeElement.className);
this.nativeElement.className = `${this.nativeElement.className} ng-native-scrollbar-hider ng-scroll-viewport ${customClassName}`;
// Check if the custom viewport has only one child and set it as the content wrapper
if (this.nativeElement.firstElementChild) {
this.contentWrapperElement = this.nativeElement.firstElementChild;
this.contentWrapperElement.classList.add('ng-scroll-content');
}
console.log('new class name:', this.nativeElement.className);
I get correct result:
class name: my-class-name
new class name: my-class-name ng-native-scrollbar-hider ng-scroll-viewport
Ok, found out what's the issue. You are missing a space. Following doesn't work:
this.nativeElement.className += `ng-native-scrollbar-hider ng-scroll-viewport ${customClassName}`;
but this works:
// space before ng-native-scrollbar-hider
this.nativeElement.className += ` ng-native-scrollbar-hider ng-scroll-viewport ${customClassName}`;
Reproduction
Steps to reproduce:
scrollViewport
directive to your owndiv
Expected Behavior
The class "foo-bar" is retained on the element.
Actual Behavior
scrollViewport
directive overrides all CSS classes andfoo-bar
class is removed.Environment