Editors Note:This article was updated to remove references to deprecated components specifically polymer-ajax which has since been replaced by core-ajax. Thanks to Rob Dodson, developer advocate at Google, for the feedback. Building modern web applications requires a lot of tooling. This includes preprocessors, JavaScript frameworks, testing tools and a whole lot more. And as the complexities of these apps increases, so does the breadth of tooling and services needed to manage them. Wouldn't it be great if some things were simplified? Web Components aim to solve some of these complexities by providing a unified way to create new elements that encompass rich functionality without the need for all the extra libraries. Web components are comprised of four different specifications (Custom Elements, Templates, Shadow DOM and HTML imports) which are being fleshed out in the W3C. To bridge the gap and give developers access to this rich functionality now, Google has created the Polymer library which serves as a set of polyfills to bring the promise of Web Components to you today. Let's dive a little deeper into.
What Is Polymer
As I mentioned, the Polymer library is a set of polyfills that help you create Web Components on all major modern browsers. It provides the framework for defining, creating and rendering complex custom elements in a simplistic fashion similar to the tags you've grown up with. What I mean by this, is that it helps to simplify the way we use complex components by:
Encapsulating much of the complex code and structure
Allowing developers to use a simple-to-use tag style naming convention
Providing a suite of predefined UI elements to leverage and extend
But the important thing to remember, is that the framework itself is developed based on the direction of the individual specifications being vetted by the W3C, thus providing a foundation that should evolve with the direction of the main standards body. What this library can do, is allow us to create reusable components that work as true DOM elements while helping to minimize our reliance on JavaScript to do complex DOM manipulation to render rich UI results. Here's a quick example from the Polymer site. Say I wanted to render a working clock on my page. That would typically entail some heavy duty JavaScript code to do but by using Polymer, I can simply use the following syntax:
1
<polymer-ui-clock></polymer-ui-clock>
This looks like the HTML tag syntax we've all grown up with and is far easier to implement, read and maintain than some complex JavaScript code. And the end result looks like this: And since it's a normal element in the DOM, you can style it as well using CSS like this:
1
2
3
4
5
6
7
8
polymer-ui-clock {
width: 320px;
height: 320px;
display: inline-block;
background: url("../assets/glass.png") no-repeat;
background-size: cover;
border: 4pxsolidrgba(32, 32, 32, 0.3);
}
It's certainly not the prettiest clock, but that's not the point. The fact is that you can customize the component to your liking and then reuse it via an easier and more maintainable syntax.
Of the three, the easiest and recommended way is to use Bower because not only is it incredibly easy to do but Bower also manages any dependencies that Polymer might have. This means that if you choose to install a specific UI element which has a dependency on another one, Bower can handle that for you. Bower is installed as a Node Packaged Module, so you'll need to have Node.js installed. From the command line, type in the following:
1
npm install-g bower
This should pull Bower from the npm registry and install it so it's globally available to you. From there, subsequent Bower-based installs take the following form:
1
bower install
At a bare minimum, you're going to want to install Polymer's platform and core components since they provide the foundation for you to create and run your customer elements.
Polymer also comes with a rich, predefined set of elements that you can begin taking advantage of immediately. They consist of UI and non-UI based elements that provide functionality such as:
Animation
Accordions
Grid layout
Ajax capabilities
Menus
Tabs
And that's only scraping the surface. There's a lot already in there with full source code available to serve as a learning tool, as well as allow you to customize the capabilities to your needs. You have a choice in how to install these components. You can install everything or only those you want to use. To install everything, you type in:
1
2
bower installPolymer/core-elements
bower installPolymer/polymer-ui-elements
This is the kitchen sink approach and when you're starting out learning Polymer, it's probably easiest just to do that, to help you get a feel for what's available. Once you're more familiar with the framework, you can cherry-pick the individual components you'd like to use and install them like this:
1
bower installPolymer/polymer-ui-accordion
This is the beauty of using Bower. Every component comes with a bower.json configuration file that outlines its dependencies. So if you were installing the accordion component, looking at the config, we can see that it has dependencies on the main polymer component as well as the selector and collapsible components.
The key thing is that you don't have to worry about that because Bower manages that for you. This is why it's the preferred tool for installing Polymer. Installing via Bower will create a folder called bower_components in your project folder housing all of the stuff Polymer needs.
Making a New Polymer Element
The Polymer site pretty much nails the description of custom elements: “Custom Elements are the core building blocks of Polymer-based applications. You create applications by assembling custom elements together, either ones provided by Polymer, ones you create yourself, or third-party elements.” Polymer gives us the ability to create our own custom elements from scratch and even reuse other elements to extend our custom ones. This is done by first creating a template of the custom element. For all intents, this template is a combination of HTML, CSS and JavaScript and includes the functionality that will be available when you use the element. It's based off the WhatWG HTML Templates specification which is meant to provide native support for client-side templating. Let's look at this simple example of a Polymer template:
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</template>
</polymer-element>
This Element allows you to easily add Lorem Ipsum text into your code by simply using the following tag:
1
<lorem-element></lorem-element>
The first thing that needs to be included is Polymer core, which is the main API that allows you to define the Custom Elements:
The next step is defining the name of the new element using Polymer's polymer-element directive:
1
<polymer-elementname="lorem-element"noscript>
In this case, I've named my new element lorem-element. The name is a required attribute and it must contain a dash (“-”). From there we use the template directive to wrap the main body tags and code that will make up our new element. For this simple example, I grab Lorem Ipsum text and wrapped it with paragraph tags. That's it! My custom element is done and I can now use it.
Using Your New Polymer Element
Keep in mind that this component will be imported into other pages in your web app that may want to leverage it. This is possible because of Polymer's implementation of the HTML Imports specification that allows you to include and reuse HTML documents in other HTML documents. First, you need to include platform.js which provides the polyfill that mimicks the native APIs:
Next, we need to import our custom element into our web page:
1
<linkrel="import"href="lorem-element.html">
Once you've done this, your new custom element is now available allowing you to do something like this:
1
<div><lorem-element></lorem-element></div>
You also have the ability to fully style the element as well:
1
2
3
4
<style>
div { width: 300px;}
lorem-element { font: bold 16px cursive;}
</style>
This is a pretty basic example. Let's take it a step further.
Adding More Functionality to Your Element
If you remember, I mentioned how you can leverage existing elements to enhance your custom one. Let's look at an example of this. Suppose I wanted to have an element that went out to Reddit and grabbed data from one of the subreddits. I could take advantage of Polymer's existing Ajax component by including that in my custom element like this:
Notice that I'm importing the Polymer Ajax component and then dropping it into my template. This now makes it easy to reference my new element which makes the XHR call to pull back the JSON data and fill it into my paragraph tags with the subreddit's public description: “Things that make you go AWW! Like puppies. And bunnies... and so on... A place for really cute pictures, videos and stories!” The response is returned and Polymer establishes a two-way data binding that allows me to be able to use the data by wrapping it in double curly braces like this {{resp.data.public_description}}. This is cool but in most cases, we're not going to hardcode a URL for a specific resource. Let's expand this further by adding attributes to our custom element. Doing this is incredibly simple. First, you need to update the polymer-element directive to reflect the attributes you want for your custom element:
In this case, I want to be able to pass a subreddit to my element and have it pull back data based on that. I can now change the call to polymer-ajax like this:
Notice how I'm using Polymer's data binding capabilities to dynamically build the URL based on the attribute value of {{subreddit}}. Now, I can update how I reference my custom element to pass in the subreddit that I want:
1
<reddit-elementsubreddit="aww"></reddit-element>
The last thing I want to do is ensure there's a default value for my attribute so my code doesn't blow up. I do this by adding the following into my element template:
1
2
3
4
5
<script>
Polymer('reddit-element', {
subreddit: "aww"
});
</script>
This ensures that I'll always have a default value for my element's public attribute. Here's how the final code looks like:
And there's quite a bit more stuff you can do including adding custom callback handlers, managing events, setting up Mutation Observers to process changes to the DOM and much more.
The Future Is Here
Web Components are going to change the way we build web apps and Polymer is bringing this to us today. You now have the ability to create your own customized and maintainable components that can be catered to the specific needs of your application. These components can be easily shared allowing other teams or the community to easily share and benefit from these encapsulated building blocks.
As the music begins to play, a soft breeze fills the space with the undeniable smell of magnolia blossoms. She gracefully enters and begins a “tree pose” with her hands in Namaste form. Our female figure is inspired by the Indian culture for its gorgeous use of color and the grace of its yoga formations. I hope you’ll enjoy the journey as we retrace, step by step to enlightenment. 1. Becoming Flesh and Blood Step 1 Our first step will be to bring in our sketch. I've scanned her in at 300 dpi to catch as much of the detail as possible. I'm not as picky about my choice of initial drawing tools, and this particular project was completed with a mechanical pencil. We'll be working with a clean line quality from Adobe Photoshop and now we can begin to work. I'm working in Adobe Illustrator at a size of 8.5 x 11 inches for this specific project. When I'm ready to start my layers, I'll label my first layer as "Sketch" and lock the layer to make su...
In this tutorial, we are going to design a set of icons in Adobe Photoshop. An icon set needs to have the same background and theme. For learning purposes, we are going to design a sun icon, an ice flake icon, and an RSS icon. Let's get started. 1. Preparing the Canvas Step 1 Start by making a new file with size 350 px × 350 px . Click the small box next to the Background Contents option to change the new canvas color. Step 2 In the Color Picker dialog box, select grey ( #e0e0e2 ) for the canvas background color. Step 3 It is always a good idea to keep your work structured from the start. Make a new layer group and name it sun . This is where we will place all layers used in the sun icon. 2. Designing the Icon Base Step 1 Use the Rounded Rectangle Tool to draw a rectangle with size 83 px × 64 px and 8 px radius. To get accurate results, use the Properties panel. In this panel, you can simply enter the exact siz...
Most Android devices don't have a physical keyboard. Instead, they rely on a virtual or soft keyboard to accept user input. If you're into Android personalization, knowing how to build a custom, soft keyboard can take your hobby to a whole new level. Using the Android SDK, you can quickly create a soft keyboard with surprisingly few lines of code, because the SDK takes care of a lot of the low level tasks, such as recognizing key touches, drawing the keyboard, and establishing connections between the keyboard and input fields. In this tutorial, you will learn how to create a fully functional soft keyboard that can serve as your Android device's default keyboard. 1. Prerequisites You will need the Eclipse ADT Bundle installed. You can download it from the Android Developer website. 2. Create a New Project Fire up Eclipse and create a new Android application. Call this application, SimpleKeyboard . Make sure you choose a unique package n...
Comments
Post a Comment