-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extension-react adapt react 16/17 (#6339)
* feat(react): adapt react 16/17 * test: update path alias, remove inner graph * chore: config test env --------- Co-authored-by: antv <[email protected]>
- Loading branch information
Showing
17 changed files
with
166 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { ReactNode } from './node'; | ||
export { render, unmount } from './render'; | ||
|
||
export type { ReactNodeStyleProps } from './node'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/g6-extension-react/src/elements/react/render.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import type * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import type { Root } from 'react-dom/client'; | ||
|
||
// Let compiler not to search module usage | ||
const fullClone = { | ||
...ReactDOM, | ||
} as typeof ReactDOM & { | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?: { | ||
usingClientEntryPoint?: boolean; | ||
}; | ||
createRoot?: CreateRoot; | ||
}; | ||
|
||
type CreateRoot = (container: ContainerType) => Root; | ||
|
||
const { version, render: reactRender, unmountComponentAtNode } = fullClone; | ||
|
||
let createRoot: CreateRoot | undefined; | ||
try { | ||
const mainVersion = Number((version || '').split('.')[0]); | ||
if (mainVersion >= 18 && fullClone.createRoot) { | ||
({ createRoot } = fullClone); | ||
} | ||
} catch (e) { | ||
// Do nothing; | ||
} | ||
|
||
/** | ||
* <zh/> 切换警告 | ||
* | ||
* <en/> Toggle warning | ||
* @param skip <zh/> 是否跳过警告 | <en/> Whether to skip the warning | ||
*/ | ||
function toggleWarning(skip: boolean) { | ||
const { __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } = fullClone; | ||
|
||
if ( | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED && | ||
typeof __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED === 'object' | ||
) { | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = skip; | ||
} | ||
} | ||
|
||
const MARK = '__rc_react_root__'; | ||
|
||
// ========================== Render ========================== | ||
type ContainerType = (Element | DocumentFragment) & { | ||
[MARK]?: Root; | ||
}; | ||
|
||
/** | ||
* <zh/> 渲染 React 节点(React >= 18) | ||
* | ||
* <en/> Render React node(React >= 18) | ||
* @param node - <zh/> React 节点 | <en/> React node | ||
* @param container - <zh/> 容器 | <en/> Container | ||
*/ | ||
function modernRender(node: React.ReactNode, container: ContainerType) { | ||
toggleWarning(true); | ||
const root = container[MARK] || createRoot!(container); | ||
toggleWarning(false); | ||
|
||
root.render(node); | ||
|
||
container[MARK] = root; | ||
} | ||
|
||
/** | ||
* <zh/> 使用旧的 React 渲染 | ||
* | ||
* <en/> Use old React render | ||
* @param node - <zh/> React 节点 | <en/> React node | ||
* @param container - <zh/> 容器 | <en/> Container | ||
*/ | ||
function legacyRender(node: React.ReactElement, container: ContainerType) { | ||
reactRender(node, container); | ||
} | ||
|
||
/** | ||
* <zh/> 渲染 React 节点(兼容 React 16 ~ 18) | ||
* | ||
* <en/> Render React node(Compatible with React 16 ~ 18) | ||
* @param node - <zh/> React 节点 | <en/> React node | ||
* @param container - <zh/> 容器 | <en/> Container | ||
*/ | ||
export function render(node: React.ReactElement, container: ContainerType) { | ||
if (createRoot) modernRender(node, container); | ||
else legacyRender(node, container); | ||
} | ||
|
||
/** | ||
* <zh/> 卸载 React 节点(React >= 18) | ||
* | ||
* <en/> Unmount React node(React >= 18) | ||
* @param container - <zh/> 容器 | <en/> Container | ||
* @returns <zh/> Promise | <en/> Promise | ||
*/ | ||
async function modernUnmount(container: ContainerType) { | ||
// Delay to unmount to avoid React 18 sync warning | ||
return Promise.resolve().then(() => { | ||
container[MARK]?.unmount(); | ||
delete container[MARK]; | ||
}); | ||
} | ||
|
||
/** | ||
* <zh/> 卸载 React 节点(React < 18) | ||
* | ||
* <en/> Unmount React node(React < 18) | ||
* @param container - <zh/> 容器 | <en/> Container | ||
*/ | ||
function legacyUnmount(container: ContainerType) { | ||
unmountComponentAtNode(container); | ||
} | ||
|
||
/** | ||
* <zh/> 卸载 React 节点(兼容 React 16 ~ 18) | ||
* | ||
* <en/> Unmount React node(Compatible with React 16 ~ 18) | ||
* @param container - <zh/> 容器 | <en/> Container | ||
* @returns <zh/> Promise | <en/> Promise | ||
*/ | ||
export async function unmount(container: ContainerType) { | ||
if (createRoot) { | ||
// Delay to unmount to avoid React 18 sync warning | ||
return modernUnmount(container); | ||
} | ||
|
||
legacyUnmount(container); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": {} | ||
"paths": { | ||
"@antv/g6": ["../g6/src/index.ts"], | ||
"@antv/g6-extension-react": ["./src/index.ts"] | ||
}, | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src/**/*", "__tests__/**/*"], | ||
"include": ["src/**/*", "__tests__/**/*", "../g6/src/layouts/hierarchy.d.ts"], | ||
"extends": "./tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters