|
| 1 | +# Typed Custom Elements |
| 2 | + |
| 3 | +[![NPM Version][npm-img]][npm-url] |
| 4 | +[![Build Status][cli-img]][cli-url] |
| 5 | + |
| 6 | +**Typed Custom Elements** is a collection of TypeScript types to help you author type-safe Web Components, with proper type checking for custom elements, their constructors, and the custom elements registry. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- Type-safe custom element definitions |
| 11 | +- Proper typing for custom element constructors |
| 12 | +- Type-safe custom elements registry |
| 13 | +- Full TypeScript support |
| 14 | +- Zero dependencies |
| 15 | +- ESM compatible |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```shell |
| 20 | +npm install typed-custom-elements |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +### Basic Custom Elements |
| 26 | + |
| 27 | +```ts |
| 28 | +import type { CustomElement } from "typed-custom-elements" |
| 29 | + |
| 30 | +class MyElement extends HTMLElement implements CustomElement { |
| 31 | + // code completion and type safety for static propertes |
| 32 | + static formAssociated = true |
| 33 | + static observedAttributes = ["name"] |
| 34 | + |
| 35 | + attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) { |
| 36 | + // type-safe callback handling |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Custom Element Constructors |
| 42 | + |
| 43 | +```ts |
| 44 | +import type { CustomElementConstructor } from "typed-custom-elements" |
| 45 | + |
| 46 | +const MyElementConstructor: CustomElementConstructor = class extends HTMLElement { |
| 47 | + // custom element implementation |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Custom Elements Registry |
| 52 | + |
| 53 | +```ts |
| 54 | +import type { CustomElementRegistry } from "typed-custom-elements" |
| 55 | + |
| 56 | +declare const customElements: CustomElementRegistry |
| 57 | + |
| 58 | +// type-safe custom element registration |
| 59 | +customElements.define('my-element', class extends HTMLElement { |
| 60 | + // code completion and type safety for static propertes, lifecycle methods, etc. |
| 61 | +}) |
| 62 | +``` |
| 63 | + |
| 64 | +## Type Safety |
| 65 | + |
| 66 | +This package provides type safety for: |
| 67 | + |
| 68 | +- Custom element class definitions |
| 69 | +- Custom element constructors |
| 70 | +- Custom element registry operations |
| 71 | +- Attribute handling |
| 72 | +- Lifecycle methods |
| 73 | + |
| 74 | +## License |
| 75 | + |
| 76 | +This project is licensed under the [MIT No Attribution License](https://opensource.org/license/mit-0). |
| 77 | + |
| 78 | +[npm-img]: https://img.shields.io/npm/v/typed-custom-elements |
| 79 | +[npm-url]: https://www.npmjs.com/package/typed-custom-elements |
| 80 | +[cli-img]: https://github.com/jsxtools/typed-custom-elements/actions/workflows/check.yml/badge.svg |
| 81 | +[cli-url]: https://github.com/jsxtools/typed-custom-elements/actions/workflows/check.yml |
0 commit comments