This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Polymer Custom Elements: Extend Native HTML Elements

3:11 JavaScript lesson by

Polymer allows us to extend a native HTML elements UI or functionality. In this lesson we'll use a button as our custom element basis and add new UI and functionality to it that can be added to any button in our application.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Polymer allows us to extend a native HTML elements UI or functionality. In this lesson we'll use a button as our custom element basis and add new UI and functionality to it that can be added to any button in our application.

Avatar
Hack to the Future 1

i'm getting a JS warning in Firefox: "mismatched tag. Expected: ."
it all works fine but wondering about this warning?

In reply to egghead.io

In this lesson, we're going to talk about extending native HTML elements using Polymer.

To get us started, I'm going to create a button here, and I'm going to give it an inner HTML of "Hello, world." This is going to be our button that we want to extend. Then we're going to have another button here that we're not going to extend, just so that we can see the difference between the two.

The way we instantiate this is with an isAttribute. I'm going to set that to my button. We're going to go ahead and HTML import our mybutton.html, and save that. We can see that we have two buttons here, neither of them is doing anything special, they're just standard buttons.

We're going to jump over here to our mybutton.html. We're going to set up our myButton element. The way that we get to the inner content or nested children of a element is with this content tag. This is very similar to React's this.props.children or Angular's transclusion or Ember's yield. We're saying at this point just let any nested elements pass on through into out template.

Now, at this point, since we have no other HTML around that content tag, we can reference whatever's inside of that content tag using this as our context. Here in our Polymer script, we're going to go ahead and add an attribute called extends. We're going to set that to the element we want to extend. In our case, that's going to be button. We're saying that this element will extend all buttons.

We're

To illustrate that, we're going to go ahead and say, on the created life cycle method,just going to add a style of color = red. Go ahead and save that, and take a look at it in a browser. We can see that in fact the extended button has a color style of red while the not extended button does not.

We're going to add a little functionality here. I'm just going to create a custom method here called clickHandler, going to be a function.

All we're going to do is say this.text.content so the inner content of our passed through element. Again, this is going to reference whatever we've passed into the content tag. This.text.content equals this.text.content.split, just split that on nothing. Then we'll reverse that. Then we'll join that back together. That'll cause a reverse of whatever the content is.

Up here in our created method or in our created life cycle, we're going to go ahead and add an event listener to let's say on the click of this. Again, this is referencing the button that's passed into our content. We're simply going to call this.clickHandler. Save that.

On the right here, now when we click on that, it's going to reverse the inner HTML or the text content of our button. So we have extended that button using Polymer.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?