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

Web Components mit Angular: UI-Feuerwerk mit Struktur

Web Components mit Angular: UI-Feuerwerk mit Struktur

Bootstrap, Foundation oder Material Design haben eine Gemeinsamkeit. Sie bieten ein Framework, um unsere HTML5-Anwendungen "entwicklerhübsch" darzustellen. Auf dieser Basis entwickelte UI-Komponenten sind meist stark mit dem Konzept und Design des Frameworks verheiratet. Eigene, oftmals durch Corporate Identity vorgegebene UI-Semantiken, Bedien- und Designkonzepte lassen sich nur schwer mit einem bestehenden Framework implementieren. Die Portier- und Wiederverwendbarkeit leiden darunter. Doch moderne Konzepte, wie Web Components, Shadow DOM und Custom-HTML-Elemente können helfen, eigene, wiederverwendbare und stylische UI-Komponenten zu entwickeln. In dieser Session will Rauber ein Konzept zur Entwicklung von Web Components mit dem Angular-Komponentenmodell vorstellen und zeigen, mit welchem Workflow man vom Design bis hin zur stylisch umgesetzten App kommt.

GitHub: https://github.com/thinktecture/jax-2018-web-components

Manuel Rauber

April 25, 2018
Tweet

More Decks by Manuel Rauber

Other Decks in Programming

