如何使用外部样式设置自定义 Polymer 元素的内部元素的样式?
How to style inner elements of custom Polymer element using external styles?
无法使用带有 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 中应用样式 if 我们使用 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>
有没有办法使用 external styles 来 select/style 这些内部元素?
下面是我上面所说的按出现顺序的直观表示:
- 聚合物 1.x 使用阴影 DOM
- Polymer 1.x 使用 ShadyDOM
- 聚合物2.x
要启用样式点,use CSS variables/mixins。
向您的元素模板添加 <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:
<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>
或者,您可以在自定义元素 <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;
}
无法使用带有 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 中应用样式 if 我们使用 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>
有没有办法使用 external styles 来 select/style 这些内部元素?
下面是我上面所说的按出现顺序的直观表示:
- 聚合物 1.x 使用阴影 DOM
- Polymer 1.x 使用 ShadyDOM
- 聚合物2.x
要启用样式点,use CSS variables/mixins。
向您的元素模板添加
<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:
<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>
或者,您可以在自定义元素 <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;
}