|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {ReactNodeList} from 'shared/ReactTypes'; |
| 11 | +import type { |
| 12 | + RootType, |
| 13 | + HydrateRootOptions, |
| 14 | + CreateRootOptions, |
| 15 | +} from './ReactDOMRoot'; |
| 16 | + |
| 17 | +import { |
| 18 | + createRoot as createRootImpl, |
| 19 | + hydrateRoot as hydrateRootImpl, |
| 20 | + isValidContainer, |
| 21 | +} from './ReactDOMRoot'; |
| 22 | +import {createEventHandle} from 'react-dom-bindings/src/client/ReactDOMEventHandle'; |
| 23 | + |
| 24 | +import { |
| 25 | + flushSyncFromReconciler as flushSyncWithoutWarningIfAlreadyRendering, |
| 26 | + isAlreadyRendering, |
| 27 | + injectIntoDevTools, |
| 28 | + findHostInstance, |
| 29 | +} from 'react-reconciler/src/ReactFiberReconciler'; |
| 30 | +import {runWithPriority} from 'react-reconciler/src/ReactEventPriorities'; |
| 31 | +import {createPortal as createPortalImpl} from 'react-reconciler/src/ReactPortal'; |
| 32 | +import {canUseDOM} from 'shared/ExecutionEnvironment'; |
| 33 | +import ReactVersion from 'shared/ReactVersion'; |
| 34 | + |
| 35 | +import { |
| 36 | + getClosestInstanceFromNode, |
| 37 | + getInstanceFromNode, |
| 38 | + getNodeFromInstance, |
| 39 | + getFiberCurrentPropsFromNode, |
| 40 | +} from 'react-dom-bindings/src/client/ReactDOMComponentTree'; |
| 41 | +import { |
| 42 | + enqueueStateRestore, |
| 43 | + restoreStateIfNeeded, |
| 44 | +} from 'react-dom-bindings/src/events/ReactDOMControlledComponent'; |
| 45 | +import Internals from '../ReactDOMSharedInternals'; |
| 46 | + |
| 47 | +export { |
| 48 | + prefetchDNS, |
| 49 | + preconnect, |
| 50 | + preload, |
| 51 | + preloadModule, |
| 52 | + preinit, |
| 53 | + preinitModule, |
| 54 | +} from '../shared/ReactDOMFloat'; |
| 55 | +export { |
| 56 | + useFormStatus, |
| 57 | + useFormState, |
| 58 | +} from 'react-dom-bindings/src/shared/ReactDOMFormActions'; |
| 59 | + |
| 60 | +if (__DEV__) { |
| 61 | + if ( |
| 62 | + typeof Map !== 'function' || |
| 63 | + // $FlowFixMe[prop-missing] Flow incorrectly thinks Map has no prototype |
| 64 | + Map.prototype == null || |
| 65 | + typeof Map.prototype.forEach !== 'function' || |
| 66 | + typeof Set !== 'function' || |
| 67 | + // $FlowFixMe[prop-missing] Flow incorrectly thinks Set has no prototype |
| 68 | + Set.prototype == null || |
| 69 | + typeof Set.prototype.clear !== 'function' || |
| 70 | + typeof Set.prototype.forEach !== 'function' |
| 71 | + ) { |
| 72 | + console.error( |
| 73 | + 'React depends on Map and Set built-in types. Make sure that you load a ' + |
| 74 | + 'polyfill in older browsers. https://react.dev/link/react-polyfills', |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function createPortal( |
| 80 | + children: ReactNodeList, |
| 81 | + container: Element | DocumentFragment, |
| 82 | + key: ?string = null, |
| 83 | +): React$Portal { |
| 84 | + if (!isValidContainer(container)) { |
| 85 | + throw new Error('Target container is not a DOM element.'); |
| 86 | + } |
| 87 | + |
| 88 | + // TODO: pass ReactDOM portal implementation as third argument |
| 89 | + // $FlowFixMe[incompatible-return] The Flow type is opaque but there's no way to actually create it. |
| 90 | + return createPortalImpl(children, container, null, key); |
| 91 | +} |
| 92 | + |
| 93 | +function createRoot( |
| 94 | + container: Element | Document | DocumentFragment, |
| 95 | + options?: CreateRootOptions, |
| 96 | +): RootType { |
| 97 | + if (__DEV__) { |
| 98 | + if (!Internals.usingClientEntryPoint && !__UMD__) { |
| 99 | + console.error( |
| 100 | + 'You are importing createRoot from "react-dom" which is not supported. ' + |
| 101 | + 'You should instead import it from "react-dom/client".', |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + return createRootImpl(container, options); |
| 106 | +} |
| 107 | + |
| 108 | +function hydrateRoot( |
| 109 | + container: Document | Element, |
| 110 | + initialChildren: ReactNodeList, |
| 111 | + options?: HydrateRootOptions, |
| 112 | +): RootType { |
| 113 | + if (__DEV__) { |
| 114 | + if (!Internals.usingClientEntryPoint && !__UMD__) { |
| 115 | + console.error( |
| 116 | + 'You are importing hydrateRoot from "react-dom" which is not supported. ' + |
| 117 | + 'You should instead import it from "react-dom/client".', |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + return hydrateRootImpl(container, initialChildren, options); |
| 122 | +} |
| 123 | + |
| 124 | +// Overload the definition to the two valid signatures. |
| 125 | +// Warning, this opts-out of checking the function body. |
| 126 | +declare function flushSync<R>(fn: () => R): R; |
| 127 | +// eslint-disable-next-line no-redeclare |
| 128 | +declare function flushSync(): void; |
| 129 | +// eslint-disable-next-line no-redeclare |
| 130 | +function flushSync<R>(fn: (() => R) | void): R | void { |
| 131 | + if (__DEV__) { |
| 132 | + if (isAlreadyRendering()) { |
| 133 | + console.error( |
| 134 | + 'flushSync was called from inside a lifecycle method. React cannot ' + |
| 135 | + 'flush when React is already rendering. Consider moving this call to ' + |
| 136 | + 'a scheduler task or micro task.', |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + return flushSyncWithoutWarningIfAlreadyRendering(fn); |
| 141 | +} |
| 142 | + |
| 143 | +function findDOMNode( |
| 144 | + componentOrElement: React$Component<any, any>, |
| 145 | +): null | Element | Text { |
| 146 | + return findHostInstance(componentOrElement); |
| 147 | +} |
| 148 | + |
| 149 | +// Expose findDOMNode on internals |
| 150 | +Internals.findDOMNode = findDOMNode; |
| 151 | + |
| 152 | +function unstable_batchedUpdates<A, R>(fn: (a: A) => R, a: A): R { |
| 153 | + // batchedUpdates was a legacy mode feature that is a no-op outside of |
| 154 | + // legacy mode. In 19, we made it an actual no-op, but we're keeping it |
| 155 | + // for now since there may be libraries that still include it. |
| 156 | + return fn(a); |
| 157 | +} |
| 158 | + |
| 159 | +export { |
| 160 | + createPortal, |
| 161 | + unstable_batchedUpdates, |
| 162 | + flushSync, |
| 163 | + ReactVersion as version, |
| 164 | + // exposeConcurrentModeAPIs |
| 165 | + createRoot, |
| 166 | + hydrateRoot, |
| 167 | + // enableCreateEventHandleAPI |
| 168 | + createEventHandle as unstable_createEventHandle, |
| 169 | + // TODO: Remove this once callers migrate to alternatives. |
| 170 | + // This should only be used by React internals. |
| 171 | + runWithPriority as unstable_runWithPriority, |
| 172 | +}; |
| 173 | + |
| 174 | +// Keep in sync with ReactTestUtils.js. |
| 175 | +// This is an array for better minification. |
| 176 | +Internals.Events = [ |
| 177 | + getInstanceFromNode, |
| 178 | + getNodeFromInstance, |
| 179 | + getFiberCurrentPropsFromNode, |
| 180 | + enqueueStateRestore, |
| 181 | + restoreStateIfNeeded, |
| 182 | + unstable_batchedUpdates, |
| 183 | +]; |
| 184 | + |
| 185 | +const foundDevTools = injectIntoDevTools({ |
| 186 | + findFiberByHostInstance: getClosestInstanceFromNode, |
| 187 | + bundleType: __DEV__ ? 1 : 0, |
| 188 | + version: ReactVersion, |
| 189 | + rendererPackageName: 'react-dom', |
| 190 | +}); |
| 191 | + |
| 192 | +if (__DEV__) { |
| 193 | + if (!foundDevTools && canUseDOM && window.top === window.self) { |
| 194 | + // If we're in Chrome or Firefox, provide a download link if not installed. |
| 195 | + if ( |
| 196 | + (navigator.userAgent.indexOf('Chrome') > -1 && |
| 197 | + navigator.userAgent.indexOf('Edge') === -1) || |
| 198 | + navigator.userAgent.indexOf('Firefox') > -1 |
| 199 | + ) { |
| 200 | + const protocol = window.location.protocol; |
| 201 | + // Don't warn in exotic cases like chrome-extension://. |
| 202 | + if (/^(https?|file):$/.test(protocol)) { |
| 203 | + // eslint-disable-next-line react-internal/no-production-logging |
| 204 | + console.info( |
| 205 | + '%cDownload the React DevTools ' + |
| 206 | + 'for a better development experience: ' + |
| 207 | + 'https://react.dev/link/react-devtools' + |
| 208 | + (protocol === 'file:' |
| 209 | + ? '\nYou might need to use a local HTTP server (instead of file://): ' + |
| 210 | + 'https://react.dev/link/react-devtools-faq' |
| 211 | + : ''), |
| 212 | + 'font-weight:bold', |
| 213 | + ); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments