Skip to content

Instantly share code, notes, and snippets.

@uztbt
Created March 6, 2021 07:12
Show Gist options
  • Save uztbt/c711dd702d58c5f30acd9a521e8d8ad1 to your computer and use it in GitHub Desktop.
Save uztbt/c711dd702d58c5f30acd9a521e8d8ad1 to your computer and use it in GitHub Desktop.
Centering the text in nodes with a <foreignObject> tag
import { RenderCustomNodeElementFn } from "react-d3-tree/lib/types/common";
export const LogiNode: RenderCustomNodeElementFn = ({ nodeDatum, toggleNode}) => {
const [x, y] = [0, -50];
const [width, height] = [100, 100];
return(
<g>
<rect x={x} y={y} width={width} height={height} onClick={toggleNode}>
</rect>
<foreignObject x={x} y={y} width={width} height={height}>
<div className="html-in-node">
<div className="text">
{nodeDatum.name}
</div>
</div>
</foreignObject>
</g>
)
}
.tree-container {
width: 100%;
height: 100%;
}
.html-in-node {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.html-in-node > .text {
width: 95%;
height: 95%;
display: flex;
justify-content: center;
align-items: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment