Skip to content

Commit 986f340

Browse files
authored
refactor: Switch to .d.ts for types (#100)
* refactor: Switch to '.d.ts' for declaring types * chore: Stop Microbundle from trying to generate types * chore: Move jsdoc example comment into ts definition where it can be used * chore: Make sure to install 'git-simple-hooks'
1 parent 52daae4 commit 986f340

File tree

4 files changed

+74
-34
lines changed

4 files changed

+74
-34
lines changed

jsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "NodeNext",
5+
"moduleResolution": "Node",
6+
"noEmit": true,
7+
"allowJs": true,
8+
"checkJs": true,
9+
"skipLibCheck": false,
10+
"jsx": "react",
11+
"jsxFactory": "h",
12+
"jsxFragmentFactory": "Fragment",
13+
}
14+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"version": "4.4.0",
44
"description": "Wrap your component up as a custom element",
55
"source": "src/index.js",
6-
"types": "dist/index.d.ts",
6+
"types": "src/index.d.ts",
77
"main": "dist/preact-custom-element.js",
88
"module": "dist/preact-custom-element.esm.js",
99
"unpkg": "dist/preact-custom-element.umd.js",
1010
"scripts": {
11-
"build": "microbundle -f cjs,es,umd",
11+
"prepare": "npx simple-git-hooks",
12+
"build": "microbundle -f cjs,es,umd --no-generateTypes",
1213
"lint": "eslint src/*.{js,jsx}",
1314
"test": "wtr src/*.test.{js,jsx}",
1415
"prettier": "prettier **/*.{js,jsx} --write",
@@ -51,6 +52,7 @@
5152
"bugs": "https://github.com/preactjs/preact-custom-element/issues",
5253
"homepage": "https://github.com/preactjs/preact-custom-element",
5354
"files": [
55+
"src",
5456
"dist",
5557
"LICENSE",
5658
"package.json",

src/index.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { h, AnyComponent } from 'preact';
2+
3+
type PreactCustomElement = HTMLElement & {
4+
_root: ShadowRoot | HTMLElement;
5+
_vdomComponent: AnyComponent;
6+
_vdom: ReturnType<typeof h> | null;
7+
_props: Record<string, unknown>;
8+
};
9+
10+
type Options =
11+
| {
12+
shadow: false;
13+
}
14+
| {
15+
shadow: true;
16+
mode?: 'open' | 'closed';
17+
adoptedStyleSheets?: CSSStyleSheet[];
18+
};
19+
20+
/**
21+
* Register a preact component as web-component.
22+
*
23+
* @example
24+
* ```jsx
25+
* // use custom web-component class
26+
* class PreactWebComponent extends Component {
27+
* static tagName = 'my-web-component';
28+
* render() {
29+
* return <p>Hello world!</p>
30+
* }
31+
* }
32+
*
33+
* register(PreactComponent);
34+
*
35+
* // use a preact component
36+
* function PreactComponent({ prop }) {
37+
* return <p>Hello {prop}!</p>
38+
* }
39+
*
40+
* register(PreactComponent, 'my-component');
41+
* register(PreactComponent, 'my-component', ['prop']);
42+
* register(PreactComponent, 'my-component', ['prop'], {
43+
* shadow: true,
44+
* mode: 'closed'
45+
* });
46+
* ```
47+
*/
48+
export default function register(
49+
Component: AnyComponent,
50+
tagName?: string,
51+
propNames?: string[],
52+
options?: Options
53+
): void;

src/index.js

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
import { h, cloneElement, render, hydrate } from 'preact';
22

33
/**
4-
* @typedef {import('preact').FunctionComponent<any> | import('preact').ComponentClass<any> | import('preact').FunctionalComponent<any> } ComponentDefinition
5-
* @typedef {{ shadow: false } | { shadow: true, mode?: 'open' | 'closed', adoptedStyleSheets?: CSSStyleSheet[] }} Options
6-
* @typedef {HTMLElement & { _root: ShadowRoot | HTMLElement, _vdomComponent: ComponentDefinition, _vdom: ReturnType<typeof import("preact").h> | null }} PreactCustomElement
4+
* @typedef {import('./index.d.ts').PreactCustomElement} PreactCustomElement
75
*/
86

7+
98
/**
10-
* Register a preact component as web-component.
11-
* @param {ComponentDefinition} Component The preact component to register
12-
* @param {string} [tagName] The HTML element tag-name (must contain a hyphen and be lowercase)
13-
* @param {string[]} [propNames] HTML element attributes to observe
14-
* @param {Options} [options] Additional element options
15-
* @example
16-
* ```ts
17-
* // use custom web-component class
18-
* class PreactWebComponent extends Component {
19-
* static tagName = 'my-web-component';
20-
* render() {
21-
* return <p>Hello world!</p>
22-
* }
23-
* }
24-
*
25-
* register(PreactComponent);
26-
*
27-
* // use a preact component
28-
* function PreactComponent({ prop }) {
29-
* return <p>Hello {prop}!</p>
30-
* }
31-
*
32-
* register(PreactComponent, 'my-component');
33-
* register(PreactComponent, 'my-component', ['prop']);
34-
* register(PreactComponent, 'my-component', ['prop'], {
35-
* shadow: true,
36-
* mode: 'closed'
37-
* });
38-
* ```
9+
* @type {import('./index.d.ts').default}
3910
*/
4011
export default function register(Component, tagName, propNames, options) {
4112
function PreactElement() {

0 commit comments

Comments
 (0)