如何使用外部样式设置自定义Polymer元素的内部元素的样式? [英] How to style inner elements of custom Polymer element using external styles?

查看:83
本文介绍了如何使用外部样式设置自定义Polymer元素的内部元素的样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使用带有Polymer 1.x或2.x的Shadow DOM设置元素的样式.考虑Polymer 2.0中的以下自定义元素:

Unable to style an element using Shadow DOM with Polymer 1.x or 2.x. Consider the following custom element in 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定义的样式被 not 应用于阴影元素;但是,在Polymer 1.x中,如果我们使用ShadyDOM,则会应用样式:

The styles defined in the demo for .button and .button.big are not applied to the shadow element; however, in Polymer 1.x the styles are applied if we use 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>

是否可以使用外部样式?

下面是我按照外观顺序所说的视觉表示:

Below is a visual representation of what I said above in order of appearance:

  1. 使用Shadow DOM的聚合物1.x
  2. 使用ShadyDOM的聚合物1.x
  3. 聚合物2.x

推荐答案

要启用样式点,请

  • 在元素模板中添加<style>标签:

    <dom-module id="polymer-button">
      <template>
        <style>
          .button {
            @apply --my-button-mixin;
          }
          .button.big {
            @apply --my-button-big-mixin;
          }
        </style>
        ...
      </template>
    </dom-module>
    

  • 在容器元素中指定mixin:

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

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

  • codepen(聚合物1)

    codepen(聚合物2)

    这篇关于如何使用外部样式设置自定义Polymer元素的内部元素的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