|
| 1 | +// Implementation reference from: https://github.com/react-component/util/blob/master/src/React/render.ts |
| 2 | +// @ts-ignore |
| 3 | +import type * as React from 'react'; |
| 4 | +import * as ReactDOM from 'react-dom'; |
| 5 | +import { createRoot as createRootClient } from 'react-dom/client'; |
| 6 | +import type { Root } from 'react-dom/client'; |
| 7 | + |
| 8 | +// Let compiler not to search module usage |
| 9 | +const fullClone = { |
| 10 | + ...ReactDOM, |
| 11 | +} as typeof ReactDOM & { |
| 12 | + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: { |
| 13 | + usingClientEntryPoint?: boolean; |
| 14 | + }; |
| 15 | + createRoot?: CreateRoot; |
| 16 | +}; |
| 17 | + |
| 18 | +type CreateRoot = (container: ContainerType) => Root; |
| 19 | + |
| 20 | +// @ts-ignore |
| 21 | +const { version, render: reactRender, unmountComponentAtNode } = fullClone; |
| 22 | + |
| 23 | +let createRoot: CreateRoot; |
| 24 | +try { |
| 25 | + const mainVersion = Number((version || '').split('.')[0]); |
| 26 | + if (mainVersion >= 18 && mainVersion < 19) { |
| 27 | + ({ createRoot } = fullClone); |
| 28 | + } else if (mainVersion >= 19) { |
| 29 | + createRoot = createRootClient; |
| 30 | + } |
| 31 | +} catch (e) { |
| 32 | + // Do nothing; |
| 33 | +} |
| 34 | + |
| 35 | +function toggleWarning(skip: boolean) { |
| 36 | + const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = fullClone; |
| 37 | + |
| 38 | + if ( |
| 39 | + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED && |
| 40 | + typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object' |
| 41 | + ) { |
| 42 | + __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = skip; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const MARK = '__td_mobile_react_root__'; |
| 47 | + |
| 48 | +// ========================== Render ========================== |
| 49 | +type ContainerType = (Element | DocumentFragment) & { |
| 50 | + [MARK]?: Root; |
| 51 | +}; |
| 52 | + |
| 53 | +function modernRender(node: React.ReactElement, container: ContainerType) { |
| 54 | + toggleWarning(true); |
| 55 | + const root = container[MARK] || createRoot(container); |
| 56 | + toggleWarning(false); |
| 57 | + |
| 58 | + root.render(node); |
| 59 | + |
| 60 | + // eslint-disable-next-line |
| 61 | + container[MARK] = root; |
| 62 | +} |
| 63 | + |
| 64 | +function legacyRender(node: React.ReactElement, container: ContainerType) { |
| 65 | + reactRender(node, container); |
| 66 | +} |
| 67 | + |
| 68 | +export function render(node: React.ReactElement, container: ContainerType) { |
| 69 | + if (createRoot) { |
| 70 | + modernRender(node, container); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + legacyRender(node, container); |
| 75 | +} |
| 76 | + |
| 77 | +// ========================= Unmount ========================== |
| 78 | +async function modernUnmount(container: ContainerType) { |
| 79 | + // Delay to unmount to avoid React 18 sync warning |
| 80 | + return Promise.resolve().then(() => { |
| 81 | + container[MARK]?.unmount(); |
| 82 | + |
| 83 | + // eslint-disable-next-line |
| 84 | + delete container[MARK]; |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +function legacyUnmount(container: ContainerType) { |
| 89 | + unmountComponentAtNode(container); |
| 90 | +} |
| 91 | + |
| 92 | +export async function unmount(container: ContainerType) { |
| 93 | + if (createRoot !== undefined) { |
| 94 | + // Delay to unmount to avoid React 18 sync warning |
| 95 | + return modernUnmount(container); |
| 96 | + } |
| 97 | + |
| 98 | + legacyUnmount(container); |
| 99 | +} |
0 commit comments