@sohanemon/utils 4.1.5 → 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/dist/types/utilities.d.ts +6 -0
- package/package.json +21 -7
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
|
};
|
|
@@ -7,6 +7,9 @@ export type SelectivePartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T
|
|
|
7
7
|
export type DeepRequired<T> = T extends Function ? T : T extends Array<infer U> ? Array<DeepRequired<U>> : T extends object ? {
|
|
8
8
|
[K in keyof T]-?: DeepRequired<T[K]>;
|
|
9
9
|
} : T;
|
|
10
|
+
export type Never<T> = {
|
|
11
|
+
[K in keyof T]: never;
|
|
12
|
+
};
|
|
10
13
|
export type DeepReadonly<T> = T extends Function ? T : T extends Array<infer U> ? ReadonlyArray<DeepReadonly<U>> : T extends object ? {
|
|
11
14
|
readonly [K in keyof T]: DeepReadonly<T[K]>;
|
|
12
15
|
} : T;
|
|
@@ -24,3 +27,6 @@ export type Diff<T, U> = Omit<T, keyof U> & Omit<U, keyof T>;
|
|
|
24
27
|
export type Intersection<T extends object, U extends object> = Pick<T, Extract<keyof T, keyof U> & Extract<keyof U, keyof T>>;
|
|
25
28
|
export type Merge<T extends object, U extends object, I = Diff<T, U> & Intersection<U, T> & Diff<U, T>> = Pick<I, keyof I>;
|
|
26
29
|
export type Substract<T extends object, U extends object> = Omit<T, keyof U>;
|
|
30
|
+
export type AllOrNone<T> = T | {
|
|
31
|
+
[P in keyof T]?: never;
|
|
32
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sohanemon/utils",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.7",
|
|
4
4
|
"author": "Sohan Emon <sohanemon@outlook.com>",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
@@ -15,19 +15,33 @@
|
|
|
15
15
|
},
|
|
16
16
|
"typesVersions": {
|
|
17
17
|
"*": {
|
|
18
|
-
"core": [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
18
|
+
"core": [
|
|
19
|
+
"dist/index.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"types": [
|
|
22
|
+
"dist/types/index.d.ts"
|
|
23
|
+
],
|
|
24
|
+
"hooks": [
|
|
25
|
+
"dist/hooks/index.d.ts"
|
|
26
|
+
],
|
|
27
|
+
"components": [
|
|
28
|
+
"dist/components/index.d.ts"
|
|
29
|
+
]
|
|
22
30
|
}
|
|
23
31
|
},
|
|
24
|
-
"files": [
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
25
36
|
"scripts": {
|
|
26
37
|
"build": "tsc",
|
|
27
38
|
"build:watch": "tsc --watch",
|
|
28
39
|
"export": "tsc && npm publish"
|
|
29
40
|
},
|
|
30
|
-
"keywords": [
|
|
41
|
+
"keywords": [
|
|
42
|
+
"utils",
|
|
43
|
+
"cn"
|
|
44
|
+
],
|
|
31
45
|
"license": "ISC",
|
|
32
46
|
"devDependencies": {
|
|
33
47
|
"@types/node": "^22.4.0",
|