angularswiper.jsshadow-dom

How to inject style in Swiper 9 webcomponent loaded in an Angular 15 application?


I'm trying to stylize the Swiper 9 web component slider loaded in an Angular 15 application. I followed the official documentation. In particular, I would like to inject styles into the shadow DOM of the slider component through the injectStyles parameter.

My Swiper config is :

public config: SwiperOptions = {
    modules: [Navigation],
    navigation: true,
    slidesPerView: 1,
    injectStyles: [
      `
        :host .swiper-wrapper {background-color: red;}
        :root {--swiper-theme-color: red;}
      `,
    ],
};

However, none of these styles are being injected into the shadow DOM. I noticed that certain parameters don't work properly, such as pagination: false, which causes the slider to crash. (ViewEncapsulation.None doesnt seem to affect the component neither).

Is this bug relative to the framework or to the developer ?

Reproduction link : StackBlitz


Solution

  • You need to add init="false" to your component, that will make your injected styles work.

    <swiper-container swiperElement [config]="config" init="false">
      <swiper-slide *ngFor="let i of [1,2,3]">
        Slide {{i}}
      </swiper-slide>
    </swiper-container>
    

    Reproduction link