Discovering
              Lit-Element

Workshop 4SH

Je suis loin d'être un expert Lit-Element

 

Il est possible que je dise de grosses bêtises

Parlons un peu d'Angular

Les points qui me rendent 🤪 avec Angular

Pour rappel, Angular c'est :

  • Projet maintenu par Google
     
  • Annonce : Oct. 2014 ; Release : Sept 2016
     
  • Ré-écriture complète d'AngularJS
    • AngularJS éliminé progressivement par Google à partir de Jan. 2018

Des fonctionnalités séduisantes

  • Un build "clefs en main"
     
  • Des fonctionnalités "core"
    • IoC, Routing, Forms, i18n, Animations
       
  • Typesafety : Typescript + Templates
     
  • Dynamic Loading
     
  • Angular Universal

Apparu avant certaines spécs importantes du W3C

         Back to Basics
                     -

     Créer un WebComponent
                     en 2021

Lit-Element

Éléments clefs

  • Projet Polymer de Google
    • Web Components library
      ​(Nov. 2013)
    • Youtube, Google Earth,
      Google Play Music...
    • Re-focused sur lit-element
      (Mai 2018)

       
  • 2 (petites) librairies
    • ​lit-html
    • lit-element

lit-html

  • Expose un template tag : html
    • ​Produit un TemplateResult​
    • Template "efficient" au niveau de ses variables
       
  • Exporte une fonction utilitaire : render​
    • Injecte un TemplateResult dans le DOM
import {html, render} from 'lit-html';

// This is a lit-html template function. It returns a lit-html template.
const helloTemplate = (name) => html`<div>Hello ${name}!</div>`;

// This renders <div>Hello Steve!</div> to the document body
render(helloTemplate('Steve'), document.body);

// This updates to <div>Hello Kevin!</div>, but only updates the ${name} part
render(helloTemplate('Kevin'), document.body);

lit-element

A simple base class for creating fast, lightweight web components​

  • Rendering / Shadow DOM
     
  • Scoped Styles
     
  • 1-way databinding
     
  • Events for bubbling changes
     
  • WebComponent lifecycle
     

lit-element (ES2015)

import { LitElement, html } from 'lit-element';

class CounterComponent extends LitElement {
  static get properties() { 
    return {
      value: {type: Number},
      max: {type: Number}
    };
  }
  
  constructor() {
    super();
    this.max = Infinity;
    this.value = 1;
  }

  render() {
    return html`
          <div>
            Counter : 
            <button @click="${this.increment}">+</button>
            ${this.value}
            <button @click="${this.decrement}">-</button>
          </div>
    `;
  }

  increment(event) { this.value = Math.min(this.value+1, this.max); }
  decrement(event) { this.value--; }
}

customElements.define('hello-counter', CounterComponent)
<hello-counter value="10" max="20"></hello-counter>

lit-element (Typescript)

import { LitElement, html, property, customElement } from 'lit-element';

@customElement('hello-counter')
class CounterComponent extends LitElement {
  @property({ type: Number }) value = 1;
  @property() max = Infinity;

  render() {
    return html`
            <div>
              Counter : 
              <button @click="${this.increment}">+</button>
              ${this.value}
              <button @click="${this.decrement}">-</button>
            </div>
    `;
  }

  increment() { this.value = Math.min(this.value+1, this.max); }
  decrement() { this.value--; }
}

declare global {
  interface HTMLElementTagNameMap {
    'hello-counter': CounterComponent
  }
}
<hello-counter value="10" max="20"></hello-counter>

Implementation details

Lit-Element
+ Vite


Live-coding time

Vite

$> npm install --save-dev vite

Lit-Element

 

Synthèse

Synthèse

  • Small footprint / simple à appréhender
         En 1h de workshop, vous êtes déjà des experts lit-element 🙂
     
  • Beaucoup plus proche de la spec
          Standardisation = Ecosystème riche sur le long terme
     
  • Templating basé sur les template tags
          Type-safety, aucune nouvelle syntaxe

Synthèse

  • Fini les bundlers
         Merci Vite !
     
  • Fini les opinions
         ⚠️ Double tranchant : nécessité de choisir les bonnes libs

Pour aller plus loin

Ce qu'il me reste à explorer

Merci !

Discovering Lit-Element - Workshop 4SH

Made with Slides.com