UNPKG

5.09 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 DevSource {
19 fileName: string;
20 lineNumber: number;
21}
22
23export interface ErrorInfo {
24 componentStack?: string;
25}
26
27export interface Options extends preact.Options {
28 /** Attach a hook that is invoked before render, mainly to check the arguments. */
29 _root?(
30 vnode: ComponentChild,
31 parent: Element | Document | ShadowRoot | DocumentFragment
32 ): void;
33 /** Attach a hook that is invoked before a vnode is diffed. */
34 _diff?(vnode: VNode): void;
35 /** Attach a hook that is invoked after a tree was mounted or was updated. */
36 _commit?(vnode: VNode, commitQueue: Component[]): void;
37 /** Attach a hook that is invoked before a vnode has rendered. */
38 _render?(vnode: VNode): void;
39 /** Attach a hook that is invoked before a hook's state is queried. */
40 _hook?(component: Component, index: number, type: HookType): void;
41 /** Bypass effect execution. Currenty only used in devtools for hooks inspection */
42 _skipEffects?: boolean;
43 /** Attach a hook that is invoked after an error is caught in a component but before calling lifecycle hooks */
44 _catchError(
45 error: any,
46 vnode: VNode,
47 oldVNode: VNode | undefined,
48 errorInfo: ErrorInfo | undefined
49 ): void;
50}
51
52export type ComponentChild =
53 | VNode<any>
54 | string
55 | number
56 | boolean
57 | null
58 | undefined;
59export type ComponentChildren = ComponentChild[] | ComponentChild;
60
61export interface FunctionComponent<P = {}> extends preact.FunctionComponent<P> {
62 // Internally, createContext uses `contextType` on a Function component to
63 // implement the Consumer component
64 contextType?: PreactContext;
65
66 // Internally, createContext stores a ref to the context object on the Provider
67 // Function component to help devtools
68 _contextRef?: PreactContext;
69
70 // Define these properties as undefined on FunctionComponent to get rid of
71 // some errors in `diff()`
72 getDerivedStateFromProps?: undefined;
73 getDerivedStateFromError?: undefined;
74}
75
76export interface ComponentClass<P = {}> extends preact.ComponentClass<P> {
77 _contextRef?: any;
78
79 // Override public contextType with internal PreactContext type
80 contextType?: PreactContext;
81}
82
83// Redefine ComponentType using our new internal FunctionComponent interface above
84export type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
85
86export interface PreactElement extends HTMLElement {
87 _children?: VNode<any> | null;
88 /** Event listeners to support event delegation */
89 _listeners?: Record<string, (e: Event) => void>;
90
91 // Preact uses this attribute to detect SVG nodes
92 ownerSVGElement?: SVGElement | null;
93
94 // style: HTMLElement["style"]; // From HTMLElement
95
96 data?: string | number; // From Text node
97}
98
99// We use the `current` property to differentiate between the two kinds of Refs so
100// internally we'll define `current` on both to make TypeScript happy
101type RefObject<T> = { current: T | null };
102type RefCallback<T> = { (instance: T | null): void; current: undefined };
103type Ref<T> = RefObject<T> | RefCallback<T>;
104
105export interface VNode<P = {}> extends preact.VNode<P> {
106 // Redefine type here using our internal ComponentType type
107 type: string | ComponentType<P>;
108 props: P & { children: ComponentChildren };
109 ref?: Ref<any> | null;
110 _children: Array<VNode<any>> | null;
111 _parent: VNode | null;
112 _depth: number | null;
113 /**
114 * The [first (for Fragments)] DOM child of a VNode
115 */
116 _dom: PreactElement | null;
117 /**
118 * The last dom child of a Fragment, or components that return a Fragment
119 */
120 _nextDom: PreactElement | null;
121 _component: Component | null;
122 _hydrating: boolean | null;
123 constructor: undefined;
124 _original: number;
125}
126
127export interface Component<P = {}, S = {}> extends preact.Component<P, S> {
128 // When component is functional component, this is reset to functional component
129 constructor: ComponentType<P>;
130 state: S; // Override Component["state"] to not be readonly for internal use, specifically Hooks
131 base?: PreactElement;
132
133 _dirty: boolean;
134 _force?: boolean;
135 _renderCallbacks: Array<() => void>; // Only class components
136 _stateCallbacks: Array<() => void>; // Only class components
137 _globalContext?: any;
138 _vnode?: VNode<P> | null;
139 _nextState?: S | null; // Only class components
140 /** Only used in the devtools to later dirty check if state has changed */
141 _prevState?: S | null;
142 /**
143 * Pointer to the parent dom node. This is only needed for top-level Fragment
144 * components or array returns.
145 */
146 _parentDom?: PreactElement | null;
147 // Always read, set only when handling error
148 _processingException?: Component<any, any> | null;
149 // Always read, set only when handling error. This is used to indicate at diffTime to set _processingException
150 _pendingError?: Component<any, any> | null;
151}
152
153export interface PreactContext extends preact.Context<any> {
154 _id: string;
155 _defaultValue: any;
156}