# @mdi/element

> Web Component Element for TypeScript

Latest version **0.0.20** (published 2021-01-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @mdi/element
pnpm add @mdi/element
yarn add @mdi/element
bun add @mdi/element
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.20 |
| Published | 2021-01-23 |
| First published | 2020-04-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 23.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Austin Andrews |
| Maintainers | mdi-owner, templarian, goyney, mrgrigri, peternoble, greenturtwig |
| Keywords | web, component, htmlelement |

## Links

- npm: https://www.npmjs.com/package/@mdi/element
- Repository: https://github.com/Templarian/Element
- Homepage: https://github.com/Templarian/Element#readme
- Issues: https://github.com/Templarian/Element/issues
- npm.io page: https://npm.io/package/@mdi/element

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.0.20 (latest) — 2021-01-23
- 0.0.19 — 2021-01-23
- 0.0.18 — 2020-11-15
- 0.0.17 — 2020-10-25
- 0.0.16 — 2020-10-25
- 0.0.15 — 2020-09-01
- 0.0.14 — 2020-09-01
- 0.0.13 — 2020-09-01
- 0.0.12 — 2020-08-20
- 0.0.11 — 2020-08-20
- 0.0.10 — 2020-08-15
- 0.0.9 — 2020-06-27
- 0.0.8 — 2020-06-08
- 0.0.7 — 2020-06-07
- 0.0.6 — 2020-04-23
- … 5 more at https://npm.io/package/@mdi/element/versions

## README

# Element

Simple TypeScript wrapper for creating a Web Component.

```bash
npm install @mdi/element
```

Example Usage: [Element-Hello-World](https://github.com/Templarian/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`)

```typescript
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`)

```html
<div part="message">Default!</div>
```

### CSS Styles (`world.css`)

```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`)

```typescript
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`)

```html
<button part="button">
  <parent/> <!-- <div>Default!</div> -->
</button>
```

### CSS Styles (`worldButton.css`)

```css
[part~=button] {
  border-radius: 0.25rem;
  border: #ddd;
  color: #222;
}
```

### `@Local(initialValue[, key])`

To access localStorage values bind them to variable.

```js
// 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.

```typescript
import { Component } from '@mdi/element';

@Component()
export default class HelloOverlay extends HtmlElement {
  static open() {

  }

  close() {

  }
}
```

---
_Source: https://npm.io/package/@mdi/element · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
