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;
|
57 | jsx?: boolean;
|
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>;
|
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 | | Component<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<any>;
|
198 | export function createElement<
|
199 | P extends JSXInternal.HTMLAttributes<T>,
|
200 | T extends HTMLElement
|
201 | >(
|
202 | type: keyof JSXInternal.IntrinsicElements,
|
203 | props: (ClassAttributes<T> & P) | null,
|
204 | ...children: ComponentChildren[]
|
205 | ): VNode<any>;
|
206 | export function createElement<
|
207 | P extends JSXInternal.SVGAttributes<T>,
|
208 | T extends HTMLElement
|
209 | >(
|
210 | type: keyof JSXInternal.IntrinsicElements,
|
211 | props: (ClassAttributes<T> & P) | null,
|
212 | ...children: ComponentChildren[]
|
213 | ): VNode<any>;
|
214 | export function createElement<T extends HTMLElement>(
|
215 | type: string,
|
216 | props:
|
217 | | (ClassAttributes<T> &
|
218 | JSXInternal.HTMLAttributes &
|
219 | JSXInternal.SVGAttributes)
|
220 | | null,
|
221 | ...children: ComponentChildren[]
|
222 | ): VNode<any>;
|
223 | export function createElement<P>(
|
224 | type: ComponentType<P>,
|
225 | props: (Attributes & P) | null,
|
226 | ...children: ComponentChildren[]
|
227 | ): VNode<any>;
|
228 | export namespace createElement {
|
229 | export import JSX = JSXInternal;
|
230 | }
|
231 |
|
232 | export function h(
|
233 | type: 'input',
|
234 | props:
|
235 | | (JSXInternal.DOMAttributes<HTMLInputElement> &
|
236 | ClassAttributes<HTMLInputElement>)
|
237 | | null,
|
238 | ...children: ComponentChildren[]
|
239 | ): VNode<any>;
|
240 | export function h<
|
241 | P extends JSXInternal.HTMLAttributes<T>,
|
242 | T extends HTMLElement
|
243 | >(
|
244 | type: keyof JSXInternal.IntrinsicElements,
|
245 | props: (ClassAttributes<T> & P) | null,
|
246 | ...children: ComponentChildren[]
|
247 | ): VNode<any>;
|
248 | export function h<
|
249 | P extends JSXInternal.SVGAttributes<T>,
|
250 | T extends HTMLElement
|
251 | >(
|
252 | type: keyof JSXInternal.IntrinsicElements,
|
253 | props: (ClassAttributes<T> & P) | null,
|
254 | ...children: ComponentChildren[]
|
255 | ): VNode<any>;
|
256 | export function h<T extends HTMLElement>(
|
257 | type: string,
|
258 | props:
|
259 | | (ClassAttributes<T> &
|
260 | JSXInternal.HTMLAttributes &
|
261 | JSXInternal.SVGAttributes)
|
262 | | null,
|
263 | ...children: ComponentChildren[]
|
264 | ): VNode<any>;
|
265 | export function h<P>(
|
266 | type: ComponentType<P>,
|
267 | props: (Attributes & P) | null,
|
268 | ...children: ComponentChildren[]
|
269 | ): VNode<any>;
|
270 | export namespace h {
|
271 | export import JSX = JSXInternal;
|
272 | }
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | export function render(
|
279 | vnode: ComponentChild,
|
280 | parent: Element | Document | ShadowRoot | DocumentFragment
|
281 | ): void;
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | export function render(
|
288 | vnode: ComponentChild,
|
289 | parent: Element | Document | ShadowRoot | DocumentFragment,
|
290 | replaceNode?: Element | Text
|
291 | ): void;
|
292 | export function hydrate(
|
293 | vnode: ComponentChild,
|
294 | parent: Element | Document | ShadowRoot | DocumentFragment
|
295 | ): void;
|
296 | export function cloneElement(
|
297 | vnode: VNode<any>,
|
298 | props?: any,
|
299 | ...children: ComponentChildren[]
|
300 | ): VNode<any>;
|
301 | export function cloneElement<P>(
|
302 | vnode: VNode<P>,
|
303 | props?: any,
|
304 | ...children: ComponentChildren[]
|
305 | ): VNode<P>;
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | export const Fragment: FunctionComponent<{}>;
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | export interface Options {
|
322 |
|
323 | vnode?(vnode: VNode): void;
|
324 |
|
325 | unmount?(vnode: VNode): void;
|
326 |
|
327 | diffed?(vnode: VNode): void;
|
328 | event?(e: Event): any;
|
329 | requestAnimationFrame?(callback: () => void): void;
|
330 | debounceRendering?(cb: () => void): void;
|
331 | useDebugValue?(value: string | number): void;
|
332 | _addHookName?(name: string | number): void;
|
333 | __suspenseDidResolve?(vnode: VNode, cb: () => void): void;
|
334 | // __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
|
335 | }
|
336 |
|
337 | export const options: Options;
|
338 |
|
339 | //
|
340 | // Preact helpers
|
341 | // -----------------------------------
|
342 | export function createRef<T = any>(): RefObject<T>;
|
343 | export function toChildArray(
|
344 | children: ComponentChildren
|
345 | ): Array<VNode | string | number>;
|
346 | export function isValidElement(vnode: any): vnode is VNode;
|
347 |
|
348 | //
|
349 | // Context
|
350 | // -----------------------------------
|
351 | export interface Consumer<T>
|
352 | extends FunctionComponent<{
|
353 | children: (value: T) => ComponentChildren;
|
354 | }> {}
|
355 | export interface PreactConsumer<T> extends Consumer<T> {}
|
356 |
|
357 | export interface Provider<T>
|
358 | extends FunctionComponent<{
|
359 | value: T;
|
360 | children: ComponentChildren;
|
361 | }> {}
|
362 | export interface PreactProvider<T> extends Provider<T> {}
|
363 | export type ContextType<C extends Context<any>> = C extends Context<infer T>
|
364 | ? T
|
365 | : never;
|
366 |
|
367 | export interface Context<T> {
|
368 | Consumer: Consumer<T>;
|
369 | Provider: Provider<T>;
|
370 | displayName?: string;
|
371 | }
|
372 | export interface PreactContext<T> extends Context<T> {}
|
373 |
|
374 | export function createContext<T>(defaultValue: T): Context<T>;
|