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>;
|
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 type RenderableProps<P, RefType = any> = P &
|
72 | Readonly<Attributes & { children?: ComponentChildren; ref?: Ref<RefType> }>;
|
73 |
|
74 | export type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>;
|
75 | export type ComponentFactory<P = {}> = ComponentType<P>;
|
76 |
|
77 | export type ComponentProps<
|
78 | C extends ComponentType<any> | keyof JSXInternal.IntrinsicElements
|
79 | > = C extends ComponentType<infer P>
|
80 | ? P
|
81 | : C extends keyof JSXInternal.IntrinsicElements
|
82 | ? JSXInternal.IntrinsicElements[C]
|
83 | : never;
|
84 |
|
85 | export interface FunctionComponent<P = {}> {
|
86 | (props: RenderableProps<P>, context?: any): VNode<any> | null;
|
87 | displayName?: string;
|
88 | defaultProps?: Partial<P>;
|
89 | }
|
90 | export interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}
|
91 |
|
92 | export interface ComponentClass<P = {}, S = {}> {
|
93 | new (props: P, context?: any): Component<P, S>;
|
94 | displayName?: string;
|
95 | defaultProps?: Partial<P>;
|
96 | contextType?: Context<any>;
|
97 | getDerivedStateFromProps?(
|
98 | props: Readonly<P>,
|
99 | state: Readonly<S>
|
100 | ): Partial<S> | null;
|
101 | getDerivedStateFromError?(error: any): Partial<S> | null;
|
102 | }
|
103 | export interface ComponentConstructor<P = {}, S = {}>
|
104 | extends ComponentClass<P, S> {}
|
105 |
|
106 |
|
107 | export type AnyComponent<P = {}, S = {}> =
|
108 | | FunctionComponent<P>
|
109 | | Component<P, S>;
|
110 |
|
111 | export interface Component<P = {}, S = {}> {
|
112 | componentWillMount?(): void;
|
113 | componentDidMount?(): void;
|
114 | componentWillUnmount?(): void;
|
115 | getChildContext?(): object;
|
116 | componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
|
117 | shouldComponentUpdate?(
|
118 | nextProps: Readonly<P>,
|
119 | nextState: Readonly<S>,
|
120 | nextContext: any
|
121 | ): boolean;
|
122 | componentWillUpdate?(
|
123 | nextProps: Readonly<P>,
|
124 | nextState: Readonly<S>,
|
125 | nextContext: any
|
126 | ): void;
|
127 | getSnapshotBeforeUpdate?(oldProps: Readonly<P>, oldState: Readonly<S>): any;
|
128 | componentDidUpdate?(
|
129 | previousProps: Readonly<P>,
|
130 | previousState: Readonly<S>,
|
131 | snapshot: any
|
132 | ): void;
|
133 | componentDidCatch?(error: any, errorInfo: any): void;
|
134 | }
|
135 |
|
136 | export abstract class Component<P, S> {
|
137 | constructor(props?: P, context?: any);
|
138 |
|
139 | static displayName?: string;
|
140 | static defaultProps?: any;
|
141 | static contextType?: Context<any>;
|
142 |
|
143 | // Static members cannot reference class type parameters. This is not
|
144 | // supported in TypeScript. Reusing the same type arguments from `Component`
|
145 | // will lead to an impossible state where one cannot satisfy the type
|
146 | // constraint under no circumstances, see #1356.In general type arguments
|
147 | // seem to be a bit buggy and not supported well at the time of this
|
148 | // writing with TS 3.3.3333.
|
149 | static getDerivedStateFromProps?(
|
150 | props: Readonly<object>,
|
151 | state: Readonly<object>
|
152 | ): object | null;
|
153 | static getDerivedStateFromError?(error: any): object | null;
|
154 |
|
155 | state: Readonly<S>;
|
156 | props: RenderableProps<P>;
|
157 | context: any;
|
158 | base?: Element | Text;
|
159 |
|
160 | // From https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e836acc75a78cf0655b5dfdbe81d69fdd4d8a252/types/react/index.d.ts#L402
|
161 | // // We MUST keep setState() as a unified signature because it allows proper checking of the method return type.
|
162 | // // See: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18365#issuecomment-351013257
|
163 | setState<K extends keyof S>(
|
164 | state:
|
165 | | ((
|
166 | prevState: Readonly<S>,
|
167 | props: Readonly<P>
|
168 | ) => Pick<S, K> | Partial<S> | null)
|
169 | | (Pick<S, K> | Partial<S> | null),
|
170 | callback?: () => void
|
171 | ): void;
|
172 |
|
173 | forceUpdate(callback?: () => void): void;
|
174 |
|
175 | abstract render(
|
176 | props?: RenderableProps<P>,
|
177 | state?: Readonly<S>,
|
178 | context?: any
|
179 | ): ComponentChild;
|
180 | }
|
181 |
|
182 | //
|
183 | // Preact createElement
|
184 | // -----------------------------------
|
185 |
|
186 | export function createElement(
|
187 | type: string,
|
188 | props:
|
189 | | (JSXInternal.HTMLAttributes &
|
190 | JSXInternal.SVGAttributes &
|
191 | Record<string, any>)
|
192 | | null,
|
193 | ...children: ComponentChildren[]
|
194 | ): VNode<any>;
|
195 | export function createElement<P>(
|
196 | type: ComponentType<P>,
|
197 | props: (Attributes & P) | null,
|
198 | ...children: ComponentChildren[]
|
199 | ): VNode<any>;
|
200 | export namespace createElement {
|
201 | export import JSX = JSXInternal;
|
202 | }
|
203 |
|
204 | export function h(
|
205 | type: string,
|
206 | props:
|
207 | | (JSXInternal.HTMLAttributes &
|
208 | JSXInternal.SVGAttributes &
|
209 | Record<string, any>)
|
210 | | null,
|
211 | ...children: ComponentChildren[]
|
212 | ): VNode<any>;
|
213 | export function h<P>(
|
214 | type: ComponentType<P>,
|
215 | props: (Attributes & P) | null,
|
216 | ...children: ComponentChildren[]
|
217 | ): VNode<any>;
|
218 | export namespace h {
|
219 | export import JSX = JSXInternal;
|
220 | }
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | export function render(
|
227 | vnode: ComponentChild,
|
228 | parent: Element | Document | ShadowRoot | DocumentFragment,
|
229 | replaceNode?: Element | Text
|
230 | ): void;
|
231 | export function hydrate(
|
232 | vnode: ComponentChild,
|
233 | parent: Element | Document | ShadowRoot | DocumentFragment
|
234 | ): void;
|
235 | export function cloneElement(
|
236 | vnode: VNode<any>,
|
237 | props?: any,
|
238 | ...children: ComponentChildren[]
|
239 | ): VNode<any>;
|
240 | export function cloneElement<P>(
|
241 | vnode: VNode<P>,
|
242 | props?: any,
|
243 | ...children: ComponentChildren[]
|
244 | ): VNode<P>;
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | export const Fragment: ComponentClass<{}, {}>;
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | export interface Options {
|
261 |
|
262 | vnode?(vnode: VNode): void;
|
263 |
|
264 | unmount?(vnode: VNode): void;
|
265 |
|
266 | diffed?(vnode: VNode): void;
|
267 | event?(e: Event): any;
|
268 | requestAnimationFrame?(callback: () => void): void;
|
269 | debounceRendering?(cb: () => void): void;
|
270 | useDebugValue?(value: string | number): void;
|
271 | _addHookName?(name: string | number): void;
|
272 | __suspenseDidResolve?(vnode: VNode, cb: () => void): void;
|
273 | // __canSuspenseResolve?(vnode: VNode, cb: () => void): void;
|
274 | }
|
275 |
|
276 | export const options: Options;
|
277 |
|
278 | //
|
279 | // Preact helpers
|
280 | // -----------------------------------
|
281 | export function createRef<T = any>(): RefObject<T>;
|
282 | export function toChildArray(
|
283 | children: ComponentChildren
|
284 | ): Array<VNode | string | number>;
|
285 | export function isValidElement(vnode: any): vnode is VNode;
|
286 |
|
287 | //
|
288 | // Context
|
289 | // -----------------------------------
|
290 | export interface Consumer<T>
|
291 | extends FunctionComponent<{
|
292 | children: (value: T) => ComponentChildren;
|
293 | }> {}
|
294 | export interface PreactConsumer<T> extends Consumer<T> {}
|
295 |
|
296 | export interface Provider<T>
|
297 | extends FunctionComponent<{
|
298 | value: T;
|
299 | children: ComponentChildren;
|
300 | }> {}
|
301 | export interface PreactProvider<T> extends Provider<T> {}
|
302 |
|
303 | export interface Context<T> {
|
304 | Consumer: Consumer<T>;
|
305 | Provider: Provider<T>;
|
306 | displayName?: string;
|
307 | }
|
308 | export interface PreactContext<T> extends Context<T> {}
|
309 |
|
310 | export function createContext<T>(defaultValue: T): Context<T>;
|