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,则会应用样式:

<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. 聚合物 2.x

差异

4

2 回答 2

7

要启用样式点,请使用 CSS variables/mixins

  1. 向元素的模板添加<style>标签:

    <dom-module id="polymer-button">
      <template>
        <style>
          .button {
            @apply --my-button-mixin;
          }
          .button.big {
            @apply --my-button-big-mixin;
          }
        </style>
        ...
      </template>
    </dom-module>
    
  2. 在容器元素中指定 mixin:

    <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>
    

    ...或在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>
    

密码笔(聚合物 1)

密码笔(聚合物 2)

于 2017-03-13T08:07:24.390 回答
5

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

<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;
}
于 2017-03-13T13:08:00.477 回答