Skip to content

Commit bd1a0d8

Browse files
committed
0.0.1
0 parents  commit bd1a0d8

16 files changed

+404
-0
lines changed

.github/workflows/check.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Check
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
check:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: Setup Bun
19+
uses: oven-sh/setup-bun@v1
20+
with:
21+
bun-version: latest
22+
23+
- name: Install dependencies
24+
run: bun install
25+
26+
- name: Validate TypeScript types
27+
run: bun run check
28+
29+
- name: Test TypeScript types
30+
run: bun run test

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
*.lock*
3+
*.log
4+
node_modules

.vscode/settings.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"editor.codeActionsOnSave": {
3+
"source.fixAll.biome": "always",
4+
"source.organizeImports.biome": "always"
5+
},
6+
"editor.defaultFormatter": "biomejs.biome",
7+
"editor.formatOnPaste": true,
8+
"editor.formatOnSave": true,
9+
"editor.detectIndentation": false,
10+
"editor.insertSpaces": false,
11+
"editor.tabSize": 4,
12+
"files.exclude": {
13+
"node_modules": true,
14+
"bun.lock*": true
15+
},
16+
"[javascript]": {
17+
"editor.defaultFormatter": "biomejs.biome"
18+
},
19+
"[json]": {
20+
"editor.defaultFormatter": "biomejs.biome"
21+
},
22+
"[typescript]": {
23+
"editor.defaultFormatter": "biomejs.biome"
24+
}
25+
}

CustomElement.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** Base class for a custom element. */
2+
export declare class CustomElement extends HTMLElement {
3+
/** List of attributes to observe for changes, invoking `attributeChangedCallback`. */
4+
static observedAttributes?: string[]
5+
6+
/** Indicates whether the custom element participates in form submission. */
7+
static formAssociated?: boolean
8+
9+
/** Called when one of the element's observed attributes changes. */
10+
attributeChangedCallback?(name: string, oldValue: string | null, newValue: string | null): void
11+
12+
/** Called when the element is added to a document. */
13+
connectedCallback?(): void
14+
15+
/** Called when the element is removed from a document. */
16+
disconnectedCallback?(): void
17+
18+
/** Called when the element is associated or disassociated with a form. */
19+
formAssociatedCallback?(form: HTMLFormElement | null): void
20+
21+
/** Called when the disabled state of the element changes. */
22+
formDisabledCallback?(isDisabled: boolean): void
23+
24+
/** Called when the associated form is reset. */
25+
formResetCallback?(): void
26+
27+
/** Called when the browser automatically fills out the element. */
28+
formStateRestoreCallback?(state: string | File | FormData, reason: "autocomplete" | "restore"): void
29+
}

CustomElement.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

CustomElementConstructor.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { CustomElement } from './CustomElement.d.ts'
2+
3+
/** Constructor interface for custom elements. */
4+
export interface CustomElementConstructor<T = CustomElement> {
5+
/** Creates a new instance of the custom element. */
6+
new (...args: any[]): HTMLElement & T
7+
8+
/** List of attributes to observe for changes, invoking `attributeChangedCallback`. */
9+
observedAttributes?: string[]
10+
11+
/** Indicates whether the custom element participates in form submission. */
12+
formAssociated?: boolean
13+
}

CustomElementConstructor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

CustomElementRegistry.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { CustomElementConstructor } from "./CustomElementConstructor.d.ts"
2+
3+
/** Provides methods for registering custom elements and querying registered elements. [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry) */
4+
export declare class CustomElementRegistry {
5+
/** Adds a definition for a custom element to the custom element registry. [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry/define) */
6+
define(name: string, constructor: CustomElementConstructor, options?: ElementDefinitionOptions): void
7+
8+
/** Returns the constructor for a previously-defined custom element. [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry/get) */
9+
get(name: string): CustomElementConstructor | undefined
10+
11+
/** Returns the name for a previously-defined custom element. [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry/getName) */
12+
getName(constructor: CustomElementConstructor): string | null
13+
14+
/** Upgrades all shadow-containing custom elements in the given root. [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry/upgrade) */
15+
upgrade(root: Node): void
16+
17+
/** Returns a Promise that resolves when the named element is defined. [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomElementRegistry/whenDefined) */
18+
whenDefined(name: string): Promise<CustomElementConstructor>
19+
}

CustomElementRegistry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)