2

I have a dropdown component in angular 12. It is shows the selectize like behavior. It is implemented as

 <ngx-select-dropdown
        [config]="config"
        [options]="dropdownOptions"
        [(ngModel)]="dataModel"
        [multiple]="false"
      ></ngx-select-dropdown>

by using https://github.com/manishjanky/ngx-select-dropdown

After rendering it generates markup like

code generated by the dropdown

Now, I want to modify the class of the button (highlighted in yellow color) rendered in markup but I don't have direct access to the markup of the button How can we do that ?

  • Have you tried assigning an id to ngx-select-dropdown, then getting the first button? Like: `let button = document.getElementById('yourDropdownId').getElementsByTagName('button')[0]`, then set whatever css styles you want - by directly assigning styles to `button` or adding a specific css class to it. Do that in a code block after the dropdown is populated. – Misha Mashina Mar 17 '22 at 06:05

1 Answers1

1

You can see this thread for some older solutions: How to style child components from parent component's CSS file? keep in mind ::ng-deep is deprecated.

I prefer the following and I will probably add it to that thread.

The only way to get your css to permeate down to a child is to make the css file global. You can use ViewEncapsulation.None to make the css of the entire component global, but that's pretty messy. I prefer to just put this global class inside the global styles.css, or you can make a new file and add it to the styles array in angular.json.

So the question is what do we use as the selector? We can't just select button as it will override all the buttons in the project, and we don't want to use .ngx-dropdown-button alone because we might want different styles for different dropdowns. We want to add a specific class inside the component first. Make sure it is unique, probably best to use the component's name.

 <ngx-select-dropdown
        class="my-component-name-dropdown"
        [config]="config"
        [options]="dropdownOptions"
        [(ngModel)]="dataModel"
        [multiple]="false"
      ></ngx-select-dropdown>

Now we can select this specific dropdown from our global styles file. Though, we need to make sure we achieve a high enough specificity to override the default styles. The quick and dirty way is to just use !important next to every property, but then you leave no way to override your css. A better way is to repeat a class name until we achieve the desired specificity. I found I needed to repeat a class three times here.

In Global Styles File

.my-component-name-dropdown
  .ngx-dropdown-button.ngx-dropdown-button.ngx-dropdown-button {
  color: red;
}

Or the quick and dirty way

.my-component-name-dropdown .ngx-dropdown-button {
  color: red !important;
}

Of course if you want to override all ngx-dropdown-buttons, the unique class is not necessary and you can just use the .ngx-dropdown-button class.

Note: Many UI components have a class option in the config / options object to let you override the default css, you may want to request that the devs add this.

Chris Hamilton
  • 9,252
  • 1
  • 9
  • 26