Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scrollViewport directive removes custom CSS classes applied on the viewport element #505

Closed
hakimio opened this issue Jul 17, 2023 · 4 comments · Fixed by #506
Closed

scrollViewport directive removes custom CSS classes applied on the viewport element #505

hakimio opened this issue Jul 17, 2023 · 4 comments · Fixed by #506
Labels

Comments

@hakimio
Copy link

hakimio commented Jul 17, 2023

Reproduction

Steps to reproduce:

  1. Assign scrollViewport directive to your own div
  2. Set some CSS class on that element:
<ng-scrollbar>
  <div 
    scrollViewport
    class="foo-bar"
  >
     <!-- CONTENT -->
  </div>
</ng-scrollbar>

Expected Behavior

The class "foo-bar" is retained on the element.

Actual Behavior

scrollViewport directive overrides all CSS classes and foo-bar class is removed.

Environment

  • Angular: 16.1
  • ngx-scrollbar: 13.0.1
  • Browser(s): Chrome 114
  • Operating System: Windows 10
@hakimio
Copy link
Author

hakimio commented Jul 17, 2023

Following line causes the issue:

this.nativeElement.className = `ng-native-scrollbar-hider ng-scroll-viewport ${customClassName}`;

It should prepend existing CSS classes instead of just replacing them.

@hakimio
Copy link
Author

hakimio commented Jul 17, 2023

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 added a commit that referenced this issue Jul 19, 2023
@MurhafSousli MurhafSousli linked a pull request Jul 19, 2023 that will close this issue
MurhafSousli added a commit that referenced this issue Jul 19, 2023
MurhafSousli added a commit that referenced this issue Jul 19, 2023
@hakimio
Copy link
Author

hakimio commented Jul 19, 2023

@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

@hakimio
Copy link
Author

hakimio commented Jul 19, 2023

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}`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants