Top Banner
Polymer 1.0 1
61

Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Apr 16, 2017

Download

Technology

Sfeir
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Polymer 1.0

1

Page 2: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 3: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Moi...

Page 4: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Des composants

Page 5: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 6: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 7: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 8: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

@polymer #itshackademic

Page 9: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 10: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<paper-tabs>

<paper-tab>KNOWLEDGE</paper-tab>

<paper-tab>HISTORY</paper-tab>

<paper-tab>FOOD</paper-tab>

</paper-tabs>

Page 11: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Web Components

Page 12: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Custom Element

Page 13: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Shadow DOM

Page 14: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

HTML Imports

Page 15: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Template

Page 16: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 17: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 18: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Polyfills...

Page 19: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Photo: http://bit.ly/1CeBPyN

Page 20: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 21: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Polymer elements

Page 22: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<paper-checkbox></paper-checkbox>

<paper-input floatinglabel

label="Type only numbers... (floating)"

validate="^[0-9]*$"

error="Input is not a number!">

</paper-input>

Page 23: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<paper-header-panel>

<paper-header-panel flex>

<paper-toolbar>

<paper-icon-button icon=“menu">

</paper-icon-button>

<div>MY APP</div>

</paper-toolbar>

<div>…</div>

</paper-header-panel>

MY APP

Page 24: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<paper-drawer-panel>

<paper-drawer-panel>

<div drawer> Drawer panel... </div>

<div main> Main panel... </div>

</paper-drawer-panel>

Page 25: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Créer un composant

Page 26: Polymer 1.0 par Cyril Balit au DevFest Paris 2016
Page 27: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<html>

<head>

<script src="webcomponents/webcomponents-lite.min.js"></script>

</head>

<body>

</body>

</html>

Polyfills

Page 28: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<html>

<head>

<script src="webcomponents/webcomponents-lite.min.js"></script>

<link rel=”import” href="paper-card.html">

</head>

<body>

</body>

</html>

Importer l’élément

Page 29: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<html>

<head>

<script src="webcomponents/webcomponents-lite.min.js"></script>

<link rel=”import” href="paper-card.html">

</head>

<body>

<paper-card heading=”hello my friend”>

<img src=”avatar.svg”>

</paper-card>

</body>

</html>

Utiliser l’élément

Page 30: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<dom-module id="paper-card"> <style> :host { border-radius: 2px; } .card-header ::content img { width: 70px; border-radius: 50%; } paper-material { border-radius: 2px; } </style> <template> <paper-material elevation="{{elevation}}" animated on-tap=”tapAction”> <div class="card-header layout horizontal center”> <content select="img"></content> <h3>{{heading}}</h3> </div> <content></content> </paper-material> </template></dom-module><script> Polymer({ is:'paper-card', properties: { heading: {type: String, reflectToAttribute: true, value: “”}, elevation: {type: Number, reflectToAttribute: true, value: 1} }, attached: function() { /* your initialisations here */ }, tapAction: function (e) { /* your event handling here */ } });</script>

script: called on init

content injection using select= polymer expression

injecting all other content

scrip

t / p

roto

type

the name must have a “-”

event binding

published attributes

data binding

tags

/ sh

adow

DO

M a

nd s

tyle

Page 31: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

MyElement = Polymer({

is: 'my-element',

created: function() {},

attached: function() {},

detached: function() {},

attributeChanged: function(name, type) {}

ready: function() {}

});

1. created callback

2. local DOM initialized

3. ready callback

4. attached callback

Lifecycle

Page 32: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Distribution du DOM : la composition

Page 33: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Light Dom and Shadow Dom

Page 34: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<my-component> <span class="title">Hello too</span> <span>Bla Bla Bla</span></my-component>

Dom & Light Dom

<h1>In Shadow</h1><h2>

<content select=".title"></content></h2><section>

<content></content></section>

Shadow Dom

<my-component><h1>In Shadow</h1><h2>

<span class="title">Hello too</span></h2><section>

<span>Bla Bla Bla</span></section>

</my-component>

Composed Dom

Light Dom and Shadow Dom

Page 35: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●●

And Shady Dom...

Page 36: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Propriétés

Page 37: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●●●●●

Polymer({

is: 'x-custom',

properties: {

userName: String,

isHappy: Boolean,

count: {

type: Number,

readOnly: true,

notify: true

}

}

...

});

