简体   繁体   English

在嵌套的mixin中设置Polymer CSS mixin

[英]setting a Polymer CSS mixin in a nested mixin

Polymer supports CSS mixins, which can be set like this: Polymer支持CSS mixins,可以像这样设置:

scope1 {
  --mixin1: {
    attr1: val1;
  };
}

and applied like this: 并应用如下:

scope2 {
  @apply --mixin1; /* sets attr1 */
}

Is there a way to set the value of a mixin from inside a mixin? 有没有办法从mixin中设置mixin的值? I tried this, but it doesn't work: 我试过这个,但它不起作用:

scope1 {
  --mixin1: {
    attr1: val1;
    --mixin2: {
      attr2: val2;
    };
  };
}

scope2 {
  @apply --mixin1; /* sets attr1 */
  @apply --mixin2; /* is attr2 set? */
}

A real case of why this would be useful: Say you have an app that uses several custom components based on paper-listbox and paper-item . 一个真实案例说明为什么这会有用:假设您有一个应用程序,它使用基于paper-listboxpaper-item多个自定义组件。 You would like to style all of the lists in your custom components with a different spacing and font. 您希望使用不同的间距和字体设置自定义组件中的所有列表的样式。 You could set the --paper-listbox and --paper-item mixins in a global scope. 您可以在全局范围内设置--paper-listbox--paper-item mixins。 But that would affect every occurrence of the two elements relying on defaults. 但这会影响依赖于默认值的两个元素的每次出现。 Instead in your custom components, you would simply @apply --custom-list; 而不是在自定义组件中,您只需@apply --custom-list; and set that mixin in a global :root {--custom-list: {/*set list style, set item style*/}; } 并将mixin设置为global :root {--custom-list: {/*set list style, set item style*/}; } :root {--custom-list: {/*set list style, set item style*/}; } . :root {--custom-list: {/*set list style, set item style*/}; }

Workaround: Instead of nesting mixins, refactor into selectors: 解决方法:不是嵌套mixins,而是重构为选择器:

scope1 {
  --mixin1: {
    attr1: val1;
  };
}
scope1 scope2 {
  --mixin2: {
    attr2: val2;
  };
}

In the use case above, instead of --custom-list with nesting: 在上面的用例中,而不是带嵌套的--custom-list

:root custom-list {
  --paper-input-container: {/*set list style*/};
  --paper-item: {/*set item style*/};
}

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

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