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

Web Components with Angular: Workshop @MobileTechCon Munich 2019

Web Components with Angular: Workshop @MobileTechCon Munich 2019

Manfred Steyer

March 25, 2019
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer ✓ Commodore 64 ✓ 1982 ✓ ~ 1 MHz

    ✓ 64 KB RAM ✓ Disks with 170 KB
  2. @ManfredSteyer About me… • Manfred Steyer • SOFTWAREarchitekt.at • Angular

    Trainings and Consultancy • Google Developer Expert (GDE) • Trusted Collaborator for Angular Page ▪ 6 Manfred Steyer Public: München, Frankfurt, Wien In-House: everywhere http://www.softwarearchitekt.at
  3. @ManfredSteyer Contents • Web Components • Using Web Components in

    Angular • Web Components with Angular Elements • Lazy and External Web Components • Building Web Components • Zone-less Change Detection • Content Projection
  4. @ManfredSteyer Templates Markup that is rendered but not displayed Can

    be displayed when needed, e. g. within components <template><span class="number"></span> Records found</template>
  5. @ManfredSteyer Selected Polyfills • webcomponents-loader.js • Loads Polyfills on demand

    • Custom Elements • Shadom DOM • Templates • native-shim.js • Needed for browsers that DO support polyfills … • … as long as we downlevel to ES5
  6. @ManfredSteyer API class HelloElement extends HTMLElement { constructor() { super();

    this.attachShadow({mode: 'open'}); this.shadowRoot.innerHTML = '<div>Hello World</div>'; } […] }
  7. @ManfredSteyer API class HelloElement extends HTMLElement { constructor() { super();

    this.attachShadow({mode: 'open'}); this.shadowRoot.innerHTML = '<div>Hello World</div>'; } […] } customElements.define('hello-world', HelloElement);
  8. @ManfredSteyer Using Properties class HelloElement extends HTMLElement { get name()

    { […] } set name(value: string) { … } constructor() { […] } […] }
  9. @ManfredSteyer Using Properties <!-- Property Binding with Angular --> <hello-world

    [name]="firstName"></hello-world> <hello-world [name]="'Max'"></hello-world> <!-- VanillaJS --> var elm = document.getElementById(…) elm['name'] = max;
  10. @ManfredSteyer Component Callbacks & Co. • attributeChangedCallback(name, oldValue, newValue) •

    static get observedAttributes • connectedCallback • disconnectedCallback • adoptedCallback
  11. @ManfredSteyer Angular Elements @NgModule({ imports: [BrowserModule], declarations: [DateComponent], bootstrap: [],

    entryComponents: [DateComponent] }) export class AppModule { constructor(private injector: Injector) { const DateElement = createCustomElement( DateComponent, { injector: this.injector }); customElements.define('date-picker', DateElement); } }
  12. @ManfredSteyer Angular (Elements) & Shadow DOM • Baked-in • Default:

    Emulation • @Component({ encapsulation: ViewEncapsulation.Emulated }) • Shadow DOM (v0) • @Component({ encapsulation: ViewEncapsulation.Native }) • Shadow DOM (v1) • @Component({ encapsulation: ViewEncapsulation.ShadowDom }) • Can be turned off: • @Component({ encapsulation: ViewEncapsulation.None })
  13. @ManfredSteyer All your users pay for IE 11! • Polyfills:

    ~ 150 KB • Even custom elements polyfills for browsers that DO support custom elements b/c of ES5 • ES5: Bigger, less optimized
  14. @ManfredSteyer Differential Serving • Provide modern ES2015+ bundels w/o polyfills

    • Provide legacy ES5 bundles w/ polyfills • Let browser decide which to load
  15. @ManfredSteyer Bundles Custom Element 1 Custom Element 2 Custom Element

    3 Libraries: Angular, RxJS, … Libraries: Angular, RxJS, …
  16. @ManfredSteyer Where Ivy can help ✓ Widgets based on @angular/core,

    @angular/common  Libs like @angular/forms or @angular/router
  17. @ManfredSteyer Sharing Libs Custom Element 1 Custom Element 2 Custom

    Element 3 Libraries: Angular, RxJS, … UMD ngx-build-plus Drawbacks: Complexity, no isolation
  18. @ManfredSteyer Why zone.js? • Used by Angular's changed detection •

    Patches each browser event • Notifies Angular after each event handler
  19. @ManfredSteyer Why not zone.js? • 127 KB (12 gzipped) •

    Awkward to force consumers of our web components to load zone.js
  20. @ManfredSteyer Calling a Tab <my-tab title="Page 1"> Lorem ipsum, dolor

    sit amet … <div slot="footer">Some extra info</div> </my-tab>
  21. @ManfredSteyer Summary Polyfills and CUSTOM ELEMENTS_SCHEMA Directives for Two-Way-Binding Lazy

    & External Web Components Build Strategies: Differential Serving Sharing Code Zone-less Slot API