|
| 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 | +- ✅ Typed custom element class definitions |
| 11 | +- ✅ Typed static properties and lifecycle methods |
| 12 | +- ✅ Typed custom element constructors |
| 13 | +- ✅ Type-safe interaction with the Custom Elements Registry |
| 14 | +- ✅ Zero dependencies |
| 15 | +- ✅ ESM compatible |
| 16 | + |
| 17 | +👉 [Try it now in StackBlitz](https://stackblitz.com/github/jsxtools/typed-custom-elements/tree/main/demo) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +```shell |
| 22 | +npm install typed-custom-elements |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Basic Custom Elements |
| 28 | + |
| 29 | +```ts |
| 30 | +import type { CustomElement } from "typed-custom-elements" |
| 31 | + |
| 32 | +class MyElement extends HTMLElement implements CustomElement { |
| 33 | + // type safety and code completion for static propertes, lifecycle methods, etc. |
| 34 | + static formAssociated = true |
| 35 | + static observedAttributes = ["name"] |
| 36 | + |
| 37 | + attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) { |
| 38 | + // type-safe callback handling |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +### Custom Element Constructors |
| 44 | + |
| 45 | +```ts |
| 46 | +import type { CustomElementConstructor } from "typed-custom-elements" |
| 47 | + |
| 48 | +const MyElementConstructor: CustomElementConstructor = class extends HTMLElement { |
| 49 | + // type safety and code completion for static propertes, lifecycle methods, etc. |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Custom Elements Registry |
| 54 | + |
| 55 | +```ts |
| 56 | +import type { CustomElementRegistry } from "typed-custom-elements" |
| 57 | + |
| 58 | +declare const customElements: CustomElementRegistry |
| 59 | + |
| 60 | +// type-safe custom element registration |
| 61 | +customElements.define('my-element', class extends HTMLElement { |
| 62 | + // type safety, code completion, you get the point |
| 63 | +}) |
| 64 | +``` |
| 65 | + |
| 66 | +## Why Use Typed Custom Elements? |
| 67 | + |
| 68 | +Working with Web Components in TypeScript often means relying on implicit or loosely typed structures. |
| 69 | +This package brings clarity and confidence to your component authoring by enforcing: |
| 70 | + |
| 71 | +- Proper typing for lifecycle callbacks. |
| 72 | +- Static property inference (`formAssociated`, `observedAttributes`). |
| 73 | +- Safer registration and instantiation via constructors and registries. |
| 74 | + |
| 75 | +## License |
| 76 | + |
| 77 | +This project is licensed under the [MIT No Attribution License](https://opensource.org/license/mit-0). |
| 78 | + |
| 79 | +[npm-img]: https://img.shields.io/npm/v/typed-custom-elements |
| 80 | +[npm-url]: https://www.npmjs.com/package/typed-custom-elements |
| 81 | +[cli-img]: https://github.com/jsxtools/typed-custom-elements/actions/workflows/check.yml/badge.svg |
| 82 | +[cli-url]: https://github.com/jsxtools/typed-custom-elements/actions/workflows/check.yml |
0 commit comments