<x-custom user-name="Scott"></x-custom>

Properties : configuration

Page 38: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Data Binding

Page 39: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<dom-module id="host-element">

<template>

<child-element name="{{myName}}"></child-element>

</template>

</dom-module>

Data Binding

Page 40: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●

2 types

Page 41: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●●

Pour faire du 2 way databinding

Page 42: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●●

<dom-module id="x-custom">

<template>

<div hidden="{{!enabled}}"></div>

<span>{{computeFullName(first, last)}}</span>

</template>

</dom-module>

<script>

Polymer({

is: 'x-custom',

properties: {

first: String,

last: String

},

computeFullName: function(first, last) {

return first + ' ' + last;

}

Les expressions

Page 43: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●

<dom-module id="employee-list">

<template>

<template is="dom-repeat" items="{{employees}}">

<div># <span>{{index}}</span></div>

<div>First name: <span>{{item.first}}</span></div>

</template>

</template>

</dom-module>

Helpers : dom-repeat

Page 44: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●<dom-module id="user-page">

<template>

<template>

All users will see this:

<div>{{user.name}}</div>

<template is="dom-if" if="{{user.isAdmin}}">

Only admins will see this.

<div>{{user.secretAdminStuff}}</div>

</template>

</template>

</dom-module>

Helpers : dom-if

Page 45: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Styling

Page 46: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●●

source :http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/

CSS et Shadow Dom

Page 47: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

<dom-module id="my-button">

<template>

<style>

.title {

color: var(--my-toolbar-title-color,red);

}

:host {

@apply(--my-button-theme);

}

</style>

</template>

</dom-module>

<style>

.submit {

--my-toolbar-title-color: green;

}

.special {

--my-button-theme:{

padding:20px;

backgound-color:red;

}

</style>

<my-button class="submit"></my-button>

<my-button class="special"></my-button>

Custom CSS properties / mixins

Page 48: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Cas réels

Page 49: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Des applications et des composants...

Page 50: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Une mire d’authentification pré-connectée

<frf-login>

<frf-login loginurl=“/login"

logouturl=“/logout">

<span>C’est qui ?</span>

</frf-login>

Page 51: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Architecture

frf-login

frf-user

frf-login-form

frf-confirm

html5-paper-input

frf-login-service

core-ajax

Page 52: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

API

Page 53: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

ui: { $frfLogin: 'frf-login'},events: { 'login-success frf-login': 'onLogin', 'logout-success frf-login': 'onLogout'},onLogin: function (e) { this.ui.$frfLogin.hide(); var userDatas=this.ui.$frfLogin.get(0).getCurrentUser();},logout: function () { his.ui.$frfLogin.get(0).logout();}

render() { reactPolymer.registerAttribute('login-url'); reactPolymer.registerEvent('login-success','login-success'); return ( <div> <link rel="import" href="frf-login/frf-login.html"/> <div className="views"> <frf-login ref="frfLogin" login-url={loginUrl} logout-url={logoutUrl} login-success={this.onLogin}> <span id="title">C'est qui ?</span> </frf-login> </div> </div> );}onLogin(event) { var userDatas = this.refs.frfLogin.getDOMNode().getCurrentUser();}

Un élément HTML comme les autres

Page 54: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Une implémentation du composant google-recaptcha

<re-captcha>

<re-captcha sitekey="yoursitekey"

></re-captcha>

source: https://github.com/cbalit/re-captcha

Page 55: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

Les outils

Page 56: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

bower install --save Polymer/polymer#^1.0.0

bower install --save PolymerElements/iron-elements

bower install --save PolymerElements/paper-elements

bower install --save PolymerElements/gold-elements

Bower

Page 57: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

npm install -g generator-polymer

yo polymer (polymer:app)

yo polymer:el

yo polymer:seed

yo polymer:gh

Yeoman

Page 58: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

npm install -g web-component-tester

wtc

OU

bower install Polymer/web-component-tester --save

<script src="../../web-component-tester/browser.js"></script>

Web Components Tester

Page 59: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

https://github.com/cbalit/re-captcha

Exemple

Page 60: Polymer 1.0 par Cyril Balit au DevFest Paris 2016

●●●●●●●

Et aussi

Page 61: Polymer 1.0 par Cyril Balit au DevFest Paris 2016