Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extend Polymer element and reuse its style?

I would like to create an advanced button based on 'paper-button'. However, if I simply do this

<link rel="import" href="../../bower_components/paper-button/paper-button.html">

<dom-module id="my-better-button">
  <script>
    Polymer({
      is: 'my-better-button',
      extends: 'paper-button'
    });
  </script>
</dom-module>

then button loses all its styles. Am I doing something wrong?

I am using Polymer 1.0.

like image 584
ironic Avatar asked Jul 20 '15 21:07

ironic


People also ask

What is the correct polymer element lifecycle?

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.

Does Polymer help in creating custom elements?

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.


2 Answers

To create your own element that uses another custom element, like paper-button, you'll want to make a wrapper.

<dom-module id="my-button">
  <template>
    <paper-button>
      <content></content>
    </paper-button>
  </template>
</dom-module>

This way whenever you use <my-button>Tap Me</my-button> it will really make a paper-button wrapped in my-button.

A good example is paper-input, which is basically a wrapper for iron-input. Take a look at it and see how the properties are passed down.

like image 163
Kevin Ashcraft Avatar answered Oct 19 '22 10:10

Kevin Ashcraft


You can extend custom elements through Behaviors like this:

<dom-module id="my-better-button">
  <script>
    Polymer({
      is: 'my-better-button',
      behaviors: [betterButton]
    });
  </script>
</dom-module>

Behaviors are like Mixins so they are, basically, a Javascript object. A behavior can define lifecycle callbacks, declared properties, default attributes, observers, and listeners.

I suggest you to watch this great video on this topic that explain very well how to use it: https://www.youtube.com/watch?v=YrlmieL3Z0k

like image 31
Granze Avatar answered Oct 19 '22 10:10

Granze