简体   繁体   English

Polymer中的Mixin,类和自定义元素

[英]Mixin, classes and custom elements in Polymer

I create a file called my-mixins.html containing: 我创建一个名为my-mixins.html的文件,其中包含:

<link rel="import" href="../polymer/polymer.html">
<style is="custom-style">
  :root {
    --red: {
      color: red;
    };
  }

  // This won't work
  .green: {
    color: green;
  }
</style>

Then I create an element my-element.html : 然后创建一个元素my-element.html

<link rel="import" href="bower_components/my-mixins/my-mixins.html">

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

<dom-module is="my-element">
  <style>
    .red {
      @apply(--red);
    }
    ...
  </style>

  <p class="red">This is red</p>
  <p class="green">This is not green</p>

  <script>
    Polymer({
      is: 'my-element'
    });
  </script>

</dom-module>

While --red worked (as it's supposed to), .green didn't. 虽然--red有效(如预期),但.green无效。 I realise this is to make sure that styling doesn't spill etc. But... what's the actual rule here? 我意识到这是要确保样式不会溢出等。但是...这里的实际规则是什么? How come --red was set and it's available to the module, whereas green isn't? --red是如何设置的,并且可用于模块,而green不是?

The answer to my specific question was here: 我的具体问题的答案在这里:

https://www.polymer-project.org/1.0/docs/devguide/styling#custom-style https://www.polymer-project.org/1.0/docs/devguide/styling#custom-style

Specifically: 特别:

Polymer provides a 聚合物提供了 custom element for defining styles in the main document that can take advantage of several special features of Polymer's styling system: 用于定义主文档中样式的自定义元素,可以利用Polymer样式系统的一些特殊功能:

Document styles defined in a custom-style are shimmed to ensure they do not leak into local DOM when running on browsers without native Shadow DOM. 对自定义样式中定义的文档样式进行填充以确保在没有本机Shadow DOM的浏览器上运行时它们不会泄漏到本地DOM中。

Custom properties used by Polymer's shim for cross-scope styling may be defined in an custom-style. 可以用自定义样式定义Polymer的填充垫用于跨范围样式的自定义属性。 Use the :root selector to define custom properties that apply to all custom elements. 使用:root选择器定义应用于所有自定义元素的自定义属性。

So, the docs explain that the :root selector specifically is to be used to set cross-scope custom properties... 因此,文档解释了:root选择器专门用于设置跨范围自定义属性...

I believe it works that way to provide the option to tweak internal styles using CSS custom properties as style placeholders. 我相信它可以通过CSS自定义属性作为样式占位符来提供调整内部样式的选项。

Whereas the reason .green is not leaking is due to the scoped CSS feature of Shadow DOM. .green不泄漏的原因是Shadow DOM具有范围的CSS功能。

You can read more about stylehooks in Eric Bidelman's Shadow DOM v1 primer 您可以在Eric Bidelman的Shadow DOM v1入门中阅读有关样式钩子的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM