Ionic 4: How to overwrite style in Shadow elements that are not controlled by css variables?

半城伤御伤魂 提交于 2020-01-11 13:38:08

问题


I have to style a small app built with Ionic 4

The HTML is:

<div class="searchbar-wrapper">
  <ion-searchbar></ion-searchbar>
</div>
<div class="content-wrapper" #scrollingBlock>
  Content
</div>

I need to get rid of the shadow that Ionic by default adds to lots of its elements. I can see in Chrome devtools the searchbar like this:

    .searchbar-input.sc-ion-searchbar-md {

       ...
       -webkit-box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.2), 0 1px 5px 0 rgba(0,0,0,.12);
       box-shadow: 0 2px 2px 0 rgba(0,0,0,.14), 0 3px 1px -2px rgba(0,0,0,.2), 0 1px 5px 0 rgba(0,0,0,.12);
    }

In theory I just need to set the box-shadow and the webkit-box-shadow equal to none. However, I can not even overwrite the box-shadow because it is in a Shadow element and there is not variable that control the shadow. Something like --box-shadow.

The question:

How could I “kill” styles coming from Ionic that are not govern by a variable and are in shadow-elements?


回答1:


ion-searchbar as some other elements expose a method to access underlying "input" element - getInputElement. That should allow you to add styles if needed. Below is how you can do that with Ionic 4.11.5:

Template remains the same, but in ts file you could do:

import { IonSearchbar } from '@ionic/angular';
///
export class MySearchPage {

    @ViewChild(IonSearchbar, { static: false }) searchbar: IonSearchbar;
    ///
    ngAfterViewInit() {
        this.searchbar.getInputElement().then((searchbarInputElement)=>{
      searchbarInputElement.style.boxShadow = "none";
        // in case you need to style its parent as well:
        //searchbarInputElement.parentElement.style.boxShadow = "none";
    })
  };

}

Let me know if this helps.



来源:https://stackoverflow.com/questions/59215542/ionic-4-how-to-overwrite-style-in-shadow-elements-that-are-not-controlled-by-cs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!