refactor: access edge-label-renderer by storing the root element

This commit is contained in:
GeoffreyLiu
2022-11-11 10:44:59 +08:00
parent 55cf7560c0
commit 3170f875e5
8 changed files with 52 additions and 17 deletions
@@ -1,19 +1,26 @@
import { useRef } from 'react';
import type { ReactNode } from 'react';
import { createPortal } from 'react-dom';
import { useStoreApi } from '../../hooks/useStore';
import { getEdgeLabelRendererId } from '../../utils/graph';
import { EDGE_LABEL_RENDERER_MAIN_CLASS } from '../../constants/component';
function EdgeLabelRenderer({ children }: { children: ReactNode }) {
const store = useStoreApi()
const state = store.getState()
const wrapperRef = useRef(document.getElementById(getEdgeLabelRendererId(state.rfId)));
const store = useStoreApi();
const {
rootElementRef: { current: rootElement },
} = store.getState();
if (!wrapperRef.current) {
if (!rootElement) {
return null;
}
return createPortal(children, wrapperRef.current);
const collection = rootElement.getElementsByClassName(EDGE_LABEL_RENDERER_MAIN_CLASS);
const edgeLabelRendererElement = collection.item(0);
if (!edgeLabelRendererElement) {
return null;
}
return createPortal(children, edgeLabelRendererElement);
}
export default EdgeLabelRenderer;