UNPKG

3.87 kBTypeScriptView Raw
1import * as preact from './index';
2
3export enum HookType {
4 useState = 1,
5 useReducer = 2,
6 useEffect = 3,
7 useLayoutEffect = 4,
8 useRef = 5,
9 useImperativeHandle = 6,
10 useMemo = 7,
11 useCallback = 8,
12 useContext = 9,
13 useErrorBoundary = 10,
14 // Not a real hook, but the devtools treat is as such
15 useDebugvalue = 11
16}
17
18export interface Options extends preact.Options {
19 /** Attach a hook that is invoked before render, mainly to check the arguments. */
20 _root?(
21 vnode: preact.ComponentChild,
22 parent: Element | Document | ShadowRoot | DocumentFragment
23 ): void;
24 /** Attach a hook that is invoked before a vnode is diffed. */
25 _diff?(vnode: VNode): void;
26 /** Attach a hook that is invoked after a tree was mounted or was updated. */
27 _commit?(vnode: VNode, commitQueue: Component[]): void;
28 /** Attach a hook that is invoked before a vnode has rendered. */
29 _render?(vnode: VNode): void;
30 /** Attach a hook that is invoked before a hook's state is queried. */
31 _hook?(component: Component, index: number, type: HookType): void;
32 /** Bypass effect execution. Currenty only used in devtools for hooks inspection */
33 _skipEffects?: boolean;
34 /** Attach a hook that is invoked after an error is caught in a component but before calling lifecycle hooks */
35 _catchError(error: any, vnode: VNode, oldVNode: VNode | undefined): void;
36}
37
38export interface FunctionalComponent<P = {}>
39 extends preact.FunctionComponent<P> {
40 // Define getDerivedStateFromProps as undefined on FunctionalComponent
41 // to get rid of some errors in `diff()`
42 getDerivedStateFromProps?: undefined;
43}
44
45// Redefine ComponentFactory using our new internal FunctionalComponent interface above
46export type ComponentFactory<P> =
47 | preact.ComponentClass<P>
48 | FunctionalComponent<P>;
49
50export interface PreactElement extends HTMLElement {
51 _children?: VNode<any> | null;
52 /** Event listeners to support event delegation */
53 _listeners: Record<string, (e: Event) => void>;
54
55 // Preact uses this attribute to detect SVG nodes
56 ownerSVGElement?: SVGElement | null;
57
58 // style: HTMLElement["style"]; // From HTMLElement
59
60 data?: string | number; // From Text node
61}
62
63export interface VNode<P = {}> extends preact.VNode<P> {
64 // Redefine type here using our internal ComponentFactory type
65 type: string | ComponentFactory<P>;
66 props: P & { children: preact.ComponentChildren };
67 _children: Array<VNode<any>> | null;
68 _parent: VNode | null;
69 _depth: number | null;
70 /**
71 * The [first (for Fragments)] DOM child of a VNode
72 */
73 _dom: PreactElement | Text | null;
74 /**
75 * The last dom child of a Fragment, or components that return a Fragment
76 */
77 _nextDom: PreactElement | Text | null;
78 _component: Component | null;
79 constructor: undefined;
80 _original?: VNode | null;
81}
82
83export interface Component<P = {}, S = {}> extends preact.Component<P, S> {
84 // When component is functional component, this is reset to functional component
85 constructor: preact.ComponentType<P>;
86 state: S; // Override Component["state"] to not be readonly for internal use, specifically Hooks
87 base?: PreactElement;
88
89 _dirty: boolean;
90 _force?: boolean;
91 _renderCallbacks: Array<() => void>; // Only class components
92 _globalContext?: any;
93 _vnode?: VNode<P> | null;
94 _nextState?: S | null; // Only class components
95 /** Only used in the devtools to later dirty check if state has changed */
96 _prevState?: S | null;
97 /**
98 * Pointer to the parent dom node. This is only needed for top-level Fragment
99 * components or array returns.
100 */
101 _parentDom?: PreactElement | null;
102 // Always read, set only when handling error
103 _processingException?: Component<any, any> | null;
104 // Always read, set only when handling error. This is used to indicate at diffTime to set _processingException
105 _pendingError?: Component<any, any> | null;
106}
107
108export interface PreactContext extends preact.Context<any> {
109 _id: string;
110 _defaultValue: any;
111}