Last active
June 7, 2023 13:29
-
-
Save ryandejaegher/aa731ccc28914d73894802890b12bea4 to your computer and use it in GitHub Desktop.
LitJS without build #javascript #webcomponent #lit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "scripts": [], | |
| "styles": [] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| doctype html | |
| html(lang="en") | |
| head | |
| meta(charset="UTF-8") | |
| meta(http-equiv="X-UA-Compatible", content="IE=edge") | |
| meta(name="viewport", content="width=device-width, initial-scale=1.0") | |
| title Document | |
| body | |
| h1 Hello World | |
| my-element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { LitElement, css, html } from 'https://cdn.skypack.dev/lit'; | |
| export class MyElement extends LitElement { | |
| static styles = css` | |
| h2 { | |
| color: blue; | |
| } | |
| .active { | |
| color: red; | |
| } | |
| `; | |
| static properties = { | |
| name: { type: String }, | |
| demo: { type: Boolean }, | |
| }; | |
| constructor() { | |
| super(); | |
| this.name = 'Dave'; | |
| this.demo = true; | |
| this.list = ['one', 'two', 'three']; | |
| this.renderList = this.renderList.bind(this); | |
| } | |
| renderList() { | |
| return html` | |
| <ul> | |
| ${this.list.map(item => { | |
| return html`<li>${item}</li>`; | |
| })} | |
| </ul> | |
| `; | |
| } | |
| render() { | |
| return html`<h2 class="${this.demo ? 'active' : null}"> | |
| Hello ${this.name}! ${this.demo ? 'π' : null} | |
| </h2> | |
| ${this.renderList()} | |
| <button | |
| @click=${() => { | |
| this.demo = !this.demo; | |
| console.log(this.demo); | |
| console.log('clicked'); | |
| // this.list.push('four'); | |
| }} | |
| > | |
| Click | |
| </button> `; | |
| } | |
| } | |
| customElements.define('my-element', MyElement); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| βββ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment