@sqlrooms/utils 0.4.0 → 0.4.1
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.d.ts +2 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +2 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useChartDimensions.d.ts +50 -0
- package/dist/hooks/useChartDimensions.d.ts.map +1 -0
- package/dist/hooks/useChartDimensions.js +47 -0
- package/dist/hooks/useChartDimensions.js.map +1 -0
- package/package.json +5 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the dimensions of a chart
|
|
3
|
+
* @interface ChartDimensions
|
|
4
|
+
* @property {number} width - The width of the chart in pixels
|
|
5
|
+
* @property {number} height - The height of the chart in pixels
|
|
6
|
+
*/
|
|
7
|
+
export interface ChartDimensions {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Props for the useChartDimensions hook
|
|
13
|
+
* @interface UseChartDimensionsProps
|
|
14
|
+
* @property {number | 'auto'} width - The explicitly provided width, or 'auto' for container-based width
|
|
15
|
+
* @property {number | 'auto'} height - The explicitly provided height, or 'auto' for aspect ratio-based height
|
|
16
|
+
* @property {number} aspectRatio - The desired width-to-height ratio when dimensions are auto-calculated
|
|
17
|
+
* @property {React.RefObject<HTMLElement>} containerRef - Reference to the container element
|
|
18
|
+
*/
|
|
19
|
+
export interface UseChartDimensionsProps {
|
|
20
|
+
width: number | 'auto';
|
|
21
|
+
height: number | 'auto';
|
|
22
|
+
aspectRatio: number;
|
|
23
|
+
containerRef: React.RefObject<HTMLElement>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A hook that calculates chart dimensions based on provided values and container size
|
|
27
|
+
*
|
|
28
|
+
* This hook handles various combinations of width/height specifications:
|
|
29
|
+
* - If both width and height are provided, uses those exact dimensions
|
|
30
|
+
* - If only width is provided, calculates height using the aspect ratio
|
|
31
|
+
* - If only height is provided, calculates width using the aspect ratio
|
|
32
|
+
* - If both are 'auto', uses container width and calculates height using the aspect ratio
|
|
33
|
+
*
|
|
34
|
+
* @param {UseChartDimensionsProps} props - The input parameters for dimension calculation
|
|
35
|
+
* @returns {ChartDimensions} The calculated width and height
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* const containerRef = useRef<HTMLDivElement>(null);
|
|
40
|
+
* const {width, height} = useChartDimensions({
|
|
41
|
+
* width: 'auto',
|
|
42
|
+
* height: 'auto',
|
|
43
|
+
* aspectRatio: 16/9,
|
|
44
|
+
* containerRef
|
|
45
|
+
* });
|
|
46
|
+
* // Returns dimensions based on container size
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function useChartDimensions({ width, height, aspectRatio, containerRef, }: UseChartDimensionsProps): ChartDimensions;
|
|
50
|
+
//# sourceMappingURL=useChartDimensions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useChartDimensions.d.ts","sourceRoot":"","sources":["../../src/hooks/useChartDimensions.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAuB;IACtC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;CAC5C;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,GACb,EAAE,uBAAuB,GAAG,eAAe,CAoB3C"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useResizeObserver } from 'usehooks-ts';
|
|
3
|
+
/**
|
|
4
|
+
* A hook that calculates chart dimensions based on provided values and container size
|
|
5
|
+
*
|
|
6
|
+
* This hook handles various combinations of width/height specifications:
|
|
7
|
+
* - If both width and height are provided, uses those exact dimensions
|
|
8
|
+
* - If only width is provided, calculates height using the aspect ratio
|
|
9
|
+
* - If only height is provided, calculates width using the aspect ratio
|
|
10
|
+
* - If both are 'auto', uses container width and calculates height using the aspect ratio
|
|
11
|
+
*
|
|
12
|
+
* @param {UseChartDimensionsProps} props - The input parameters for dimension calculation
|
|
13
|
+
* @returns {ChartDimensions} The calculated width and height
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* const containerRef = useRef<HTMLDivElement>(null);
|
|
18
|
+
* const {width, height} = useChartDimensions({
|
|
19
|
+
* width: 'auto',
|
|
20
|
+
* height: 'auto',
|
|
21
|
+
* aspectRatio: 16/9,
|
|
22
|
+
* containerRef
|
|
23
|
+
* });
|
|
24
|
+
* // Returns dimensions based on container size
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function useChartDimensions({ width, height, aspectRatio, containerRef, }) {
|
|
28
|
+
const { width: containerWidth = 0 } = useResizeObserver({
|
|
29
|
+
ref: containerRef,
|
|
30
|
+
box: 'border-box',
|
|
31
|
+
});
|
|
32
|
+
return useMemo(() => {
|
|
33
|
+
if (width !== 'auto' && height !== 'auto') {
|
|
34
|
+
return { width, height };
|
|
35
|
+
}
|
|
36
|
+
if (width !== 'auto') {
|
|
37
|
+
return { width, height: width / aspectRatio };
|
|
38
|
+
}
|
|
39
|
+
if (height !== 'auto') {
|
|
40
|
+
return { width: height * aspectRatio, height };
|
|
41
|
+
}
|
|
42
|
+
const finalWidth = containerWidth;
|
|
43
|
+
const finalHeight = finalWidth / aspectRatio;
|
|
44
|
+
return { width: finalWidth, height: finalHeight };
|
|
45
|
+
}, [containerWidth, aspectRatio, width, height]);
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=useChartDimensions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useChartDimensions.js","sourceRoot":"","sources":["../../src/hooks/useChartDimensions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAC;AA4B9C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,kBAAkB,CAAC,EACjC,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,GACY;IACxB,MAAM,EAAC,KAAK,EAAE,cAAc,GAAG,CAAC,EAAC,GAAG,iBAAiB,CAAC;QACpD,GAAG,EAAE,YAAY;QACjB,GAAG,EAAE,YAAY;KAClB,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,IAAI,KAAK,KAAK,MAAM,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;YAC1C,OAAO,EAAC,KAAK,EAAE,MAAM,EAAC,CAAC;QACzB,CAAC;QACD,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;YACrB,OAAO,EAAC,KAAK,EAAE,MAAM,EAAE,KAAK,GAAG,WAAW,EAAC,CAAC;QAC9C,CAAC;QACD,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;YACtB,OAAO,EAAC,KAAK,EAAE,MAAM,GAAG,WAAW,EAAE,MAAM,EAAC,CAAC;QAC/C,CAAC;QACD,MAAM,UAAU,GAAG,cAAc,CAAC;QAClC,MAAM,WAAW,GAAG,UAAU,GAAG,WAAW,CAAC;QAC7C,OAAO,EAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAC,CAAC;IAClD,CAAC,EAAE,CAAC,cAAc,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;AACnD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/utils",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -37,7 +37,9 @@
|
|
|
37
37
|
"@types/d3-color": "^3.1.3",
|
|
38
38
|
"@types/d3-format": "^3.0.4",
|
|
39
39
|
"@types/d3-time-format": "^4.0.3",
|
|
40
|
-
"@types/seedrandom": "^3.0.8"
|
|
40
|
+
"@types/seedrandom": "^3.0.8",
|
|
41
|
+
"react": "^19.0.0",
|
|
42
|
+
"usehooks-ts": "^3.1.1"
|
|
41
43
|
},
|
|
42
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "4260241d97423fd1839746ebb7980d01eaffde97"
|
|
43
45
|
}
|