@sohanemon/utils 4.1.1 → 4.1.2

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.
@@ -94,15 +94,40 @@ export declare function useCopyToClipboard({ timeout }: {
94
94
  /**
95
95
  * Properties for height calculation.
96
96
  */
97
- type CalculationProps = {
97
+ type CalculationProps2 = {
98
98
  blockIds: string[];
99
99
  dynamic?: boolean | string;
100
100
  margin?: number;
101
101
  substract?: boolean;
102
102
  };
103
103
  /**
104
+ *
104
105
  * Hook to calculate the height of an element based on viewport and other block heights.
105
106
  * @param params - Configuration object for height calculation.
106
107
  * @returns The calculated height.
107
108
  */
108
- export declare const useHeightCalculation: ({ blockIds, margin, substract, dynamic, }: CalculationProps) => number;
109
+ export declare const useHeightCalculation: ({ blockIds, margin, substract, dynamic, }: CalculationProps2) => number;
110
+ /**
111
+ * Properties for DOM calculation.
112
+ */
113
+ type CalculationProps = {
114
+ blockIds: string[];
115
+ dynamic?: boolean | string;
116
+ margin?: number;
117
+ substract?: boolean;
118
+ onChange?: (results: {
119
+ blocksHeight: number;
120
+ blocksWidth: number;
121
+ remainingWidth: number;
122
+ remainingHeight: number;
123
+ }) => void;
124
+ };
125
+ /**
126
+ * Hook to calculate dimensions (height and width) of an element based on viewport and other block dimensions.
127
+ * @param params - Configuration object for dimension calculation.
128
+ * @returns An object containing the calculated height and width.
129
+ */
130
+ export declare const useDomCalculation: ({ blockIds, margin, substract, dynamic, onChange, }: CalculationProps) => {
131
+ height: number;
132
+ width: number;
133
+ };
@@ -260,6 +260,7 @@ export function useCopyToClipboard({ timeout = 2000 }) {
260
260
  return { isCopied, copy };
261
261
  }
262
262
  /**
263
+ *
263
264
  * Hook to calculate the height of an element based on viewport and other block heights.
264
265
  * @param params - Configuration object for height calculation.
265
266
  * @returns The calculated height.
@@ -292,3 +293,47 @@ export const useHeightCalculation = ({ blockIds = [], margin = 0, substract = tr
292
293
  }, []);
293
294
  return height;
294
295
  };
296
+ /**
297
+ * Hook to calculate dimensions (height and width) of an element based on viewport and other block dimensions.
298
+ * @param params - Configuration object for dimension calculation.
299
+ * @returns An object containing the calculated height and width.
300
+ */
301
+ export const useDomCalculation = ({ blockIds = [], margin = 0, substract = true, dynamic = false, onChange, }) => {
302
+ const [dimensions, setDimensions] = React.useState({
303
+ height: 500,
304
+ width: 500,
305
+ });
306
+ const handleCalculation = () => {
307
+ const blocksHeight = blockIds.reduce((prevHeight, id) => prevHeight + (document.getElementById(id)?.clientHeight || 0), 0);
308
+ const blocksWidth = blockIds.reduce((prevWidth, id) => prevWidth + (document.getElementById(id)?.clientWidth || 0), 0);
309
+ const height = substract
310
+ ? window.innerHeight - blocksHeight - margin
311
+ : blocksHeight + margin;
312
+ const width = substract
313
+ ? window.innerWidth - blocksWidth - margin
314
+ : blocksWidth + margin;
315
+ const newDimensions = { height, width };
316
+ setDimensions(newDimensions);
317
+ onChange?.({
318
+ blocksWidth,
319
+ blocksHeight,
320
+ remainingWidth: width,
321
+ remainingHeight: height,
322
+ });
323
+ };
324
+ useIsomorphicEffect(() => {
325
+ handleCalculation();
326
+ if (!dynamic)
327
+ return;
328
+ if (typeof dynamic === 'string') {
329
+ const resizableElement = document.getElementById(dynamic);
330
+ const resizeObserver = new ResizeObserver(() => handleCalculation());
331
+ if (resizableElement)
332
+ resizeObserver.observe(resizableElement);
333
+ return () => resizeObserver.disconnect();
334
+ }
335
+ window.addEventListener('resize', handleCalculation);
336
+ return () => window.removeEventListener('resize', handleCalculation);
337
+ }, []);
338
+ return dimensions;
339
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "4.1.1",
3
+ "version": "4.1.2",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",
@@ -15,33 +15,19 @@
15
15
  },
16
16
  "typesVersions": {
17
17
  "*": {
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
- ]
18
+ "core": ["dist/index.d.ts"],
19
+ "types": ["dist/types/index.d.ts"],
20
+ "hooks": ["dist/hooks/index.d.ts"],
21
+ "components": ["dist/components/index.d.ts"]
30
22
  }
31
23
  },
32
- "files": [
33
- "dist",
34
- "README.md"
35
- ],
24
+ "files": ["dist", "README.md"],
36
25
  "scripts": {
37
26
  "build": "tsc",
38
27
  "build:watch": "tsc --watch",
39
28
  "export": "tsc && npm publish"
40
29
  },
41
- "keywords": [
42
- "utils",
43
- "cn"
44
- ],
30
+ "keywords": ["utils", "cn"],
45
31
  "license": "ISC",
46
32
  "devDependencies": {
47
33
  "@types/node": "^22.4.0",