Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Web Components: The Future of Web Development is Here

290 views

Published on

From Drupaldelphia 2018

If you haven’t explored Web Components yet, you’re missing out on a powerful tool that can greatly enhance reusability of common web elements throughout your websites and web applications. As Comcast has been updating our web properties to unify under a single UX, using Web Components with Polymer has helped make that process much more efficient.

This session will introduce you to what exactly Web Components are and how to use them. We’ll also cover building Web Components with Polymer, the most popular Web Component library. You’ll get to hear how Comcast is using the web platform to build its next generation single page apps & websites using the latest browser APIs.

You’ll also learn about how easy it is to onboard a team to using Polymer, tips for sharing components with other websites & across teams, and best practices Comcast has established for efficient development of Web Components.

Published in: Internet
  • Hey guys! Who wants to chat with me? More photos with me here 👉 http://www.bit.ly/katekoxx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Web Components: The Future of Web Development is Here

  1. 1. Drupaldelphia - April 27, 2018 John Riviello @JohnRiv Chris Lorenzo @Chiefcll Web Components: The Future of Web Development is Here
  2. 2. © Comcast
  3. 3. © Comcast
  4. 4. © Comcast
  5. 5. © Comcast
  6. 6. © Comcast
  7. 7. © Comcast
  8. 8. Don’t all of today’s web frameworks/libraries provide “components”?
  9. 9. React
  10. 10. React is all about building reusable components. In fact, with React the only thing you do is build components. Since they're so encapsulated, components make code reuse, testing, and separation of concerns easy. Source: https://facebook.github.io/react/docs/why-react.html
  11. 11. © Comcast
  12. 12. © Comcast
  13. 13. Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll13
  14. 14. How do we create a component without the framework?
  15. 15. #UseThePlatform - Polymer Motto
  16. 16. Web Components #UseThePlatform
  17. 17. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll17 4 Specs
  18. 18. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll18 Custom Elements
  19. 19. What Are Web Components? 19 Custom Elements •Provides a way for authors to build their own fully-featured DOM elements. <paper-tabs selected="0" scrollable> <paper-tab>The first tab</paper-tab> <paper-tab>Tab two</paper-tab> <paper-tab>The third tab</paper-tab> <paper-tab>Fourth tab</paper-tab> </paper-tabs> Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  20. 20. What Are Web Components? 20 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  21. 21. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll21 HTML Imports
  22. 22. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll22 HTML Imports • Means to import custom elements - <link rel="import" href="../paper-tabs/paper-tabs.html"> • Built-in deduplication • Componetize the HTML, CSS & JavaScript • Will be replaced by ES6 Modules - import "../paper-tabs/paper-tabs.js"
  23. 23. What Are Web Components? 23 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  24. 24. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll24 Templates
  25. 25. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll25 • Used to declare fragments of HTML - <template id="tab"> <div class="tab-content"></div> </template> • The element itself renders nothing • Can be cloned and inserted in the document via JavaScript, which will render the content Templates
  26. 26. What Are Web Components? 26 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  27. 27. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll27 Shadow DOM
  28. 28. What Are Web Components? Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll28 •Allows you to take a DOM subtree and hide it from the document scope •Hides CSS styles as well •Common examples from HTML5 include: <select>, <video>, & <input type="date"> Shadow DOM
  29. 29. What Are Web Components? 29 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  30. 30. Can we even use these APIs?
  31. 31. Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll31 Source: https://lists.w3.org/Archives/Public/public-webapps/2011JulSep/0975.html
  32. 32. Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll32 Sources: https://developers.google.com/web/fundamentals/getting-started/primers/customelements https://developers.google.com/web/fundamentals/getting-started/primers/shadowdom
  33. 33. 33 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  34. 34. 34 Source: https://developer.microsoft.com/en-us/microsoft-edge/platform/status/ Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  35. 35. 35 Source: https://developer.microsoft.com/en-us/microsoft-edge/platform/status/ Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  36. 36. Source: http://gs.statcounter.com/browser-version-partially-combined-market-share/all/united-states-of-america/#weekly-201812-201817-bar 84.5%
  37. 37. "Samsung Galaxy S7" by Kārlis Dambrāns is licensed under CC BY 2.0 Mobile Safari 10.1Chrome for Android 54 = nearly 100% of mobile
  38. 38. How do we use a component with a framework?
  39. 39. © Comcast
  40. 40. © Comcast Source: https://www.github.com/Comcast/polaris
  41. 41. © Comcast
  42. 42. <script href="https://polaris.xfinity.com/polaris.js"></script> <xc-header tab="myaccount" is-authed="[[isAuthed]]" login-url="/login" sign-out-url="/logout" first-name="[[openidData.given_name]]" user-name="[[openidData.preferred_username]]"> </xc-header> <xc-footer></xc-footer>
  43. 43. Existing Frameworks Applications Web Platform Web Components built with Polymer (or not)
  44. 44. Rob Dodson: Custom Elements Everywhere https://custom-elements-everywhere.com
  45. 45. How do we use a component with Drupal?
  46. 46. How do I use Web Components?
  47. 47. “The Way of the Web” - Ben Issa, ING Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll49 Source: https://youtu.be/8ZTFEhPBJEE?list=PLNYkxOF6rcICc687SxHQRuo9TVNOJelSZ
  48. 48. “Assemblers First, Craftspeople Second” - Ben Issa, 2016 Polymer Summit Using Web Components Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll50 "Puzzle Pieces Jigsaw Piece Concept" is licensed under CC0 1.0 / Color adjusted from original "Blacksmith Forging Smith Forger" is licensed under CC0 1.0 / Color adjusted from original
  49. 49. $ bower install ELEMENT_NAME --save $ bower install iron-pages --save
  50. 50. $ bower install ELEMENT_NAME --save $ bower install iron-pages --save
  51. 51. Using Web Components • Import the component - <link rel="import" href="../iron-pages/iron-pages.html"> - import "../iron-pages/iron-pages.js" • Use your custom element as a normal HTML tag - <iron-pages> <div>Page 1 content</div> <div>Page 2 content</div> </iron-pages> Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll54
  52. 52. Google Polymer Project
  53. 53. Using Web Components Polymer is NOT • Material Design Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll59
  54. 54. Source: https://ebidel.github.io/material-playground/
  55. 55. Using Web Components Polymer is NOT • Material Design • Required to use Web Components • A framework • Heavy Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll61
  56. 56. Intro to Web Components & Polymer Framework/Library Sizes (with GZIP compression) 62 Source: http://minime.stephan-brumme.com React’s size is react + react-dom & gzipped via http://cnvyr.io/online Polymer’s range is v2.6.0 with just polymer-element.html, to polymer.html without the legacy imports. Bundled, minified & gzipped. 113.7kb 57.2kb 29.2kb30.4kb 12.8kb - 21.6kb 33.3kb v3.0.0 v1.6.9 v3.3.1v2.5.16 v2.6.0v16.3.0 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  57. 57. Why should I use Polymer?
  58. 58. Primitives are designed so we can build libraries on top of them.
  59. 59. Vanilla JS class MyButton extends HTMLElement { connectedCallback() { if (super.connectedCallback) super.connectedCallback(); this.addEventListener('click', this._click); } _click() { alert('Hello'); } } customElements.define('my-button', MyButton); Polymer class MyButton extends Polymer.Element { static get is() { return 'my-button'; } ready() { super.ready(); this.addEventListener('click', e => {this._click(e)}); } _click() { alert('Hello'); } } customElements.define(MyButton.is, MyButton);
  60. 60. Vanilla JS connectedCallback() { this.innerHTML = ` <button id='kickMe'> Kick Me </button> `; this.querySelector('#kickMe') .addEventListener('click', this.handleClick.bind(this)); } Polymer <dom-module id="my-button"> <template> <button on-click="handleClick"> Kick Me </button> </template> </dom-module>
  61. 61. Vanilla JS - Only a few components Polymer - Lots of components or a site
  62. 62. FYI - Other Libraries Skate JS X-Tag < >XX-Tag
  63. 63. Source: https://github.com/webcomponents/gold-standard/wiki
  64. 64. How do I build a Polymer Component?
  65. 65. Source: https://www.polymer- project.org/2.0/docs/tools/polymer-cli
  66. 66. $ polymer help Available Commands analyze Writes analysis metadata in JSON format to standard out build Builds an application-style project help Shows this help message, or help for a specific command init Initializes a Polymer project install installs Bower dependencies, optionally installing "variants" lint Identifies potential errors in your code. serve Runs the polyserve development server test Runs web-component-tester
  67. 67. $ polymer help Available Commands analyze Writes analysis metadata in JSON format to standard out build Builds an application-style project help Shows this help message, or help for a specific command init Initializes a Polymer project install installs Bower dependencies, optionally installing "variants" lint Identifies potential errors in your code. serve Runs the polyserve development server test Runs web-component-tester
  68. 68. $ polymer init ? Which starter template would you like to use? (Use arrow keys) ❯ polymer-1-element - A simple Polymer 1.0 element template polymer-2-element - A simple Polymer 2.0 element template polymer-1-application - A simple Polymer 1.0 application template polymer-2-application - A simple Polymer 2.0 application polymer-1-starter-kit - A Polymer 1.x starter application template, with navigation and "PRPL pattern" loading polymer-2-starter-kit - A Polymer 2.x starter application template, with navigation and "PRPL pattern" loading shop - The "Shop" Progressive Web App demo
  69. 69. $ polymer init ? Which starter template would you like to use? (Use arrow keys) polymer-1-element - A simple Polymer 1.0 element template ❯ polymer-2-element - A simple Polymer 2.0 element template polymer-1-application - A simple Polymer 1.0 application template polymer-2-application - A simple Polymer 2.0 application polymer-1-starter-kit - A Polymer 1.x starter application template, with navigation and "PRPL pattern" loading polymer-2-starter-kit - A Polymer 2.x starter application template, with navigation and "PRPL pattern" loading shop - The "Shop" Progressive Web App demo
  70. 70. $ polymer init ? Which starter template would you like to use? polymer-2-element info: Running template polymer-2-element... ? Element name my-element ? Brief description of the element My sample element create bower.json create demo/index.html create index.html create polymer.json create README.md create my-element.html create test/my-element_test.html Project generated! Installing dependencies...
  71. 71. <!-- Material design: [Tabs](https://www.google.com/design/s pec/components/tabs.html) `paper-tabs` makes it easy to explore and switch between different views or functional aspects of an app, or to browse categorized data sets. Use `selected` property to get or set the selected tab. Example: <paper-tabs selected="0"> <paper-tab>TAB 1</paper-tab> <paper-tab>TAB 2</paper-tab> <paper-tab>TAB 3</paper-tab> </paper-tabs>
  72. 72. <!-- ### Styling The following custom properties and mixins are available for styling: Custom property | Description | Default ----------------|-------------|------- --- `--paper-tabs-selection-bar-color` | Color for the selection bar | `-- paper-yellow-a100` `--paper-tabs-selection-bar` | Mixin applied to the selection bar | `{}` `--paper-tabs` | Mixin applied to the tabs | `{}` `--paper-tabs-content` | Mixin applied to the content container of tabs | `{}` `--paper-tabs-container` | Mixin applied to the layout container of
  73. 73. /** * If true, the tabs are aligned to bottom (the selection bar appears at the top). */ alignBottom: { type: Boolean, value: false }, /** * If true, tabs are automatically selected when focused using the * keyboard. */ autoselect: { type: Boolean, value: false },
  74. 74. Documenting Your Polymer Code • Document with JSDoc syntax - usejsdoc.org • Create a page that imports & includes iron-component-page • Generate the docs and load your page: $ npm i -g polymer-cli bower $ polymer analyze > analysis.json $ polymer serve --open Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll86 Additional Details: https://github.com/PolymerElements/iron-component-page
  75. 75. <test-fixture id="NoLabel">
  76. 76. a11ySuite('NoLabel');
  77. 77. Demo Time!!!
  78. 78. What are some Best Practices?
  79. 79. Sharable ComponentsSharable Components
  80. 80. You Need a TeamYou Need a Team
  81. 81. © Comcast
  82. 82. TDD?
  83. 83. DDD!
  84. 84. D D D
  85. 85. Demo Driven Development
  86. 86. © Comcast
  87. 87. Use CORS (Cross-Origin Resource Sharing) Link: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
  88. 88. Separate Your Data from Your UI Link: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
  89. 89. Why? 1.Decouple State / UI 2.Unidirectional Flow 3.Loadable State 4.Community Tools
  90. 90. <xc-app> index.html LogicStore {}
  91. 91. component-name-event-name <xc-app> index.html Logic xc-people-unpause-all-devices Native Event Store {}
  92. 92. component-name-event-name <xc-app> index.html Logic xc-people-unpause-all-devices Native Event Store {} What’s That?
  93. 93. component-name-event-name <xc-app> index.html Logic xc-people-unpause-all-devices Native Event Store {}
  94. 94. component-name-event-name <xc-app> index.html Store {} Logic xc-people-unpause-all-devices Native Event
  95. 95. component-name-event-name <xc-app> index.html Store {} Logic xc-people-unpause-all-devices Native Event component-name-event-name xc-people-unpause-all-devices Native Event Smart Parent +
  96. 96. <xc-app> index.html LogicStore {} component-name-event-name xc-people-unpause-all-devices Native Event
  97. 97. <xc-app> index.html LogicStore {} component-name-event-name xc-people-unpause-all-devices Native Event
  98. 98. “How to use Redux in Polymer -- Polycasts #61” - Rob Dodson Web Components: The Future of Web Development is Here114 Source: https://youtu.be/PahsgJn0sgU?list=PLNYkxOF6rcIDdS7HWIC_BYRunV6MHs5xo
  99. 99. Important Concepts to Understand • Encapsulation • Composition • Separation of Concerns • Functional Decomposition "separated" by hansol is licensed under CC BY 2.0
  100. 100. Simplicity Matters -Rich Hickey
  101. 101. "Rocky" by Stefan Ogrisek is licensed under CC BY 2.0 / Color adjusted from original Simplicity Creates Opportunity
  102. 102. Useful Links • WebComponents.org - webcomponents.org • Polymer Website - polymer-project.org • HAX (Headless Authoring eXperience) - haxtheweb.org • LRNWebComponents - https://www.webcomponents.org/author/LRNWebComponents • Polymer Slack - polymer-slack.herokuapp.com • Polymer 2.x Cheat Sheet - https://meowni.ca/posts/polymer-2-cheatsheet/ • PWAs with Polymer: a checklist - https://meowni.ca/posts/polymer-pwa-checklist/ • Custom Elements Everywhere - custom-elements-everywhere.com • Polycasts on YouTube - https://www.youtube.com/playlist?list=PLOU2XLYxmsII5c3Mgw6fNYCzaWrsM3sMN • 2017 Polymer Summit videos on YouTube - https://www.youtube.com/playlist?list=PLNYkxOF6rcIDP0PqVaJxqNWwIgvoEPzJi • End-to-End Polymer Apps - 2017 Chrome Dev Summit video - https://youtu.be/Wu2GCRkDecI • 2017 Google I/O Polymer videos on YouTube - https://www.youtube.com/playlist?list=PL_c6rbXV248du6m1VJABo32mP7sXWVb4m 118 Web Components: The Future of Web Development is Here - @JohnRiv & @chiefcll
  103. 103. Thank You!
  104. 104. Thank you! John Riviello @JohnRiv Chris Lorenzo @Chiefcll

×