Polymer @import theme file with :host in styles has no affect

淺唱寂寞╮ 提交于 2019-12-18 07:00:23

问题


Back with another Polymer question, I have a Polymer/Electron app that I'm trying to style.

I want to create a theme.css that contains a :host block with my entire theme in it that I can then import into my modules stylesheet but I've tried a few different things and tried finding anything in the documentation to no avail.

So far, I have tried in, and outside of the <template> definition:

<link rel="stylesheet" href="./account-list.css"> with an @import
<style>@import 'my-theme.css';</style> just above my <link>
:root instead of :host in my theme.css

But neither seem to work, the theme.css is definitely being requested but has no affect on the module's style.

Is there anyway to have a theme like this for Polymer, I really don't want to have a build step.


回答1:


There's a new concept called style module (actually a dom-module element behind the scene) introduced in Polymer 1.1 (read it here) and the old way of including external stylesheets has been deprecated (read it here).

Basically, you need to create an html file like how you normally create an element to store your styles. The id defines the name of this file that will be referenced later on.

<!-- shared-styles.html -->
<dom-module id="shared-styles">
  <template>
    <style>
      .red { color: red; }
    </style> 
  </template>
</dom-module>

Then obviously you need to import this file in your page.

<link rel="import" href="shared-styles.html">

Now, there are two scenarios.

  1. If you are using custom-style at the document level, you need to include the style module you previously defined like this -

    <style is="custom-style" include="shared-styles"></style>

  2. If you simply want to include the style module inside one of your elements, do this -

    <dom-module id="my-element"> <style include="shared-styles"></style>

Have a look at this plunker that demonstrates both scenarios.

Keep in mind that in your particular example, since you are using :host, I assume you will go with scenario 2. So this plunker should be a bit more clearer.




回答2:


Using dom-module concept, and in order to use a external third party I did the next and it is working, but probably is not a Polymer best practice.

Dom module with 3rd party css (third-party-styles.html)

<dom-module id="third-party-styles">
    <link rel="stylesheet" href="../bower_components/thirdParty/external.css">
</dom-module>

I created a container (elements.html) where import all needed html modules, and there I registered the third party style module and my module

<link rel="import" href="third-party-styles.html">
<link rel="import" href="my-module.html">

And I added the elements.html in the head of my index.html

<head>
    ...
    <link rel="import" href="elements.html">
<head>
<body>
    <my-module></my-module>
</body>

In my Polymer Element (my-module.html)

<link rel="import" href="third-party-styles.html">
<dom-module id="my-module">
    <style include="third-party-styles"></style>
    <template>
        <p class=".thirdPartyClass">Content with third party css rule</p>
    </template>
</dom-module>

any feedback?



来源:https://stackoverflow.com/questions/32298500/polymer-import-theme-file-with-host-in-styles-has-no-affect

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