Given an element like:
<polymer-element name="custom-element">
<template>
<style>
#container {
color: red;
}
</style>
<div id="container" on-click="{{clickContainer}}">
... lots of other stuff here ...
</div>
</template>
<script>
Polymer('custom-element', {
clickContainer: function() {
}
});
</script>
</polymer-element>
I'd like to have another element that wraps the first:
<polymer-element name="my-custom-element" extends="custom-element">
<!-- extra styling -->
<script>
Polymer('my-custom-element', {
clickContainer: function() {
this.super();
}
});
</script>
</polymer-element>
My problems:
<content select=".stuff">
but for the base's shadow markup.Polymer element lifecycle. Polymer elements follow the standard lifecycle for custom elements. The custom element spec provides a set of callbacks called "custom element reactions" that allow you to run user code in response to certain lifecycle changes.
Polymer adds a set of features to the basic custom element: Instance methods to handle common tasks. Automation for handling properties and attributes, such as setting a property based on the corresponding attribute. Creating shadow DOM trees for element instances based on a supplied template.
html, polymer. html are the imports used to define the Polymer elements. Import technically translates to receiving data into one program from another.
- What's the best way to specify additional styling ?
my-custom-element
), as usual. <shadow></shadow>
element where you want the superclass template to appear.:host::shadow .someclass { ... }
See example below.
- Can I wrap the base element in additional markup (like another container) ?
Yes, you can put whatever markup you want around the <shadow></shadow>
.
<div>
<shadow></shadow>
</div>
- Can I select elements from the base element? Something like
<content select=".stuff">
but for the base's shadow markup.
No. You cannot project like that (it's the reverse direction from all other projections).
If you really want to cherry-pick nodes out of the older shadow-root, this can be done in code by pulling nodes directly out of this.shadowRoot.olderShadowRoot
. But this can be tricky because the superclass may have expectations about the structure.
Example code:
<polymer-element name="my-custom-element" extends="custom-element">
<template>
<style>
/* note that :host::shadow rules apply
to all shadow-roots in this element,
including this one */
:host::shadow #container {
color: blue;
}
:host {
/* older shadow-roots can inherit inheritable
styles like font-family */
font-family: sans-serif;
}
</style>
<p>
<shadow></shadow>
</p>
</template>
<script>
Polymer('my-custom-element', {
clickContainer: function() {
this.super();
}
});
</script>
</polymer-element>
ProTips:
olderShadowRoot
will exist whether or not you include the <shadow></shadow>
tag, but it will not be part of the rendered DOM unless you do.olderShadowRoot(s)
from being created you can override parseDeclarations
(source). Any of parseDeclarations
, parseDeclaration
, fetchTemplate
can be overidden for various effects.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With