Slide 1

Slide 1 text

Getting started with Lit #WebComponents Hardik Pithva @hardikpthv #UseThePlatform

Slide 2

Slide 2 text

● Set of web platform APIs ● Specifications: ○ Custom Elements ○ Shadow DOM ○ ES Modules ○ Template Web Components

Slide 3

Slide 3 text

Custom Elements Shadow DOM - - - LitElement

Slide 4

Slide 4 text

Custom Elements

Slide 5

Slide 5 text

What is it? ● Create new or extend existing HTML tags ● Create reusable components using nothing more than vanilla JS / HTML / CSS ● Combines the features offered by most modern browsers ● An approach to modernizing HTML @hardikpthv class FrontManiaDrawer extends HTMLElement { ... } window.customElements.define('front-mania-drawer', FrontManiaDrawer); front-mania-drawer.js index.html

Slide 6

Slide 6 text

Front-Mania Drawer Element @hardikpthv class FrontManiaDrawer extends HTMLElement { constructor() { super(); ... } connectedCallback() { ... } disconnectedCallback() { ... } attributeChangedCallback(attrName, oldVal, newVal) { ... } } front-mania-drawer.js

Slide 7

Slide 7 text

Shadow DOM

Slide 8

Slide 8 text

● Problems: ○ Global attitude of HTML, CSS, and JS ○ CSS:!important (specificity) ○ CSS Containment What is it? @hardikpthv ● Shadow DOM is the rescuer: ○ Support for the scoped style ○ Neither tools nor naming conventions ○ Self-contained component ● An option to provide HTML and CSS to your custom element ● Just like a DOM, but not a DOM. (Isolation from DOM tree, although it’s a part of it.)

Slide 9

Slide 9 text

Shadow DOM (cont’d) @hardikpthv Isolated DOM Scoped CSS Composition Simplifies CSS

Slide 10

Slide 10 text

Custom Element with Shadow DOM @hardikpthv class FrontManiaDrawer extends HTMLElement { constructor() { super(); const shadowRoot = this.attachShadow({mode: 'open'}); shadowRoot.innerHTML = ` #tabs { ... }
...
...
`; } ... } front-mania-drawer.js

Slide 11

Slide 11 text

Who uses the WebComponents!/?

Slide 12

Slide 12 text

…and many more

Slide 13

Slide 13 text

Slide 14

Slide 14 text

● HTML templating library for JavaScript (No VDOM) ● Lets you write HTML templates in JavaScript using `template literals` ● Lazily rendered ● Possible to style, bind to attributes, properties, and add event listeners lit-html @hardikpthv // Import lit-html import {html, render} from 'lit-html'; // Define a template const eventTemplate = (eventName) => html`

Welcome to ${eventName}

`; // Render the template to the document render(eventTemplate('FrontMania'), document.body); index.js

Slide 15

Slide 15 text

Example (cont’d) @hardikpthv (eventName) => html`

Welcome to ${eventName}

`; TEMPLATE

Welcome to {{}}

PARSED

Welcome to

PARSED Joins all the literal parts with a special placeholder, similar to "{{}}" Creates a ``

Slide 16

Slide 16 text

LitElement

Slide 17

Slide 17 text

● A simple base class ● Lets you create fast, lightweight web components ● Work in any web page with any framework ● Uses lit-html to render ● Reactive properties, async updates for elements, life cycle hooks. LitElement @hardikpthv import { LitElement, html } from 'lit-element'; class FrontManiaDrawer extends LitElement { render(){ ... } } customElements.define('front-mania-drawer', FrontManiaDrawer); front-mania-drawer.js

Slide 18

Slide 18 text

Front-Mania Drawer as LitElement @hardikpthv import { LitElement, html } from 'lit-element'; class FrontManiaDrawer extends LitElement { render(){ return html`

2019 FrontMania

`; } } customElements.define('front-mania-drawer', FrontManiaDrawer); front-mania-drawer.js

Slide 19

Slide 19 text

Tool and Best Practices

Slide 20

Slide 20 text

OpenWC ● To encourage developers with efficient and futuristic tools ● Set of recommendations and standard practices for: ○ Developing: Dev-server, Scaffold ○ Linting: ESLint, Prettier, Types ○ Testing: Karma, Browserstack, Wallaby ○ Building: Rollup, Webpack ○ Demoing: Storybook ○ Publishing: Netlify ○ Automating: CircleCI @hardikpthv

Slide 21

Slide 21 text

OpenWC (cont’d) @hardikpthv npm init @open-wc BEGIN WITH Requires Node 10 or > and NPM 6 or >

Slide 22

Slide 22 text

Slide 23

Slide 23 text

View @ bit.ly/lit-movies

Slide 24

Slide 24 text

Compare DX with React

Slide 25

Slide 25 text

View @ github.com/react-movies View @ github.com/lit-movies

Slide 26

Slide 26 text

Thank You Hardik Pithva @hardikpthv

Slide 27

Slide 27 text

Get the content and stay tuned ● Slides @ bit.ly/start-with-lit ● Repo. @ github.com/online-edu/lit-movies ● Demo @ bit.ly/lit-movies ● Follow @ github.com/hardikpthv or speakerdeck.com/hardikpthv ● Resources: ○ open-wc.org ○ lit-element.polymer-project.org ○ lit-html.polymer-project.org