Skip to content

Commit f5704b7

Browse files
committed
fix: Types
1 parent d8d4b0f commit f5704b7

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/index.d.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
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-
};
1+
import { AnyComponent } from 'preact';
92

103
type Options =
114
| {
@@ -46,9 +39,11 @@ type Options =
4639
* const klass = register(PreactComponent, 'my-component');
4740
* ```
4841
*/
49-
export default function register<P = {}, S = {}>(
42+
declare function register<P = {}, S = {}>(
5043
Component: AnyComponent<P, S>,
5144
tagName?: string,
5245
propNames?: (keyof P)[],
5346
options?: Options
5447
): HTMLElement;
48+
49+
export = register;

src/index.js

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

33
/**
4-
* @typedef {import('./index.d.ts').PreactCustomElement} PreactCustomElement
4+
* @typedef {import('./internal.d.ts').PreactCustomElement} PreactCustomElement
55
*/
66

77
/**
8-
* @type {import('./index.d.ts').default}
8+
* @type {import('./index.d.ts')}
99
*/
1010
export default function register(Component, tagName, propNames, options) {
1111
function PreactElement() {
1212
const inst = /** @type {PreactCustomElement} */ (
1313
Reflect.construct(HTMLElement, [], PreactElement)
1414
);
1515
inst._vdomComponent = Component;
16-
inst._root =
17-
options && options.shadow
18-
? inst.attachShadow({ mode: options.mode || 'open' })
19-
: inst;
2016

21-
if (options && options.adoptedStyleSheets) {
22-
inst._root.adoptedStyleSheets = options.adoptedStyleSheets;
17+
if (options && options.shadow) {
18+
inst._root = inst.attachShadow({ mode: options.mode || 'open' });
19+
20+
if (options.adoptedStyleSheets) {
21+
inst._root.adoptedStyleSheets = options.adoptedStyleSheets;
22+
}
23+
} else {
24+
inst._root = inst;
2325
}
2426

2527
return inst;
@@ -49,9 +51,7 @@ export default function register(Component, tagName, propNames, options) {
4951
propNames.forEach((name) => {
5052
Object.defineProperty(PreactElement.prototype, name, {
5153
get() {
52-
return this._vdom
53-
? this._vdom.props[name]
54-
: this._props[name];
54+
return this._vdom ? this._vdom.props[name] : this._props[name];
5555
},
5656
set(v) {
5757
if (this._vdom) {

src/internal.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { h, AnyComponent } from 'preact';
2+
3+
export type PreactCustomElement = HTMLElement & {
4+
_root: ShadowRoot | HTMLElement;
5+
_vdomComponent: AnyComponent;
6+
_vdom: ReturnType<typeof h> | null;
7+
_props: Record<string, unknown>;
8+
};

0 commit comments

Comments
 (0)