@sohanemon/utils 4.1.6 → 4.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/index.js +67 -15
- package/package.json +1 -1
package/dist/hooks/index.js
CHANGED
|
@@ -303,7 +303,7 @@ export const useDomCalculation = ({ blockIds = [], margin = 0, substract = true,
|
|
|
303
303
|
height: 500,
|
|
304
304
|
width: 500,
|
|
305
305
|
});
|
|
306
|
-
const handleCalculation = () => {
|
|
306
|
+
const handleCalculation = React.useCallback(() => {
|
|
307
307
|
const blocksHeight = blockIds.reduce((prevHeight, id) => prevHeight + (document.getElementById(id)?.clientHeight || 0), 0);
|
|
308
308
|
const blocksWidth = blockIds.reduce((prevWidth, id) => prevWidth + (document.getElementById(id)?.clientWidth || 0), 0);
|
|
309
309
|
const height = substract
|
|
@@ -312,28 +312,80 @@ export const useDomCalculation = ({ blockIds = [], margin = 0, substract = true,
|
|
|
312
312
|
const width = substract
|
|
313
313
|
? window.innerWidth - blocksWidth - margin
|
|
314
314
|
: blocksWidth + margin;
|
|
315
|
-
|
|
316
|
-
|
|
315
|
+
setDimensions((prev) => {
|
|
316
|
+
// Only update state if dimensions have actually changed
|
|
317
|
+
if (prev.height === height && prev.width === width) {
|
|
318
|
+
return prev;
|
|
319
|
+
}
|
|
320
|
+
return { height, width };
|
|
321
|
+
});
|
|
317
322
|
onChange?.({
|
|
318
323
|
blocksWidth,
|
|
319
324
|
blocksHeight,
|
|
320
325
|
remainingWidth: width,
|
|
321
326
|
remainingHeight: height,
|
|
322
327
|
});
|
|
323
|
-
};
|
|
328
|
+
}, [blockIds, margin, substract, onChange]);
|
|
324
329
|
useIsomorphicEffect(() => {
|
|
325
330
|
handleCalculation();
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
331
|
+
const cleanups = [];
|
|
332
|
+
if (blockIds.length > 0) {
|
|
333
|
+
const observer = new MutationObserver((mutations) => {
|
|
334
|
+
let shouldRecalculate = false;
|
|
335
|
+
for (const mutation of mutations) {
|
|
336
|
+
for (const node of mutation.addedNodes) {
|
|
337
|
+
if (node instanceof Element) {
|
|
338
|
+
if (blockIds.includes(node.id) ||
|
|
339
|
+
blockIds.some((id) => node.querySelector(`#${CSS.escape(id)}`))) {
|
|
340
|
+
shouldRecalculate = true;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (shouldRecalculate)
|
|
346
|
+
break;
|
|
347
|
+
for (const node of mutation.removedNodes) {
|
|
348
|
+
if (node instanceof Element) {
|
|
349
|
+
if (blockIds.includes(node.id) ||
|
|
350
|
+
blockIds.some((id) => node.querySelector(`#${CSS.escape(id)}`))) {
|
|
351
|
+
shouldRecalculate = true;
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (shouldRecalculate)
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
if (shouldRecalculate) {
|
|
360
|
+
handleCalculation();
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
observer.observe(document.body, {
|
|
364
|
+
childList: true,
|
|
365
|
+
subtree: true,
|
|
366
|
+
});
|
|
367
|
+
cleanups.push(() => observer.disconnect());
|
|
334
368
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
369
|
+
if (dynamic) {
|
|
370
|
+
if (typeof dynamic === 'string') {
|
|
371
|
+
const resizableElement = document.getElementById(dynamic);
|
|
372
|
+
if (resizableElement) {
|
|
373
|
+
const resizeObserver = new ResizeObserver(handleCalculation);
|
|
374
|
+
resizeObserver.observe(resizableElement);
|
|
375
|
+
cleanups.push(() => resizeObserver.unobserve(resizableElement));
|
|
376
|
+
cleanups.push(() => resizeObserver.disconnect());
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
window.addEventListener('resize', handleCalculation);
|
|
381
|
+
cleanups.push(() => window.removeEventListener('resize', handleCalculation));
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return () => {
|
|
385
|
+
for (const cleanup of cleanups) {
|
|
386
|
+
cleanup();
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}, [handleCalculation, dynamic, blockIds.join(',')]);
|
|
338
390
|
return dimensions;
|
|
339
391
|
};
|