Share
Embed
Transcript
60 slides
-
自己紹介 • 大木尊紀(オオキタカノリ) / @takanorip • 株式会社スマートドライブ • フロントエンドエンジニア •
React、Vue.js、Polymer、ウェブ制作全般 • Polymer Japan 翻訳チーム • 「イヌでもわかるWeb Components」 Boothにて販売中! -
PolymerでWeb制作をするメリット • 環境構築をほとんどしなくて良い • HTML/CSSがscoped • コンポーネント指向なマークアップができる • custom CSS
propertiesや custom CSS mixinsを使うことができる -
$npm install -g polymer-cli $polymer init ? Which starter template
would you like to use? polymer-2-element - A simple Polymer 2.0 element template polymer-2-application - A simple Polymer 2.0 application › polymer-2-starter-kit - A Polymer 2.x starter application template, with navigation and "PRPL patter n" loading shop - The "Shop" Progressive Web App demo $polymer serve info: Files in this directory are available under the following URLs applications: http://127.0.0.1:8081 reusable components: http://127.0.0.1:8081/components/polymer- starter-kit/ -
├── README.md ├── bower.json ├── bower_components ├── images ├── index.html
├── manifest.json ├── package.json ├── polymer.json ├── service-worker.js ├── src │ ├── my-app.html │ ├── my-icons.html │ ├── my-view1.html │ ├── my-view2.html │ ├── my-view3.html │ ├── my-view404.html │ └── shared-styles.html ├── sw-precache-config.js └── test -
<link rel="import" href=“../bower_components/polymer/polymer-element.html"> <link rel="import" href=“./component/sample-button.html"> <dom-module id="sample-view1"> <template> <style>
:host { display: block; } </style> <div class="sample"> <h1 class=“sample-titke”>Hello Polymer</h1> <sample-button></sample-button> </div> </template> <script> class SampleView1 extends Polymer.Element { static get is() { return 'sample-view1'; } } window.customElements.define(SampleView1.is, SampleView1); </script> </dom-module> -
Shadow DOM • PolymerのエレメントでShadow DOMを定義するには <dom-module>と<template>を使う • <dom-module>はShadow DOM全体(HTML/CSS/JS) をラップする
• <template>にはShadow DOMのHTML部分を記述する -
<dom-module id="sample-view1"> <template> <style> :host { display: block; } </style>
<div class="sample"> <h1 class=“sample-titke”>Hello Polymer</h1> <sample-button></sample-button> </div> </template> </dom-module> -
<dom-module id="sample-view1"> <script> class SampleView1 extends Polymer.Element { static get
is() { return 'sample-view1'; } } window.customElements.define(SampleView1.is, SampleView1); </script> </dom-module> -
<link rel="import" href=“../bower_components/polymer/polymer-element.html"> <dom-module id="sample-view1"> <template> <style> :host { display:
block; } </style> <div class="sample"> <h1 class=“sample-titke”>Hello Polymer</h1> </div> </template> <script> class SampleView1 extends Polymer.Element { static get is() { return 'sample-view1'; } } window.customElements.define(SampleView1.is, SampleView1); </script> </dom-module> -
<link rel="import" href=“../bower_components/polymer/polymer-element.html"> <link rel="import" href=“./component/sample-button.html"> <dom-module id="sample-view1"> <template> <style>
:host { display: block; } </style> <div class="sample"> <h1 class=“sample-titke”>Hello Polymer</h1> <sample-button></sample-button> </div> </template> <script> class SampleView1 extends Polymer.Element { static get is() { return 'sample-view1'; } } window.customElements.define(SampleView1.is, SampleView1); </script> </dom-module> -
<script> class SampleButton extends Polymer.Element { static get is() {
return "sample-button"; } static get properties() { return { buttonText: { type: String } } } constructor() { super(); } } customElements.define(SampleButton.is, SampleButton); </script> -
class SampleButton extends Polymer.Element { static get is() { return
'sample-button'; } static get properties() { return { num: { type: Number, notify: true, reflectToAttribute: true, value: 1 }, } } count() { this.num++; } } window.customElements.define(SampleButton.is, SampleButton); -
Event Listener • 普通のJSと少し書き方が異なるので注意 • notify • 値が変更されるとイベントを発火させる • reflectToAttribute
• 値の変更を反映する • value • 初期値 -
CSS variables • PolymerではCustom CSS property implementationを 使うことができる • CSSの中で変数を使うことができる
• var(--my-custom-property, defaultValue) -
body { --sample-color1: #1E88E5; --sample-color2: #333333; } :host { display:
block; background-color: var(--sample-color1, currentcolor); } -
html { --my-custom-mixin: { color: white; background-color: blue; } }
:host { @apply --my-custom-mixin; } -
<app-location route="{{route}}" url-space-regex="^[[rootPath]]"> </app-location> <app-route route="{{route}}" pattern="[[rootPath]]:page" data="{{routeData}}" tail="{{subroute}}"> </app-route>
<sample-layout> <sample-header slot="header" /> <iron-pages selected="[[page]]" attr-for-selected="name" fallback-selection="view404" role="main"> <sample-view1 name="view1"></sample-view1> <sample-view2 name="view2"></sample-view2> <sample-view3 name="view3"></sample-view3> <sample-view404 name="view404"></sample-view404> </iron-pages> <sample-footer slot="footer" /> </sample-layout> -
番外編:PWA • PWA(Progressive Web Apps)の略 • iOS 11.3のSafari 11.1からService Workerに対応!
• オフライン対応やPush通知などネイティブアプリに近 い挙動を実現する技術の総称 • Service Worker • Push Notification • ネイティブアプリに近いUI -
if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('service-worker.js', {
scope: Polymer.rootPath, }); }); } -
module.exports = { staticFileGlobs: [ '/index.html', '/manifest.json', '/bower_components/webcomponentsjs/webcomponents-lite.js', '/images/*' ],
navigateFallback: '/index.html', navigateFallbackWhitelist: [/^(?!.*\.html$|\/data\/).*/] } -
課題 • 配信されているコンポーネントが少ない • 今後に期待 / 自分で作って公開する • Sassなどを簡単に使うことができない •
Gulpなど使う必要がある / ディレクトリ構成が複雑 • bower • Polymer3.0に期待