0.0.20 ā€¢ Published 3 years ago

@mdi/element v0.0.20

Weekly downloads
28
License
MIT
Repository
github
Last release
3 years ago

Element

Simple TypeScript wrapper for creating a Web Component.

npm install @mdi/element

Example Usage: Element-Hello-World

Basics

To make things easier setup the project assuming the custom element <hello-world message="Hello World!"></hello-world>.

šŸ“‚ src/
  šŸ“‚ hello/
    šŸ“‚ world/
      šŸ“ƒ world.ts
      šŸ“ƒ world.html
      šŸ“ƒ world.css
šŸ“ƒ rollup.config.js
šŸ“ƒ tsconfig.json
šŸ“ƒ package.json

Class (world.ts)

import { Component, Prop, Part } from '@mdi/element';

import template from "./world.html";
import style from './world.css';

@Component({
  selector: 'hello-world',
  style,
  template
})
export default class HelloWorld extends HTMLElement {
  @Prop() message = 'Hello World';
  
  @Part() $message: HTMLDivElement;
  
  render(changes) {
    if (changes.message) {
      this.$message.innerText = this.message;
    }
  }
}

Template (world.html)

<div part="message">Default!</div>

CSS Styles (world.css)

:host {
  display: block;
}
[part~=message] {
  /* Style Part */
}

Advanced

Starting with a simple component can allow one to extend it with more features later on. This can be done by extending components.

šŸ“‚ src/
  šŸ“‚ hello/
    šŸ“‚ world/
      šŸ“ƒ world.ts
      šŸ“ƒ world.html
      šŸ“ƒ world.css
    šŸ“‚ worldButton/
      šŸ“ƒ worldButton.ts
      šŸ“ƒ worldButton.html
      šŸ“ƒ worldButton.css

TypeScript (worldButton.ts)

import { Component, Prop, Part } from '@mdi/element';
import HelloWorld from '../world/world';

import style from './worldButton.css';
import template from './worldButton.html';

@Component({
  selector: 'hello-world-button',
  style,
  template
})
export default class HelloWorldButton extends HelloWorld {
  @Part() $button: HTMLButtonElement;

  renderCallback() {
    this.$button.addEventListener('click', () => {
      alert(this.message);
    });
  }
}

Template (worldButton.html)

<button part="button">
  <parent/> <!-- <div>Default!</div> -->
</button>

CSS Styles (worldButton.css)

[part~=button] {
  border-radius: 0.25rem;
  border: #ddd;
  color: #222;
}

@Local(initialValue[, key])

To access localStorage values bind them to variable.

// Default to 42
@Local('42') foo;
// Default to 42 and share a global key
@Local('42', 'sharedKeyName') foo;

Development

# Build
npm run build
# View files in dist/
# Then link for use locally
npm link
# Within a local project directory
npm link @mdi/element

Other

Some other notes about unique use cases that are handled.

Optional Component() Config

Utility base classes can be defined without a config. These are rarely used, but are supported.

import { Component } from '@mdi/element';

@Component()
export default class HelloOverlay extends HtmlElement {
  static open() {

  }

  close() {

  }
}
0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago