如何使用外部样式为自定义的Polymer元素内部元素设置样式?

4
无法使用Polymer 1.x或2.x的Shadow DOM样式化元素。考虑以下在Polymer 2.0中的自定义元素:
<link rel="import" href="../polymer/polymer.html">

<!--
`semantic-ui-button`


@demo demo/index.html
-->

<dom-module id="polymer-button">
  <template>
    <div class$="button {{size}}">{{label}}</div>
  </template>

  <script>
    class MyElement extends Polymer.Element {
      static get is() {
        return 'polymer-button';
      }
      static get properties() {
        return {
          label: {
            type: String,
            value: 'polymer-element'
          },
          size: { type: String }
        };
      }
    }

    window.customElements.define(MyElement.is, MyElement);
  </script>
</dom-module>

在演示中:
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

    <title>polymer-element demo</title>

    <script src="../../webcomponentsjs/webcomponents-lite.js"></script>

    <link rel="import" href="../../iron-demo-helpers/demo-pages-shared-styles.html">
    <link rel="import" href="../../iron-demo-helpers/demo-snippet.html">
    <link rel="import" href="../polymer-element.html">

    <style is="custom-style" include="demo-pages-shared-styles"></style>
    <style>
      body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
      .button {
        background: #ccc;
        border-radius: 4px;
        color: #444;
      }
      .button.big {
        font-size: 1rem;
        padding: 6px;
      }
    </style>
  </head>
  <body>
    <div class="vertical-section-container centered">
      <h3>Basic polymer-element demo</h3>
      <demo-snippet>
        <template>
          <polymer-button label="Demo"></polymer-button>
        </template>
      </demo-snippet>
    </div>
  </body>
</html>

在演示中定义的.button.button.big样式并未应用于阴影元素;然而,在Polymer 1.x中,如果我们使用ShadyDOM,则会应用这些样式(仅限Polymer 1.x)

<link rel="import" href="../polymer/polymer.html">

<!--
`polymer-button`


@demo demo/index.html
-->

<dom-module id="polymer-button">
  <template>
    <div class$="button {{size}}">{{label}}</div>
  </template>

  <script>
    Polymer({

      is: 'polymer-button',

      properties: {
        label: { type: String }
      },

    });
  </script>
</dom-module>

有没有一种方法可以使用外部样式选择/样式化这些内部元素?

下面是我上面说的以出现顺序为基础的可视化表示:

  1. 使用 Shadow DOM 的 Polymer 1.x
  2. 使用 ShadyDOM 的 Polymer 1.x
  3. Polymer 2.x

differences

2个回答

7
为了启用点的样式,请使用CSS变量/混合
  1. Add a <style> tag to your element's template:

    <dom-module id="polymer-button">
      <template>
        <style>
          .button {
            @apply --my-button-mixin;
          }
          .button.big {
            @apply --my-button-big-mixin;
          }
        </style>
        ...
      </template>
    </dom-module>
    
  2. Specify the mixin in a container element:

    <dom-module id="x-foo">
      <template>
        <style>
          polymer-button {
            --my-button-mixin: {
              background: red;
              color: white;
            };
          }
        </style>
        <polymer-button label="Red button"></polymer-button>
      </template>
    </dom-module>
    

    ...or in index.html:

    <body>
      <custom-style>
        <style>
          polymer-button {
            --my-button-mixin: {
              background: red;
              color: white;
            };
          }
        </style>
      </custom-style>
      <polymer-button label="Red button"></polymer-button>
    </body>
    

代码笔 (Polymer 1)

代码笔 (Polymer 2)


1
@djthoms,使用Webcomponents polyfills实际上可以在IE/Edge上支持。 屏幕截图 - tony19

5

另外,您还可以在自定义元素的 <template> 中添加一个带有 @import url 规则的 <style> 元素来导入外部样式表:

<template>
    <style>
         @import url( external.css )
    </style>
    <div class$="button {{size}}">{{label}}</div>
</template>

在您的CSS样式表(例如:external.css)中,您可以定义标准的CSS:
.button {
    background: #ccc;
    border-radius: 4px;
    color: #444;
}
.button.big {
    font-size: 1rem;
    padding: 6px;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接