CustomNode.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React, { memo } from "react";
  2. import { Label, Node, Port, NodeChildProps, NodeProps } from "reaflow";
  3. import styled from "styled-components";
  4. const StyledNode = styled(Node)`
  5. stroke: black;
  6. fill: ${({ theme }) => theme.BLACK_PRIMARY};
  7. stroke-width: 1;
  8. `;
  9. const StyledTextWrapper = styled.div`
  10. position: absolute;
  11. display: flex;
  12. justify-content: center;
  13. align-items: center;
  14. font-size: 12px;
  15. width: 100%;
  16. height: 100%;
  17. `;
  18. const StyledText = styled.pre<{ width: number; height: number }>`
  19. width: ${({ width }) => width};
  20. height: ${({ height }) => height};
  21. color: ${({ theme }) => theme.SILVER};
  22. `;
  23. const StyledForeignObject = styled.foreignObject<{
  24. width: number;
  25. height: number;
  26. }>`
  27. position: "relative" !important;
  28. pointer-events: "none" !important;
  29. width: ${({ width }) => width + "px"};
  30. height: ${({ height }) => height + "px"};
  31. `;
  32. const baseLabelStyle = {
  33. fill: "transparent",
  34. stroke: "transparent",
  35. strokeWidth: 0,
  36. };
  37. const basePortStyle = {
  38. fill: "black",
  39. };
  40. const CustomNode = ({ nodeProps }) => {
  41. const { properties: data } = nodeProps;
  42. return (
  43. <StyledNode
  44. label={<Label style={baseLabelStyle} />}
  45. port={<Port style={basePortStyle} rx={10} ry={10} />}
  46. {...nodeProps}
  47. >
  48. {(nodeProps: NodeChildProps) => {
  49. const { width, height } = nodeProps;
  50. return (
  51. <StyledForeignObject width={width} height={height} x={0} y={0}>
  52. <StyledTextWrapper>
  53. <StyledText width={width} height={height}>
  54. {data.text}
  55. </StyledText>
  56. </StyledTextWrapper>
  57. </StyledForeignObject>
  58. );
  59. }}
  60. </StyledNode>
  61. );
  62. };
  63. export const NodeWrapper = (nodeProps: NodeProps) => {
  64. return nodeProps.properties?.data?.type ? (
  65. <CustomNode nodeProps={nodeProps} />
  66. ) : (
  67. <Node {...nodeProps} />
  68. );
  69. };