1 | export as namespace preact;
|
2 |
|
3 | import { JSXInternal } from './jsx';
|
4 |
|
5 | export import JSX = JSXInternal;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export interface VNode<P = {}> {
|
12 | type: ComponentType<P> | string;
|
13 | props: P & { children: ComponentChildren };
|
14 | key: Key;
|
15 | |
16 |
|
17 |
|
18 |
|
19 | ref?: Ref<any> | null;
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 | startTime?: number;
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 | endTime?: number;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export type Key = string | number | any;
|
39 |
|
40 | export type RefObject<T> = { current: T | null };
|
41 | export type RefCallback<T> = (instance: T | null) => void;
|
42 | export type Ref<T> = RefObject<T> | RefCallback<T> | null;
|
43 |
|
44 | export type ComponentChild =
|
45 | | VNode<any>
|
46 | | object
|
47 | | string
|
48 | | number
|
49 | | bigint
|
50 | | boolean
|
51 | | null
|
52 | | undefined;
|
53 | export type ComponentChildren = ComponentChild[] | ComponentChild;
|
54 |
|
55 | export interface Attributes {
|
56 | key?: Key | undefined;
|
57 | jsx?: boolean | undefined;
|
58 | }
|
59 |
|
60 | export interface ClassAttributes<T> extends Attributes {
|
61 | ref?: Ref<T>;
|
62 | }
|
63 |
|
64 | export interface PreactDOMAttributes {
|
65 | children?: ComponentChildren;
|
66 | dangerouslySetInnerHTML?: {
|
67 | __html: string;
|
68 | };
|
69 | }
|
70 |
|
71 | export interface ErrorInfo {
|
72 | componentStack?: string;
|
73 | }
|
74 |
|
75 | export type RenderableProps<P, RefType = any> = P &
|
76 | Readonly<Attributes & { children?: ComponentChildren; ref?: Ref<RefType> }>;
|
77 |
|
78 | export type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
|
79 | export type ComponentFactory<P = {}> = ComponentType<P>;
|
80 |
|
81 | export type ComponentProps<
|
82 | C extends ComponentType<any> | keyof JSXInternal.IntrinsicElements
|
83 | > = C extends ComponentType<infer P>
|
84 | ? P
|
85 | : C extends keyof JSXInternal.IntrinsicElements
|
86 | ? JSXInternal.IntrinsicElements[C]
|
87 | : never;
|
88 |
|
89 | export interface FunctionComponent<P = {}> {
|
90 | (props: RenderableProps<P>, context?: any): VNode<any> | null;
|
91 | displayName?: string;
|
92 | defaultProps?: Partial<P> | undefined;
|
93 | }
|
94 | export interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}
|
95 |
|
96 | export interface ComponentClass<P = {}, S = {}> {
|
97 | new (props: P, context?: any): Component<P, S>;
|
98 | displayName?: string;
|
99 | defaultProps?: Partial<P>;
|
100 | contextType?: Context<any>;
|
101 | getDerivedStateFromProps?(
|
102 | props: Readonly<P>,
|
103 | state: Readonly<S>
|
104 | ): Partial<S> | null;
|
105 | getDerivedStateFromError?(error: any): Partial<S> | null;
|
106 | }
|
107 | export interface ComponentConstructor<P = {}, S = {}>
|
108 | extends ComponentClass<P, S> {}
|
109 |
|
110 |
|
111 | export type AnyComponent<P = {}, S = {}> =
|
112 | | FunctionComponent<P>
|
113 | | ComponentConstructor<P, S>;
|
114 |
|
115 | export interface Component<P = {}, S = {}> {
|
116 | componentWillMount?(): void;
|
117 | componentDidMount?(): void;
|
118 | componentWillUnmount?(): void;
|
119 | getChildContext?(): object;
|
120 | componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
|
121 | shouldComponentUpdate?(
|
122 | nextProps: Readonly<P>,
|
123 | nextState: Readonly<S>,
|
124 | nextContext: any
|
125 | ): boolean;
|
126 | componentWillUpdate?(
|
127 | nextProps: Readonly<P>,
|
128 | nextState: Readonly<S>,
|
129 | nextContext: any
|
130 | ): void;
|
131 | getSnapshotBeforeUpdate?(oldProps: Readonly<P>, oldState: Readonly<S>): any;
|
132 | componentDidUpdate?(
|
133 | previousProps: Readonly<P>,
|
134 | previousState: Readonly<S>,
|
135 | snapshot: any
|
136 | ): void;
|
137 | componentDidCatch?(error: any, errorInfo: ErrorInfo): void;
|
138 | }
|
139 |
|
140 | export abstract class Component<P, S> {
|
141 | constructor(props?: P, context?: any);
|
142 |
|
143 | static displayName?: string;
|
144 | static defaultProps?: any;
|
145 | static contextType?: Context<any>;
|
146 |
|
147 | // Static members cannot reference class type parameters. This is not
|
148 | // supported in TypeScript. Reusing the same type arguments from `Component`
|
149 | // will lead to an impossible state where one cannot satisfy the type
|
150 | // constraint under no circumstances, see #1356.In general type arguments
|
151 | // seem to be a bit buggy and not supported well at the time of this
|
152 | // writing with TS 3.3.3333.
|
153 | static getDerivedStateFromProps?(
|
154 | props: Readonly<object>,
|
155 | state: Readonly<object>
|
156 | ): object | null;
|
157 | static getDerivedStateFromError?(error: any): object | null;
|
158 |
|
159 | state: Readonly<S>;
|
160 | props: RenderableProps<P>;
|
161 | context: any;
|
162 | base?: Element | Text;
|
163 |
|
164 | // From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e836acc75a78cf0655b5dfdbe81d69fdd4d8a252/types/react/index.d.ts#L402
|
165 | // // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
|
166 | // // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
|
167 | setState<K extends keyof S>(
|
168 | state:
|
169 | | ((
|
170 | prevState: Readonly<S>,
|
171 | props: Readonly<P>
|
172 | ) => Pick<S, K> | Partial<S> | null)
|
173 | | (Pick<S, K> | Partial<S> | null),
|
174 | callback?: () => void
|
175 | ): void;
|
176 |
|
177 | forceUpdate(callback?: () => void): void;
|
178 |
|
179 | abstract render(
|
180 | props?: RenderableProps<P>,
|
181 | state?: Readonly<S>,
|
182 | context?: any
|
183 | ): ComponentChild;
|
184 | }
|
185 |
|
186 | //
|
187 | // Preact createElement
|
188 | // -----------------------------------
|
189 |
|
190 | export function createElement(
|
191 | type: 'input',
|
192 | props:
|
193 | | (JSXInternal.DOMAttributes<HTMLInputElement> &
|
194 | ClassAttributes<HTMLInputElement>)
|
195 | | null,
|
196 | ...children: ComponentChildren[]
|
197 | ): VNode<
|
198 | JSXInternal.DOMAttributes<HTMLInputElement> &
|
199 | ClassAttributes<HTMLInputElement>
|
200 | >;
|
201 | export function createElement<
|
202 | P extends JSXInternal.HTMLAttributes<T>,
|
203 | T extends HTMLElement
|
204 | >(
|
205 | type: keyof JSXInternal.IntrinsicElements,
|
206 | props: (ClassAttributes<T> & P) | null,
|
207 | ...children: ComponentChildren[]
|
208 | ): VNode<ClassAttributes<T> & P>;
|
209 | export function createElement<
|
210 | P extends JSXInternal.SVGAttributes<T>,
|
211 | T extends HTMLElement
|
212 | >(
|
213 | type: keyof JSXInternal.IntrinsicElements,
|
214 | props: (ClassAttributes<T> & P) | null,
|
215 | ...children: ComponentChildren[]
|
216 | ): VNode<ClassAttributes<T> & P>;
|
217 | export function createElement<T extends HTMLElement>(
|
218 | type: string,
|
219 | props:
|
220 | | (ClassAttributes<T> &
|
221 | JSXInternal.HTMLAttributes &
|
222 | JSXInternal.SVGAttributes)
|
223 | | null,
|
224 | ...children: ComponentChildren[]
|
225 | ): VNode<
|
226 | ClassAttributes<T> & JSXInternal.HTMLAttributes & JSXInternal.SVGAttributes
|
227 | >;
|
228 | export function createElement<P>(
|
229 | type: ComponentType<P>,
|
230 | props: (Attributes & P) | null,
|
231 | ...children: ComponentChildren[]
|
232 | ): VNode<P>;
|
233 | export namespace createElement {
|
234 | export import JSX = JSXInternal;
|
235 | }
|
236 |
|
237 | export function h(
|
238 | type: 'input',
|
239 | props:
|
240 | | (JSXInternal.DOMAttributes<HTMLInputElement> &
|
241 | ClassAttributes<HTMLInputElement>)
|
242 | | null,
|
243 | ...children: ComponentChildren[]
|
244 | ): VNode<
|
245 | JSXInternal.DOMAttributes<HTMLInputElement> &
|
246 | ClassAttributes<HTMLInputElement>
|
247 | >;
|
248 | export function h<
|
249 | P extends JSXInternal.HTMLAttributes<T>,
|
250 | T extends HTMLElement
|
251 | >(
|
252 | type: keyof JSXInternal.IntrinsicElements,
|
253 | props: (ClassAttributes<T> & P) | null,
|
254 | ...children: ComponentChildren[]
|
255 | ): VNode<ClassAttributes<T> & P>;
|
256 | export function h<
|
257 | P extends JSXInternal.SVGAttributes<T>,
|
258 | T extends HTMLElement
|
259 | >(
|
260 | type: keyof JSXInternal.IntrinsicElements,
|
261 | props: (ClassAttributes<T> & P) | null,
|
262 | ...children: ComponentChildren[]
|
263 | ): VNode<ClassAttributes<T> & P>;
|
264 | export function h<T extends HTMLElement>(
|
265 | type: string,
|
266 | props:
|
267 | | (ClassAttributes<T> &
|
268 | JSXInternal.HTMLAttributes &
|
269 | JSXInternal.SVGAttributes)
|
270 | | null,
|
271 | ...children: ComponentChildren[]
|
272 | ): VNode<
|
273 | | (ClassAttributes<T> &
|
274 | JSXInternal.HTMLAttributes &
|
275 | JSXInternal.SVGAttributes)
|
276 | | null
|
277 | >;
|
278 | export function h<P>(
|
279 | type: ComponentType<P>,
|
280 | props: (Attributes & P) | null,
|
281 | ...children: ComponentChildren[]
|
282 | ): VNode<Attributes & P>;
|
283 | export namespace h {
|
284 | export import JSX = JSXInternal;
|
285 | }
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | interface ContainerNode {
|
291 | readonly nodeType: number;
|
292 | readonly parentNode: ContainerNode | null;
|
293 | readonly firstChild: ContainerNode | null;
|
294 | readonly childNodes: ArrayLike<ContainerNode>;
|
295 |
|
296 | contains(other: ContainerNode | null): boolean;
|
297 | insertBefore(node: ContainerNode, child: ContainerNode | null): ContainerNode;
|
298 | appendChild(node: ContainerNode): ContainerNode;
|
299 | removeChild(child: ContainerNode): ContainerNode;
|
300 | }
|
301 |
|
302 | export function render(vnode: ComponentChild, parent: ContainerNode): void;
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | export function render(
|
309 | vnode: ComponentChild,
|
310 | parent: ContainerNode,
|
311 | replaceNode?: Element | Text
|
312 | ): void;
|
313 | export function hydrate(vnode: ComponentChild, parent: ContainerNode): void;
|
314 | export function cloneElement(
|
315 | vnode: VNode<any>,
|
316 | props?: any,
|
317 | ...children: ComponentChildren[]
|
318 | ): VNode<any>;
|
319 | export function cloneElement<P>(
|
320 | vnode: VNode<P>,
|
321 | props?: any,
|
322 | ...children: ComponentChildren[]
|
323 | ): VNode<P>;
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | export const Fragment: FunctionComponent<{}>;
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | export interface Options {
|
340 |
|
341 | vnode?(vnode: VNode): void;
|
342 |
|
343 | unmount?(vnode: VNode): void;
|
344 |
|
345 | diffed?(vnode: VNode): void;
|
346 | event?(e: Event): any;
|
347 | requestAnimationFrame?(callback: () => void): void;
|
348 | debounceRendering?(cb: () => void): void;
|
349 | useDebugValue?(value: string | number): void;
|
350 | _addHookName?(name: string | number): void;
|
351 | __suspenseDidResolve?(vnode: VNode, cb: () => void): void;
|
352 | // __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
|
353 |
|
354 | /**
|
355 | * Customize attribute serialization when a precompiled JSX transform
|
356 | * is used.
|
357 | */
|
358 | attr?(name: string, value: any): string | void;
|
359 | }
|
360 |
|
361 | export const options: Options;
|
362 |
|
363 | //
|
364 | // Preact helpers
|
365 | // -----------------------------------
|
366 | export function createRef<T = any>(): RefObject<T>;
|
367 | export function toChildArray(
|
368 | children: ComponentChildren
|
369 | ): Array<VNode | string | number>;
|
370 | export function isValidElement(vnode: any): vnode is VNode;
|
371 |
|
372 | //
|
373 | // Context
|
374 | // -----------------------------------
|
375 | export interface Consumer<T>
|
376 | extends FunctionComponent<{
|
377 | children: (value: T) => ComponentChildren;
|
378 | }> {}
|
379 | export interface PreactConsumer<T> extends Consumer<T> {}
|
380 |
|
381 | export interface Provider<T>
|
382 | extends FunctionComponent<{
|
383 | value: T;
|
384 | children?: ComponentChildren;
|
385 | }> {}
|
386 | export interface PreactProvider<T> extends Provider<T> {}
|
387 | export type ContextType<C extends Context<any>> = C extends Context<infer T>
|
388 | ? T
|
389 | : never;
|
390 |
|
391 | export interface Context<T> {
|
392 | Consumer: Consumer<T>;
|
393 | Provider: Provider<T>;
|
394 | displayName?: string;
|
395 | }
|
396 | export interface PreactContext<T> extends Context<T> {}
|
397 |
|
398 | export function createContext<T>(defaultValue: T): Context<T>;
|