Skip to content

Commit e3010cc

Browse files
committed
add typescript definitions
1 parent fa74367 commit e3010cc

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Wrap your component up as a custom element",
55
"main": "dist/preact-custom-element.js",
66
"module": "dist/preact-custom-element.esm.js",
7+
"types": "types/index.d.ts",
78
"unpkg": "dist/preact-custom-element.umd.js",
89
"source": "src/index.js",
910
"scripts": {

types/index.d.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type {
2+
FunctionComponent,
3+
FunctionalComponent,
4+
ComponentClass,
5+
} from 'preact';
6+
7+
export type RegisterOptions =
8+
| {
9+
/**
10+
* Default: do not use shadow-dom for rendering
11+
*/
12+
shadow: false;
13+
}
14+
| {
15+
/**
16+
* Use shadow-dom for rendering
17+
*/
18+
shadow: true;
19+
/**
20+
* A string specifying the encapsulation mode for the shadow DOM tree.
21+
* Default mode is `"open"`
22+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#mode
23+
*/
24+
mode?: 'open' | 'closed';
25+
};
26+
27+
/**
28+
* Register a preact component as web-component.
29+
* @param componentDefinition The preact component to register
30+
* @param tagName The HTML element tag-name (must contain a hyphen and be lowercase)
31+
* @param observedAttributes HTML element attributes to observe
32+
* @param options Additional element options
33+
* @example
34+
* ```ts
35+
* // use custom web-component class
36+
* class PreactWebComponent extends Component {
37+
* static tagName = 'my-web-component';
38+
* render() {
39+
* return <p>Hello world!</p>
40+
* }
41+
* }
42+
*
43+
* register(PreactComponent);
44+
*
45+
* // use a preact component
46+
* function PreactComponent({ prop }) {
47+
* return <p>Hello {prop}!</p>
48+
* }
49+
*
50+
* register(PreactComponent, 'my-component');
51+
* register(PreactComponent, 'my-component', ['prop']);
52+
* register(PreactComponent, 'my-component', ['prop'], {
53+
* shadow: true,
54+
* mode: 'closed'
55+
* });
56+
* ```
57+
*/
58+
declare function register(
59+
componentDefinition:
60+
| FunctionComponent<any>
61+
| ComponentClass<any>
62+
| FunctionalComponent<any>,
63+
tagName?: string,
64+
observedAttributes?: string[],
65+
options?: RegisterOptions
66+
): void;
67+
68+
export default register;

0 commit comments

Comments
 (0)