Skip to content

Commit 203d3eb

Browse files
committed
fix initial size, updating initial measures and fix clas enahncement
1 parent 358ea04 commit 203d3eb

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

src/extensions/react-flow/nodes/NodeContent.tsx

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,14 @@ export function NodeContent<CONTENT_PROPS = any>({
389389
handleStack[Position.Left] =
390390
flowVersionCheck === "legacy" ? ([] as NodeContentHandleLegacyProps[]) : ([] as NodeContentHandleNextProps[]);
391391

392+
const saveOriginalSize = () => {
393+
originalSize.current.width = nodeContentRef.current.offsetWidth as number;
394+
originalSize.current.height = nodeContentRef.current.offsetHeight as number;
395+
}
396+
392397
React.useEffect(() => {
393398
if(nodeContentRef.current && !(originalSize.current.width || originalSize.current.height)) {
394-
originalSize.current.width = nodeContentRef.current.offsetWidth as number;
395-
originalSize.current.height = nodeContentRef.current.offsetHeight as number;
399+
saveOriginalSize();
396400
}
397401
}, [!!nodeContentRef.current, (!originalSize.current.width || !originalSize.current.height)])
398402

@@ -402,14 +406,38 @@ export function NodeContent<CONTENT_PROPS = any>({
402406
const updateHeight = nodeDimensions?.height ? validateHeight(nodeDimensions?.height) : undefined;
403407
setWidth(updateWidth);
404408
setHeight(updateHeight);
409+
if (!nodeDimensions?.width && !nodeDimensions?.height) {
410+
// provoke new measuring if no dimensions are set
411+
saveOriginalSize();
412+
}
405413
}, [nodeDimensions]);
406414

407-
// resizing check and conditional enhancements
408-
React.useEffect(() => {
415+
const isResizingActive = () : boolean => {
409416
const currentClassNames = nodeContentRef.current.classList;
410417
const resizingActive =
411418
resizeDirections.right === currentClassNames.contains("is-resizable-horizontal") &&
412419
resizeDirections.bottom === currentClassNames.contains("is-resizable-vertical");
420+
return resizingActive;
421+
}
422+
423+
// force default size when resizing is activated but no dimensions are set
424+
React.useEffect(() => {
425+
const resizingActive = isResizingActive();
426+
427+
if (isResizable && !resizingActive) {
428+
if (!width || !height) {
429+
const newWidth = validateWidth(width ?? originalSize.current?.width as number);
430+
const newHeight = validateHeight(height ?? originalSize.current?.height as number);
431+
setWidth(newWidth);
432+
setHeight(newHeight);
433+
}
434+
}
435+
}, [nodeContentRef.current, onNodeResize, minimalShape, resizeDirections?.bottom, resizeDirections?.right]); // need to be done everytime a property is changed and the element is re-rendered, otherwise the resizing class is lost
436+
437+
// conditional enhancements for activated resizing
438+
React.useEffect(() => {
439+
const currentClassNames = nodeContentRef.current.classList;
440+
const resizingActive = isResizingActive();
413441

414442
if (isResizable && !resizingActive) {
415443
if (currentClassNames.contains("is-resizable-horizontal")) {
@@ -426,7 +454,7 @@ export function NodeContent<CONTENT_PROPS = any>({
426454
nodeContentRef.current.classList.add("is-resizable-vertical");
427455
}
428456
}
429-
}, [nodeContentRef.current, onNodeResize, minimalShape, resizeDirections?.bottom, resizeDirections?.right]); // need to be done everytime a property is changed and the element is re-rendered, otherwise the resizing class is lost
457+
}); // need to be done everytime a property is changed and the element is re-rendered, otherwise the resizing class is lost
430458

431459
// remove introduction class
432460
React.useEffect(() => {

0 commit comments

Comments
 (0)