Transcript

  1. MANUEL RAUBER THEMEN § Cross-Plattform Entwicklung mit Angular § Backend

    Entwicklung mit Node.js oder .NET Core § Consultant, Speaker, Writer KONTAKT § Mail: [email protected] § Twitter: @manuelrauber § Blog: https://manuel-rauber.com
  2. <html> <head> <script src="fancyFramework.js"></script> </head> <body> <div id="fancyWidget"></div> </body> </html>

    § Keine Semantik § Intransparenz § Anpassungen schwierig § Framework-Abhängigkeit
  3. <html> <head> <script src="fancyFramework.js"></script> </head> <body> <div id="fancyWidget"> <span id="label"></span>

    <button id="incButton">+</button> <button id="decButton">-</button> … </div> </body> </html> § Global Scoping § Namenskonflikte § Styling-Konflikte
  4. <html> <head> <link rel="import" href="number-pad.html"></link> </head> <body> <number-pad value="1" minimum="1"

    maximum="1"> </number-pad> </body> </html> § Semantik § Local Scoping § Konfiguration § Bundle Import
  5. CUSTOM ELEMENTS Eine API zur Registrierung von eigenen HTML Elementen

    HTML TEMPLATES UI-Vorlage für eine Komponente SHADOW DOM Kapselung der inneren DOM- Bestandteile eines Elements HTML IMPORTS / ES MODULES Importieren von Komponenten WEB COMPONENTS
  6. <html> <head> <link rel="import" href="number-pad.html"></link> </head> <body> <number-pad value="1" minimum="1"

    maximum="1"> </number-pad> </body> </html> Custom Elements HTML Imports Shaddow DOM HTML Templates
  7. CUSTOM ELEMENTS DEFINITION § Eigene HTML-Elemente erstellen § Das Verhalten

    ist vom Dokument entkoppelt § Eigenständiges Lifecycle-Modell (Erstellung, Attributänderungen, …) <my-person firstName="Jörg"></my-person>
  8. HTML TEMPLATES DEFINITION § HTML Element <template> § Enthält Content

    der nach dem Laden nicht direkt gerendert wird § Wird erst bei Verwendung gerendert § Das Fragment kann im Dokument wiederverwendet werden <table id="producttable"> <thead> <tr> <td>Nr.</td> <td>Name</td> </tr> </thead> <tbody> <!-- wird dynamisch mit inhalt gefüllt --> </tbody> </table> <template id="productrow"> <tr> <td class="record"></td> <td></td> </tr> </template>
  9. SHADOW DOM DEFINITION § Bietet ein gekapseltes DOM / CSS

    für eine Komponente FEATURES § Isolated DOM: Lokale Elemente der Komponente sind von außen nicht sichtbar. § Scoped CSS: Lokale CSS-Definitionen wirken sich nur auf die Komponente aus. § Composition: Komponente kann von außen per Markup erweitert werden. § Einfaches CSS: Keine Konflikte mit id/class-Namen von außen. § Produktivität: App besteht aus gekapselten, wiederverwendbaren Komponenten.
  10. SHADOW DOM - STYLING CSS SELECTORS § Pseudo-classes: :host, :host(),

    :host-context() § Pseudo-elements: ::slotted() § Combinator: >>> * <template id="template"> <style> :host { display: block; width: fit-content; font-size: 30px; background: #78909c; margin: 1px; padding: 14px; } span { background: white; font-size: 30px; } </style> <span id="counter"></span> </template> Document Element (Shadow Host) Shadow Root Content
  11. <div id="frame"> <span> <span id="label"></span> <slot name="addition"></slot> </span> <button id="increment-button">+</button>

    <button id="decrement-button">-</button> </div> <number-pad value="3" minimum="1" maximum="5"> <span slot="addition">kg</span> </number-pad> Verwendung: Template: SHADOW DOM - COMPOSITION DEFINITION § Ermöglicht das Einbetten von benutzerdefinierten Elementen § <slot>-Element im Template dient als Platzhalter § slot-Eigenschaft im Host stellt die Verbindung her
  12. HTML IMPORTS DEFINITION § Dienen zum Paketieren von Komponenten §

    Kann CSS und JavaScript enthalten § Einbindung erfolgt über: <link rel="import"> § Wird zukünftig von ES6-Modulen abgelöst <html> <head> <title>Human Being</title> <link rel="import" href="/imports/heart.html"> </head> <body> <p>What is a body without a heart?</p> </body> </html> var link = document.querySelector('link[rel=import]'); var heart = link.import; // Access DOM of the document in /imports/heart.html var pulse = heart.querySelector('div.pulse');
  13. BROWSER-KOMPATIBILITÄT Chrome Firefox IE 11+/ Edge Opera Safari 9+ Chrome

    (Android) Safari (iOS 9+) Template Native Native Partial Native Native Native Native HTML Imports Native Polyfill Polyfill Native Polyfill Native Polyfill Custom Elements Native Polyfill Polyfill Native Partial Native Partial Shadow DOM Native Polyfill Polyfill Native Partial Native Partial
  14. POLYMER WEB COMPONENTS-FRAMEWORK VON GOOGLE § Version 1.0 wurde 2015

    veröffentlicht § Version 2 in 2017 FEATURES § Einfache Entwicklung von Custom Elements § Two-Way Data Binding § Computed Properties § Conditional & Repeat Templates § Gesture Events § Element Library
  15. ANGULAR SINGLE-PAGE APPLICATION FRAMEWORK VON GOOGLE § Version 1 wurde

    2010 veröffentlicht § Version 2 in 2016 als kompletter Rewrite („Application Platform“) § Folgt semantischer Versionierung (a lá Google Chrome) FEATURES § Hierarchisches Komponentenmodell § Datenbindung § Datenflusssteuerung via Input & Output § Routing § Dependency Injection
  16. KOMPONENTEN HIERARCHISCHES KOMPONENTENMODELL § Kapseln Logik und Anzeige § Anzeige

    besteht aus Struktur und Design Parent -> Child Application Hackathons Users Hackathon Hackathon [hackathons]=“someExp” [hackathon]=“someExp” Child-> Parent Application Hackathons Users Hackathon Hackathon (rate)=“onRate()” (rate)=“onRate()” via @Input() via @Output()
  17. KOMPONENTEN ANGULAR COMPONENTS === WEBCOMPONENTS? § Noch nicht! § Neue

    experimentelle API in Angular 6: Angular Elements § Wandelt Angular Components in echte Web Components um § Können überall eingebunden werden § Automatisches Bootstrapping § Automatische Umwandlung von Input/Output in echte Events und Observed Attributes § Wird mit Angular neuem Renderer Ivy sehr interessant („Hello World“ @ 2,7 kb)
  18. FAZIT Komponenten helfen! § Bessere Struktur der App § Klare

    Semantik § Fördert die Wartbarkeit Web Components unterstützen § Kapselung von Markup & Code § Bessere Wiederverwendung § Bisher noch nicht 100% Browser Support Angular § Unterstützt eine komponentenbasierte Architektur
  19. RESSOURCEN WEBCOMPONENTS.ORG § http://webcomponents.org/ WEB COMPONENTS TUTORIALS § https://blogs.windows.com/msedgedev/2015/07/14/bringing-componentization-to-the-web-an- overview-of-web-components/#TSeSZK7qXciRX8oE.99

    § https://component.kitchen/tutorial WEB COMPONENTS BROWSER SUPPORT § http://webcomponents.org SHADOW DOM § https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM
  20. WEB COMPONENTS: BROWSER SUPPORT ÜBERBLICK ALLER FEATURES/BROWSERS § http://jonrimmer.github.io/are-we-componentized-yet/ CUSTOM

    ELEMENTS § http://caniuse.com/#search=Custom%20elements SHADOW DOM § http://caniuse.com/#search=shadow%20dom TEMPLATES § http://caniuse.com/#feat=template HTML IMPORTS § http://caniuse.com/#search=html%20imports
  21. SPECS CUSTOM ELEMENTS § http://w3c.github.io/webcomponents/spec/custom/ HTML IMPORTS § http://w3c.github.io/webcomponents/spec/imports/ TEMPLATES

    § https://html.spec.whatwg.org/multipage/scripting.html#the-template-element SHADOW DOM § http://w3c.github.io/webcomponents/spec/shadow/