UNPKG

3.95 kBJavaScriptView Raw
1import {
2 createElement,
3 render as preactRender,
4 cloneElement as preactCloneElement,
5 createRef,
6 Component,
7 createContext,
8 Fragment
9} from 'preact';
10import {
11 useState,
12 useReducer,
13 useEffect,
14 useLayoutEffect,
15 useRef,
16 useImperativeHandle,
17 useMemo,
18 useCallback,
19 useContext,
20 useDebugValue
21} from 'preact/hooks';
22import { PureComponent } from './PureComponent';
23import { memo } from './memo';
24import { forwardRef } from './forwardRef';
25import { Children } from './Children';
26import { Suspense, lazy } from './suspense';
27import { SuspenseList } from './suspense-list';
28import { createPortal } from './portals';
29import {
30 hydrate,
31 render,
32 REACT_ELEMENT_TYPE,
33 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
34} from './render';
35
36const version = '16.8.0'; // trick libraries to think we are react
37
38/**
39 * Legacy version of createElement.
40 * @param {import('./internal').VNode["type"]} type The node name or Component constructor
41 */
42function createFactory(type) {
43 return createElement.bind(null, type);
44}
45
46/**
47 * Check if the passed element is a valid (p)react node.
48 * @param {*} element The element to check
49 * @returns {boolean}
50 */
51function isValidElement(element) {
52 return !!element && element.$$typeof === REACT_ELEMENT_TYPE;
53}
54
55/**
56 * Wrap `cloneElement` to abort if the passed element is not a valid element and apply
57 * all vnode normalizations.
58 * @param {import('./internal').VNode} element The vnode to clone
59 * @param {object} props Props to add when cloning
60 * @param {Array<import('./internal').ComponentChildren>} rest Optional component children
61 */
62function cloneElement(element) {
63 if (!isValidElement(element)) return element;
64 return preactCloneElement.apply(null, arguments);
65}
66
67/**
68 * Remove a component tree from the DOM, including state and event handlers.
69 * @param {import('./internal').PreactElement} container
70 * @returns {boolean}
71 */
72function unmountComponentAtNode(container) {
73 if (container._children) {
74 preactRender(null, container);
75 return true;
76 }
77 return false;
78}
79
80/**
81 * Get the matching DOM node for a component
82 * @param {import('./internal').Component} component
83 * @returns {import('./internal').PreactElement | null}
84 */
85function findDOMNode(component) {
86 return (
87 (component &&
88 (component.base || (component.nodeType === 1 && component))) ||
89 null
90 );
91}
92
93/**
94 * Deprecated way to control batched rendering inside the reconciler, but we
95 * already schedule in batches inside our rendering code
96 * @template Arg
97 * @param {(arg: Arg) => void} callback function that triggers the updated
98 * @param {Arg} [arg] Optional argument that can be passed to the callback
99 */
100// eslint-disable-next-line camelcase
101const unstable_batchedUpdates = (callback, arg) => callback(arg);
102
103/**
104 * Strict Mode is not implemented in Preact, so we provide a stand-in for it
105 * that just renders its children without imposing any restrictions.
106 */
107const StrictMode = Fragment;
108
109export * from 'preact/hooks';
110export {
111 version,
112 Children,
113 render,
114 hydrate,
115 unmountComponentAtNode,
116 createPortal,
117 createElement,
118 createContext,
119 createFactory,
120 cloneElement,
121 createRef,
122 Fragment,
123 isValidElement,
124 findDOMNode,
125 Component,
126 PureComponent,
127 memo,
128 forwardRef,
129 // eslint-disable-next-line camelcase
130 unstable_batchedUpdates,
131 StrictMode,
132 Suspense,
133 SuspenseList,
134 lazy,
135 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
136};
137
138// React copies the named exports to the default one.
139export default {
140 useState,
141 useReducer,
142 useEffect,
143 useLayoutEffect,
144 useRef,
145 useImperativeHandle,
146 useMemo,
147 useCallback,
148 useContext,
149 useDebugValue,
150 version,
151 Children,
152 render,
153 hydrate,
154 unmountComponentAtNode,
155 createPortal,
156 createElement,
157 createContext,
158 createFactory,
159 cloneElement,
160 createRef,
161 Fragment,
162 isValidElement,
163 findDOMNode,
164 Component,
165 PureComponent,
166 memo,
167 forwardRef,
168 unstable_batchedUpdates,
169 StrictMode,
170 Suspense,
171 SuspenseList,
172 lazy,
173 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
174};