Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import external stylesheets in polymer 3

Is there a way to import external css files that only affects the shadow DOM? I am working with sass and creating the css files automatically, so any tricks using javascript imports can't be done.

Right now, what I have is:

static get template() {
return html`
  <style>
  :host {
    display: block;
  }
  </style>
  ....
}

In Polymer 2, it was possible to do something like:

 <dom-module id="my-app">
   <link rel="stylesheet" href="style.css">
   <template></template>
 </dom-module>

Is there a Polymer 3 way of acheving the same thing?

like image 531
Valentin Sánchez Avatar asked May 27 '18 03:05

Valentin Sánchez


2 Answers

this works great for me!

return html`
      <link rel="stylesheet" href="//cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
      <style>

/* I had to put !important to override the css imported above. */
      </style>

      <divclass="blablabla"></div>
    `;
like image 102
Emerson Bottero Avatar answered Oct 04 '22 23:10

Emerson Bottero


You can use variables in html-tag, like this:

import { htmlLiteral } from '@polymer/polymer/lib/utils/html-tag.js';

import myCSS from "style.css";
let myCSSLiteral = htmlLiteral(myCSS);
...
class MyElement extends PolymerElement {
  static get template() {
    return html`<style>${myCSSLiteral}</style>...`;
    ...
  }
  ...
}

Please note: You have to convert variable from string to a htmlLiteral for using it in html-tag, though I do not know why Polymer does not support raw string variable directlly. good luck!

like image 30
Imskull Avatar answered Oct 04 '22 22:10

Imskull