Mobile web application with Polymer and Material Design (quick-start)
Weâll see how to create a mobile webapp for Android with Polymer (and a bit of âMaterial Designâ).
Prepare the project
Remark: you need:
- bower http://bower.io/
- http-server https://www.npmjs.org/package/http-server or any other http server
In a directory (ie: my-project
) create 2 files:
.bowerrc
bower.json
with these contents:
.bowerrc
bower.json
And type (in terminal or console application) : bower install
, waiting âŠ, youâre ready!
Step 01: Our first component
Specifications:: âI want a web app with a main panel and a menu panelâ
first create (at the root of my-project
) a new html file named index.html
:
So now, if you have followed, we will create a main-screen
component.
main-screen component
Firstly, create a components
directory into js
directory, and a new html file named main-screen.html
inside components
directory:
My sample code is inspired from Polymer documentation http://www.polymer-project.org/docs/elements/core-elements.html# core-scaffold : core-scaffold
provides general application layout, introducing a responsive scaffold containing a header, toolbar, menu, title and areas for application content.
Note: keyword tool
in <span tool>
tag allows the itle bar of the right panel.
You can test your web app now. Run your http sever (in my case, I run http-server
from my project directory and I open http://localhost:8080/ with Chrome). You get this:
If you reduce width of browser window, left panel disappear, and a button appears inside the header:
Click on the button, the left panel appears again (click outside the left panel to hide it):
But itâs ugly!. We will have to make it a little prettier, this is âstep 02â.
You can find source code of this first step, here: https://github.com/metals/copper/tree/master/creation-steps/step-01.
Step 02: something prettier
We will try to do something nicer. I will use the colors shown here: http://www.google.com/design/spec/style/color.html# color-ui-color-palette
Beautify the left panel
At the same location of your component (main-screen.html
), create a css file main-screen.css
. Add a reference to this file in your component code:
Remark: you have to put <link rel="stylesheet" href="main-screen.css">
inside <template></template>
.
So, add some code to our css file:
Add content
class to the div âcontentâ in main-screen.html
:
And test your web app again:
Itâs better! ;)
Beautify the right panel
core-toolbar
and core-header-panel
are âdifficultâ to find because they are inside the Shadow DOM.
If you want to style these elements, you have to use this syntax: ::shadow
with your selector (try document.querySelectorAll("core-scaffold::shadow core-toolbar")
with the browser console).
(See this post: http://stackoverflow.com/questions/24594333/polymer-core-scaffold-coloring-and-paper-button)
Then, add this to main-screen.css
:
And test your web app again:
Nicer, no?
But, there is not the shadow effect on the title bar of the right panel!!!.
Add this <link rel="import" href="js/vendors/paper-shadow/paper-shadow.html">
to the index.html
file and update the main-screen.html
file with <paper-shadow z="1"></paper-shadow>
like that:
Itâs âmore Material Designâ ;)
Before we go any further, letâs change two or three things. I want something âmore reusableâ. Change the code of the component (main-screen.html
):
Add attributes for title bars: attributes="menuTitle mainTitle"
Value of menuTitle
will be displayed here: {{menuTitle}}
Value of mainTitle
will be displayed here: {{mainTitle}}
And now you can (re)use your component like that (in index.html
):
You can find source code of this second step, here: https://github.com/metals/copper/tree/master/creation-steps/step-02.
Step 03: and now, some action
Now I want to display different content in the right panel, when I click on the items in the left pane. For this I will use the component core-pages
(see http://www.polymer-project.org/docs/elements/core-elements.html# core-pages).
We will once again change our component.
Firstly, add this core-pages
reference to index.html
: <link rel="import" href="js/vendors/core-pages/core-pages.html">
.
Then, go back to our component (main-screen.html
) to change the code:
core-menu
changes:
- when an item is selected, we call
selectAction
method ofmain-screen
:on-core-select="{{selectAction}}
- I added a custom attribute
num
to eachcore-item
:num="0"
,num="1"
,num="2"
Add core-pages
component:
- first item is selected :
selected="0"
- identify the component with
id="pages"
Add some code:
- add reference to
pages
:this.pages = this.$.pages;
(this.pages
becomes a property ofmain-screen
component) - get the value (
num
attribute) of the selected item:selectedItem.attributes.num.nodeValue
- select the âwantedâ page:
this.pages.selected = selectedItem.attributes.num.nodeValue;
You can try:
Easy! :)
You can find source code of this third step, here: https://github.com/metals/copper/tree/master/creation-steps/step-03.
And now you can test it directly on your mobile: http://metals.github.io/copper/.
Warning: Itâs better with Chrome for Android, especially about styling, because there are no ShadowDom support on Safari Mobile (see http://caniuse.com/shadowdom).
Tweet