Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Welcome to Polymer

Welcome to Polymer

PolymerライブラリとWeb Componentsについて書きました。Polyerの実装例として電卓を作ってみました。

Yohei Munesada

August 03, 2015
Tweet

More Decks by Yohei Munesada

Other Decks in Technology

Transcript

  1. 5FNQMBUFT <!— ैདྷͷ৔߹ —> <script type=“text/html”> <img src=“"> <p>comment</p> </script>

    <!— Web Componentsͷ৔߹ —> <template> <img src=“"> <p>comment</p> </template> ैདྷͱͷൺֱ
  2. 5FNQMBUFT <template> <img src=“"> <p>comment</p> </template> <script> var t =

    document.querySelector(‘template’); t.content.querySelector(‘img’).src = ‘logo.png’; var clone = document.importNode(t.content, true); document.body.appendChild(clone); </script> ར༻ํ๏
  3. ར༻ํ๏ )5.-*NQPSUT <!— index.html —> <link rel="import" href=“import.html"> <script> var

    link = document.querySelector('link[rel="import"]'); var content = link.import; document.body.appendChild(content.body.cloneNode(true)); </script> <!— import.html —> <h3>Hello!</h3> <p>HTML ImportsͰಡΈࠐ·ΕͨཁૉͩΑʂ</p>
  4. ੜ੒ 4IBEPX%0.T <button>Hello, world!</button> <script> var btn = document.querySelector('button'); var

    root = btn.createShadowRoot(); root.textContent = '͜Μʹͪ͸ɺӨͷੈք!'; </script> )FMMP XPSME ▶<button> “Hello, world!” </button>
  5. ੜ੒ 4IBEPX%0.T <button>Hello, world!</button> <script> var btn = document.querySelector('button'); var

    root = btn.createShadowRoot(); root.textContent = '͜Μʹͪ͸ɺӨͷੈք!'; </script> ͜Μʹͪ͸ɺӨͷੈք ▶<button> ▼#shadow-root | “͜Μʹͪ͸ɺӨͷੈք” “Hello, world!” </button>
  6. είʔϓ 4IBEPX%0.T <div>௨ৗͷDIVλάͰ͢</div> <div id="shadow"></div> <template id="t1"> <style>div{background-color:red;}</style> <div>ShadowDOM಺ͷDIVͰ͢</div> </template>

    <script> var div = document.querySelector('#shadow'); var root = div.createShadowRoot(); var template = document.querySelector('#t1'); var node = document.importNode(template.content, true); root.appendChild(node); </script>
  7. είʔϓ 4IBEPX%0.T <div>௨ৗͷDIVλάͰ͢</div> <div id="shadow"></div> <template id="t1"> <style>div{background-color:red;}</style> <div>ShadowDOM಺ͷDIVͰ͢</div> </template>

    <script> var div = document.querySelector(‘#shadow'); var root = div.createShadowRoot(); var template = document.querySelector('#t1'); var node = document.importNode(template.content, true); root.appendChild(node); </script> 4IBEPX%0.಺ͰͷΈ༗ޮ
  8. <div>௨ৗͷDIVλάͰ͢</div> <div id="shadow"></div> <template id="t1"> <style>div{background-color:red;}</style> <div>ShadowDOM಺ͷDIVͰ͢</div> </template> <script> var

    div = document.querySelector(‘#shadow'); var root = div.createShadowRoot(); var template = document.querySelector('#t1'); var node = document.importNode(template.content, true); root.appendChild(node); </script> 4IBEPX%0.಺ͰͷΈ༗ޮ είʔϓ 4IBEPX%0.T
  9. ར༻ྫᶃ $VTUPN&MFNFOUT <!— Custom Elementͷఆٛ —> <script> var p =

    Object.create(HTMLElement.prototype) document.registerElement(‘my-element’, {prototype:p}); </script>
  10. ར༻ྫᶃ $VTUPN&MFNFOUT <!— Custom Elementͷఆٛ —> <script> var p =

    Object.create(HTMLElement.prototype) document.registerElement(‘my-element’, {prototype:p}); </script> <!— Custom Elementͷར༻ —> <my-element></my-element>
  11. ར༻ྫᶄ $VTUPN&MFNFOUT <template> <style>p {color: red;}</style> <p>I am Yohei Munesada</p>

    </template> <script> var proto = Object.create(HTMLElement.prototype); document.registerElement('my-element2', {prototype: proto}); </script> 5FNQMBUF $VTUPN&MFNFOU JNQPSUIUNM
  12. ར༻ྫᶄ $VTUPN&MFNFOUT <template> <style>p {color: red;}</style> <p>I am Yohei Munesada</p>

    </template> <script> var proto = Object.create(HTMLElement.prototype); var importDoc = document.currentScript.ownerDocument; proto.createdCallback = function () { var root = this.createShadowRoot(); var t = importDoc.querySelector('template'); var clone = document.importNode(t.content, true); root.appendChild(clone); } document.registerElement('my-element2', {prototype: proto}); </script> 5FNQMBUF $VTUPN&MFNFOU 4IBEPX%0. JNQPSUIUNM
  13. Web components primitives Polymer library Custom elements EFDMBSBUJWFTZOUBY UFNQMBUJOH XBZCJOEJOH

    PCTFSWJOH FUDʜ *SPO&MFNFOUT 1BQFS&MFNFOUT (PPHMF8FC$PNQPOFOUT ߏ੒಺༰
  14. ಛ௃ᶃએݴతͳ࣮૷ <!— PolymerͷΠϯϙʔτ —> <link rel=“import" href=“polymer/polymer.html”> <!— ΧελϜΤϨϝϯτͷఆٛ —>

    <dom-module id="my-element"> <template> <p>I'm a My element. This is my local DOM!</p> </template> </dom-module>
  15. ಛ௃ᶃએݴతͳ࣮૷ <!— PolymerͷΠϯϙʔτ —> <link rel=“import" href=“polymer/polymer.html”> <!— ΧελϜΤϨϝϯτͷఆٛ —>

    <dom-module id="my-element"> <template> <p>I'm a My element. This is my local DOM!</p> </template> <script> Polymer({ is: "my-element" }); </script> </dom-module>
  16. ಛ௃ᶃએݴతͳ࣮૷ <!DOCTYPE html> <html> <head> <script src="pathTo/webcomponents-lite.min.js"></script> <link rel="import" href="my-element.html">

    </head> <body> <my-element></my-element> </body> </html> I'm a .Z element. This is my local DOM! I'm a .Z element. This is my local DOM!
  17. ಛ௃ᶄσʔλόΠϯσΟϯά <link rel="import" href="polymer/polymer.html"> <link rel="import" href="iron-input/iron-input.html"> <dom-module id="name-tag"> <template>

    <p>Hello <span>{{name}}</span></p> <input type="text" is="iron-input" bind-value="{{name}}"> </template> </dom-module>
  18. ಛ௃ᶄσʔλόΠϯσΟϯά <link rel="import" href="polymer/polymer.html"> <link rel="import" href="iron-input/iron-input.html"> <dom-module id="name-tag"> <template>

    <p>Hello <span>{{name}}</span></p> <input type="text" is="iron-input" bind-value="{{name}}"> </template> <script> Polymer({ is: "name-tag", properties: { name: {type: String, value: "DefaultName"} } }); </script> </dom-module>
  19. ಛ௃ᶄσʔλόΠϯσΟϯά <link rel="import" href="polymer/polymer.html"> <link rel="import" href="iron-input/iron-input.html"> <dom-module id="name-tag"> <template>

    <p>Hello <span>{{name}}</span></p> <input type="text" is="iron-input" bind-value="{{name}}"> </template> <script> Polymer({ is: "name-tag", properties: { name: {type: String, value: "DefaultName"} } }); </script> </dom-module> \\OBNF^^ͰσʔλόΠϯυ \\OBNF^^ͰσʔλόΠϯυ \\OBNF^^ͰσʔλόΠϯυ
  20. ಛ௃ᶄσʔλόΠϯσΟϯά <link rel="import" href="polymer/polymer.html"> <link rel="import" href="iron-input/iron-input.html"> <dom-module id="name-tag"> <template>

    <p>Hello <span>{{name}}</span></p> <input type="text" is="iron-input" bind-value="{{name}}"> </template> <script> Polymer({ is: "name-tag", properties: { name: {type: String, value: "DefaultName"} } }); </script> </dom-module> \\OBNF^^ͰσʔλόΠϯυ \\OBNF^^ͰσʔλόΠϯυ \\OBNF^^ͰσʔλόΠϯυ
  21. <!DOCTYPE html> <html lang="en"> <head> <link rel="import" href=“my-calculator.html"> </head> <body>

    <my-calculator></my-calculator> </body> </html> $BMDVMBUPSJOEFYIUNM )5.-*NQPSUTͰಡΈࠐΈ $VTUPN&MFNFOUͷར༻
  22. <template> <div class="root"> <div class="display"> <span>{{display}}</span> </div> <div class="controls"> <paper-button

    class="col3">AC</paper-button> <paper-button>+</paper-button> <paper-button>7</paper-button> <paper-button>8</paper-button> <paper-button>9</paper-button> <paper-button>÷</paper-button> /* … */ <paper-button>=</paper-button> </div> </div> </div> </template> $BMDVMBUPSNZDBMVDMBUPSIUNMɿϚʔΫΞοϓ σʔλόΠϯυΛར༻ .BUFSJBM%FTJHOΛ࢖͏
  23. <style> :host {display: block; width: 100%; height: 100%;} paper-button {

    float: left; width: 25%; height: calc(100% / 5); /* … */ } paper-button::shadow paper-ripple { color: rgba(255,255,0,1); } </style> $BMDVMBUPSNZDBMVDMBUPSIUNMɿελΠϦϯά IPTUͰɺNZDBMDVMBUPSλάͷ૷০ ΧελϜΤϨϝϯτ΁ͷ૷০ ΧελϜΤϨϝϯτͷ4IBEPX%0.಺΁ ͷ૷০΋Ͱ͖Δ
  24. <dom-module id=“my-calculator"> <style>/* … */</style> <template>/* … */</template> <script> Polymer({

    is: ‘my-calculator’, properties: { display: {type: String, value: 0} }, }); </script> </dom-module> $BMDVMBUPSNZDBMVDMBUPSIUNMɿ+4Ͱొ࿥ ΧελϜΤϨϝϯτͱͯ͠ొ࿥
  25. <!— HTML —> <paper-button on-click="numberClick">1</paper-button> <!— JavaScript —> Polymer({ is:

    ‘my-calculator’, numberClick: function (e) { var num = e.target.textContent; this.display += num; } }); $BMDVMBUPSNZDBMVDMBUPSIUNMɿৼΔ෣͍ᶃ Πϕϯτొ࿥ ॲཧΛఆٛ͢Δ <div class="display"> <span>{{display}}</span> </div>
  26. <!— HTML —> <paper-button on-click="calcClick">=</paper-button> <!— JavaScript —> Polymer({ is:

    ‘my-calculator’, numberClick: function (e) {/**/}, calcClick: function (e) { var formula = this.display; var result = calc(formula); this.display = result; } }); $BMDVMBUPSNZDBMVDMBUPSIUNMɿৼΔ෣͍ᶄ Πϕϯτొ࿥ ॲཧΛఆٛ͢Δ