@recharts/devtools 0.0.4 → 0.0.5

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.
Files changed (67) hide show
  1. package/README.md +2 -0
  2. package/dist/components/RechartsDevtools.js +58 -0
  3. package/dist/components/useEventDispatch.js +11 -0
  4. package/dist/constants.js +1 -0
  5. package/dist/context/RechartsDevtoolsContext.js +19 -0
  6. package/dist/generateMockData.js +14 -0
  7. package/dist/{src/hooks → hooks}/useSessionStorageState.js +6 -9
  8. package/dist/index.d.ts +5 -0
  9. package/dist/index.js +5 -0
  10. package/dist/{src/inspectors → inspectors}/ActiveTooltipLabelInspector.d.ts +1 -1
  11. package/dist/inspectors/ActiveTooltipLabelInspector.js +12 -0
  12. package/dist/{src/inspectors → inspectors}/ChartDimensionInspector.d.ts +1 -1
  13. package/dist/inspectors/ChartDimensionInspector.js +18 -0
  14. package/dist/{src/inspectors → inspectors}/MarginInspector.d.ts +1 -1
  15. package/dist/inspectors/MarginInspector.js +37 -0
  16. package/dist/{src/inspectors → inspectors}/OffsetInspector.d.ts +1 -1
  17. package/dist/inspectors/OffsetInspector.js +39 -0
  18. package/dist/{src/inspectors → inspectors}/PlotAreaInspector.d.ts +1 -1
  19. package/dist/inspectors/PlotAreaInspector.js +30 -0
  20. package/dist/{src/inspectors → inspectors}/UseActiveTooltipDataPointsInspector.d.ts +1 -1
  21. package/dist/inspectors/UseActiveTooltipDataPointsInspector.js +12 -0
  22. package/dist/{src/inspectors → inspectors}/XAxisDomainInspector.d.ts +1 -1
  23. package/dist/inspectors/XAxisDomainInspector.js +12 -0
  24. package/dist/{src/inspectors → inspectors}/YAxisDomainInspector.d.ts +1 -1
  25. package/dist/inspectors/YAxisDomainInspector.js +12 -0
  26. package/dist/inspectors/generic/ArrayInspector.js +27 -0
  27. package/dist/inspectors/generic/ObjectInspector.js +43 -0
  28. package/dist/inspectors/generic/PrimitiveInspector.js +8 -0
  29. package/dist/inspectors/utils/SvgDimensionShower.js +60 -0
  30. package/dist/{src/random.js → random.js} +2 -6
  31. package/package.json +11 -8
  32. package/dist/src/components/RechartsDevtools.js +0 -65
  33. package/dist/src/components/useEventDispatch.js +0 -14
  34. package/dist/src/constants.js +0 -4
  35. package/dist/src/context/RechartsDevtoolsContext.js +0 -58
  36. package/dist/src/generateMockData.js +0 -17
  37. package/dist/src/index.d.ts +0 -5
  38. package/dist/src/index.js +0 -21
  39. package/dist/src/inspectors/ActiveTooltipLabelInspector.js +0 -18
  40. package/dist/src/inspectors/ChartDimensionInspector.js +0 -24
  41. package/dist/src/inspectors/MarginInspector.js +0 -43
  42. package/dist/src/inspectors/OffsetInspector.js +0 -45
  43. package/dist/src/inspectors/PlotAreaInspector.js +0 -36
  44. package/dist/src/inspectors/UseActiveTooltipDataPointsInspector.js +0 -18
  45. package/dist/src/inspectors/XAxisDomainInspector.js +0 -18
  46. package/dist/src/inspectors/YAxisDomainInspector.js +0 -18
  47. package/dist/src/inspectors/generic/ArrayInspector.js +0 -63
  48. package/dist/src/inspectors/generic/ObjectInspector.js +0 -77
  49. package/dist/src/inspectors/generic/PrimitiveInspector.js +0 -15
  50. package/dist/src/inspectors/utils/SvgDimensionShower.js +0 -66
  51. package/dist/src/types.js +0 -2
  52. package/dist/test/generateMockData.spec.js +0 -27
  53. package/dist/test/random.spec.d.ts +0 -1
  54. package/dist/test/random.spec.js +0 -30
  55. /package/dist/{src/components → components}/RechartsDevtools.d.ts +0 -0
  56. /package/dist/{src/components → components}/useEventDispatch.d.ts +0 -0
  57. /package/dist/{src/constants.d.ts → constants.d.ts} +0 -0
  58. /package/dist/{src/context → context}/RechartsDevtoolsContext.d.ts +0 -0
  59. /package/dist/{src/generateMockData.d.ts → generateMockData.d.ts} +0 -0
  60. /package/dist/{src/hooks → hooks}/useSessionStorageState.d.ts +0 -0
  61. /package/dist/{src/inspectors → inspectors}/generic/ArrayInspector.d.ts +0 -0
  62. /package/dist/{src/inspectors → inspectors}/generic/ObjectInspector.d.ts +0 -0
  63. /package/dist/{src/inspectors → inspectors}/generic/PrimitiveInspector.d.ts +0 -0
  64. /package/dist/{src/inspectors → inspectors}/utils/SvgDimensionShower.d.ts +0 -0
  65. /package/dist/{src/random.d.ts → random.d.ts} +0 -0
  66. /package/dist/{src/types.d.ts → types.d.ts} +0 -0
  67. /package/dist/{test/generateMockData.spec.d.ts → types.js} +0 -0
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Devtools for Recharts.
4
4
 
5
+ [![npm version](https://badge.fury.io/js/@recharts%2Fdevtools.svg)](https://badge.fury.io/js/@recharts%2Fdevtools)
6
+
5
7
  ## Installation
6
8
 
7
9
  ```bash
@@ -0,0 +1,58 @@
1
+ import React from 'react';
2
+ import { createPortal } from 'react-dom';
3
+ import { RECHARTS_DEVTOOLS_PORTAL_ID } from '../constants.js';
4
+ import { ChartDimensionInspector } from '../inspectors/ChartDimensionInspector.js';
5
+ import { MarginInspector } from '../inspectors/MarginInspector.js';
6
+ import { OffsetInspector } from '../inspectors/OffsetInspector.js';
7
+ import { PlotAreaInspector } from '../inspectors/PlotAreaInspector.js';
8
+ import { UseActiveTooltipDataPointsInspector } from '../inspectors/UseActiveTooltipDataPointsInspector.js';
9
+ import { XAxisDomainInspector } from '../inspectors/XAxisDomainInspector.js';
10
+ import { YAxisDomainInspector } from '../inspectors/YAxisDomainInspector.js';
11
+ import { ActiveTooltipLabelInspector } from '../inspectors/ActiveTooltipLabelInspector.js';
12
+ import { useSessionStorageState } from '../hooks/useSessionStorageState.js';
13
+ import { useRechartsDevtoolsContext } from '../context/RechartsDevtoolsContext.js';
14
+ const INSPECTORS = {
15
+ 'useChartWidth | useChartHeight': ChartDimensionInspector,
16
+ useMargin: MarginInspector,
17
+ useOffset: OffsetInspector,
18
+ usePlotArea: PlotAreaInspector,
19
+ useActiveTooltipDataPoints: UseActiveTooltipDataPointsInspector,
20
+ useXAxisDomain: XAxisDomainInspector,
21
+ useYAxisDomain: YAxisDomainInspector,
22
+ useActiveTooltipLabel: ActiveTooltipLabelInspector,
23
+ };
24
+ function useSelectedInspector() {
25
+ const [selectedInspectorId, setSelectedInspectorId] = useSessionStorageState('useChartWidth | useChartHeight');
26
+ const selectedInspector = INSPECTORS[selectedInspectorId];
27
+ if (!selectedInspector) {
28
+ setSelectedInspectorId('useChartWidth | useChartHeight');
29
+ }
30
+ return { selectedInspectorId, setSelectedInspectorId, selectedInspector };
31
+ }
32
+ export const RechartsDevtools = () => {
33
+ const contextId = useRechartsDevtoolsContext();
34
+ const portalId = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
35
+ const [container, setContainer] = React.useState(null);
36
+ React.useEffect(() => {
37
+ setContainer(document.getElementById(portalId));
38
+ }, [portalId]);
39
+ const { selectedInspectorId, setSelectedInspectorId, selectedInspector } = useSelectedInspector();
40
+ const [isOverlayEnabled, setIsOverlayEnabled] = useSessionStorageState(false);
41
+ if (!container || !selectedInspector) {
42
+ return null;
43
+ }
44
+ const { Inspector, Overlay } = selectedInspector;
45
+ return (React.createElement(React.Fragment, null,
46
+ Overlay && isOverlayEnabled && React.createElement(Overlay, null),
47
+ createPortal(React.createElement("div", { className: "recharts-devtools", style: { fontFamily: 'monospace', fontSize: '12px' } },
48
+ React.createElement("div", { style: { marginBottom: '10px', paddingBottom: '10px', borderBottom: '1px solid #ccc' } },
49
+ React.createElement("div", { style: { display: 'flex', alignItems: 'center', marginBottom: '4px', gap: '8px' } },
50
+ React.createElement("label", null, "Inspect Hook:"),
51
+ React.createElement("select", { value: selectedInspectorId, onChange: (e) => setSelectedInspectorId(e.target.value), style: { padding: '4px' } }, Object.keys(INSPECTORS).map((key) => (React.createElement("option", { key: key, value: key }, key)))),
52
+ Overlay && (React.createElement("div", { style: { marginTop: '4px' } },
53
+ React.createElement("label", { style: { cursor: 'pointer', display: 'flex', alignItems: 'center' } },
54
+ React.createElement("input", { type: "checkbox", checked: isOverlayEnabled, onChange: (e) => setIsOverlayEnabled(e.target.checked), style: { marginRight: '4px' } }),
55
+ "Show Overlay on chart"))))),
56
+ React.createElement("div", { style: { padding: '10px 0' } },
57
+ React.createElement(Inspector, null))), container)));
58
+ };
@@ -0,0 +1,11 @@
1
+ import { useEffect } from "react";
2
+ import { RECHARTS_DEVTOOLS_PORTAL_ID } from "../constants.js";
3
+ export function useEventDispatch(detail) {
4
+ useEffect(() => {
5
+ const container = document.getElementById(RECHARTS_DEVTOOLS_PORTAL_ID);
6
+ if (!container)
7
+ return;
8
+ const event = new CustomEvent('recharts-devtools-change', { detail });
9
+ container.dispatchEvent(event);
10
+ }, [detail]);
11
+ }
@@ -0,0 +1 @@
1
+ export const RECHARTS_DEVTOOLS_PORTAL_ID = 'recharts-devtools-portal';
@@ -0,0 +1,19 @@
1
+ import React, { createContext, useContext, useId } from 'react';
2
+ import { RECHARTS_DEVTOOLS_PORTAL_ID } from '../constants.js';
3
+ const Context = createContext(null);
4
+ export const RechartsDevtoolsContext = ({ children }) => {
5
+ // Generate a unique ID for this instance.
6
+ // React.useId returns a string that includes colons, e.g., ":r0:"
7
+ // We sanitize it to make it a valid HTML ID and safer for CSS selectors if needed.
8
+ const rawId = useId();
9
+ const portalId = `recharts-devtools-portal-${rawId.replace(/:/g, '')}`;
10
+ return React.createElement(Context.Provider, { value: portalId }, children);
11
+ };
12
+ export const useRechartsDevtoolsContext = () => {
13
+ return useContext(Context);
14
+ };
15
+ export const RechartsDevtoolsPortal = (props) => {
16
+ const contextId = useRechartsDevtoolsContext();
17
+ const id = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
18
+ return React.createElement("div", { id: id, ...props });
19
+ };
@@ -0,0 +1,14 @@
1
+ import { between, random } from './random.js';
2
+ export function generateMockData(length, seed) {
3
+ const result = [];
4
+ const gen = random(seed);
5
+ for (let i = 0; i < length; i++) {
6
+ result.push({
7
+ label: `Iter: ${i}`,
8
+ x: between(gen, 100, 300),
9
+ y: between(gen, 400, 800),
10
+ z: between(gen, 1000, 2000),
11
+ });
12
+ }
13
+ return result;
14
+ }
@@ -1,16 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSessionStorageState = void 0;
4
- const react_1 = require("react");
1
+ import { useId, useState, useEffect } from "react";
5
2
  /**
6
3
  * This hook works just the same as `useState`, but it also stores the state in the session storage.
7
4
  * It uses `useId` from React to generate a unique key for the state.
8
5
  *
9
6
  * In browsers without session storage support, it will fallback to `useState`.
10
7
  */
11
- const useSessionStorageState = (initialState) => {
12
- const id = (0, react_1.useId)();
13
- const [state, setState] = (0, react_1.useState)(() => {
8
+ export const useSessionStorageState = (initialState) => {
9
+ const id = useId();
10
+ const [state, setState] = useState(() => {
14
11
  try {
15
12
  const storedState = sessionStorage.getItem(id);
16
13
  return storedState ? JSON.parse(storedState) : initialState;
@@ -19,13 +16,13 @@ const useSessionStorageState = (initialState) => {
19
16
  return initialState;
20
17
  }
21
18
  });
22
- (0, react_1.useEffect)(() => {
19
+ useEffect(() => {
23
20
  try {
24
21
  sessionStorage.setItem(id, JSON.stringify(state));
25
22
  }
26
23
  catch {
24
+ /* Ignored intentionally */
27
25
  }
28
26
  }, [state]);
29
27
  return [state, setState];
30
28
  };
31
- exports.useSessionStorageState = useSessionStorageState;
@@ -0,0 +1,5 @@
1
+ export { between, random } from './random.js';
2
+ export { generateMockData } from './generateMockData.js';
3
+ export { RechartsDevtools } from './components/RechartsDevtools.js';
4
+ export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
5
+ export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { between, random } from './random.js';
2
+ export { generateMockData } from './generateMockData.js';
3
+ export { RechartsDevtools } from './components/RechartsDevtools.js';
4
+ export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
5
+ export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const ActiveTooltipLabelInspector: InspectorDef;
@@ -0,0 +1,12 @@
1
+ import React from 'react';
2
+ import { useActiveTooltipLabel } from 'recharts';
3
+ import { PrimitiveInspector } from './generic/PrimitiveInspector.js';
4
+ import { useEventDispatch } from '../components/useEventDispatch.js';
5
+ const Inspector = () => {
6
+ const activeLabel = useActiveTooltipLabel();
7
+ useEventDispatch(activeLabel);
8
+ return React.createElement(PrimitiveInspector, { value: activeLabel });
9
+ };
10
+ export const ActiveTooltipLabelInspector = {
11
+ Inspector,
12
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const ChartDimensionInspector: InspectorDef;
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ import { useChartHeight, useChartWidth } from 'recharts';
3
+ import { useEventDispatch } from '../components/useEventDispatch.js';
4
+ const Inspector = () => {
5
+ const chartWidth = useChartWidth();
6
+ const chartHeight = useChartHeight();
7
+ useEventDispatch({ useChartWidth: chartWidth, useChartHeight: chartHeight });
8
+ return (React.createElement("div", null,
9
+ React.createElement("div", null,
10
+ "useChartWidth: ",
11
+ chartWidth),
12
+ React.createElement("div", null,
13
+ "useChartHeight: ",
14
+ chartHeight)));
15
+ };
16
+ export const ChartDimensionInspector = {
17
+ Inspector,
18
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const MarginInspector: InspectorDef;
@@ -0,0 +1,37 @@
1
+ import React from 'react';
2
+ import { useChartHeight, useChartWidth, useMargin } from 'recharts';
3
+ import { SvgDimensionShower } from './utils/SvgDimensionShower.js';
4
+ import { ObjectInspector } from './generic/ObjectInspector.js';
5
+ const Overlay = () => {
6
+ const margin = useMargin();
7
+ const height = useChartHeight();
8
+ const width = useChartWidth();
9
+ if (margin == null || width == null || height == null) {
10
+ return null;
11
+ }
12
+ return (React.createElement(React.Fragment, null,
13
+ React.createElement(SvgDimensionShower, { width: margin.left, height: height, labels: {
14
+ background: 'Left',
15
+ horizontal: 'useMargin.left',
16
+ } }),
17
+ React.createElement(SvgDimensionShower, { width: margin.right, x: width - margin.right, height: height, labels: {
18
+ background: 'Right',
19
+ horizontal: 'useMargin.right',
20
+ } }),
21
+ React.createElement(SvgDimensionShower, { width: width, height: margin.top, labels: {
22
+ background: 'Top',
23
+ vertical: 'useMargin.top',
24
+ } }),
25
+ React.createElement(SvgDimensionShower, { width: width, height: margin.bottom, y: height - margin.bottom, labels: {
26
+ background: 'Bottom',
27
+ vertical: 'useMargin.bottom',
28
+ } })));
29
+ };
30
+ const Inspector = () => {
31
+ const margin = useMargin();
32
+ return React.createElement(ObjectInspector, { obj: margin });
33
+ };
34
+ export const MarginInspector = {
35
+ Overlay,
36
+ Inspector,
37
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const OffsetInspector: InspectorDef;
@@ -0,0 +1,39 @@
1
+ import React from 'react';
2
+ import { useChartHeight, useChartWidth, useOffset } from 'recharts';
3
+ import { SvgDimensionShower } from './utils/SvgDimensionShower.js';
4
+ import { ObjectInspector } from './generic/ObjectInspector.js';
5
+ import { useEventDispatch } from '../components/useEventDispatch.js';
6
+ const Overlay = () => {
7
+ const offset = useOffset();
8
+ const height = useChartHeight();
9
+ const width = useChartWidth();
10
+ if (offset == null || width == null || height == null) {
11
+ return null;
12
+ }
13
+ return (React.createElement(React.Fragment, null,
14
+ React.createElement(SvgDimensionShower, { width: offset.left, height: height, labels: {
15
+ background: 'Left',
16
+ horizontal: 'useOffset.left',
17
+ } }),
18
+ React.createElement(SvgDimensionShower, { width: offset.right, x: width - offset.right, height: height, labels: {
19
+ background: 'Right',
20
+ horizontal: 'useOffset.right',
21
+ } }),
22
+ React.createElement(SvgDimensionShower, { width: width, height: offset.top, labels: {
23
+ background: 'Top',
24
+ vertical: 'useOffset.top',
25
+ } }),
26
+ React.createElement(SvgDimensionShower, { width: width, height: offset.bottom, y: height - offset.bottom, labels: {
27
+ background: 'Bottom',
28
+ vertical: 'useOffset.bottom',
29
+ } })));
30
+ };
31
+ const Inspector = () => {
32
+ const offset = useOffset();
33
+ useEventDispatch(offset);
34
+ return React.createElement(ObjectInspector, { obj: offset });
35
+ };
36
+ export const OffsetInspector = {
37
+ Overlay,
38
+ Inspector,
39
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const PlotAreaInspector: InspectorDef;
@@ -0,0 +1,30 @@
1
+ import React from 'react';
2
+ import { useChartHeight, useChartWidth, usePlotArea } from 'recharts';
3
+ import { SvgDimensionShower } from './utils/SvgDimensionShower.js';
4
+ import { ObjectInspector } from './generic/ObjectInspector.js';
5
+ import { useEventDispatch } from '../components/useEventDispatch.js';
6
+ const Overlay = () => {
7
+ const plotArea = usePlotArea();
8
+ const width = useChartWidth();
9
+ const height = useChartHeight();
10
+ if (plotArea == null || width == null || height == null) {
11
+ return null;
12
+ }
13
+ return (React.createElement(React.Fragment, null,
14
+ React.createElement(SvgDimensionShower, { width: width, height: plotArea.y, labels: { background: 'y', vertical: 'usePlotArea.y' } }),
15
+ React.createElement(SvgDimensionShower, { width: plotArea.x, height: height, labels: { background: 'x', horizontal: 'usePlotArea.x' } }),
16
+ React.createElement(SvgDimensionShower, { x: plotArea.x, y: plotArea.y, width: plotArea.width, height: plotArea.height, labels: {
17
+ background: 'Plot area',
18
+ horizontal: 'usePlotArea.width',
19
+ vertical: 'usePlotArea.height',
20
+ } })));
21
+ };
22
+ const Inspector = () => {
23
+ const plotArea = usePlotArea();
24
+ useEventDispatch(plotArea);
25
+ return React.createElement(ObjectInspector, { obj: plotArea });
26
+ };
27
+ export const PlotAreaInspector = {
28
+ Overlay,
29
+ Inspector,
30
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const UseActiveTooltipDataPointsInspector: InspectorDef;
@@ -0,0 +1,12 @@
1
+ import React from 'react';
2
+ import { useActiveTooltipDataPoints } from 'recharts';
3
+ import { ArrayInspector } from './generic/ArrayInspector.js';
4
+ import { useEventDispatch } from '../components/useEventDispatch.js';
5
+ const Inspector = () => {
6
+ const dataPoints = useActiveTooltipDataPoints();
7
+ useEventDispatch(dataPoints);
8
+ return React.createElement(ArrayInspector, { arr: dataPoints, expandByDefault: true });
9
+ };
10
+ export const UseActiveTooltipDataPointsInspector = {
11
+ Inspector,
12
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const XAxisDomainInspector: InspectorDef;
@@ -0,0 +1,12 @@
1
+ import React from 'react';
2
+ import { useXAxisDomain } from 'recharts';
3
+ import { ArrayInspector } from './generic/ArrayInspector.js';
4
+ import { useEventDispatch } from '../components/useEventDispatch.js';
5
+ const Inspector = () => {
6
+ const xAxisDomain = useXAxisDomain();
7
+ useEventDispatch(xAxisDomain);
8
+ return React.createElement(ArrayInspector, { arr: xAxisDomain });
9
+ };
10
+ export const XAxisDomainInspector = {
11
+ Inspector,
12
+ };
@@ -1,2 +1,2 @@
1
- import { InspectorDef } from '../types';
1
+ import { InspectorDef } from '../types.js';
2
2
  export declare const YAxisDomainInspector: InspectorDef;
@@ -0,0 +1,12 @@
1
+ import React from 'react';
2
+ import { useYAxisDomain } from 'recharts';
3
+ import { ArrayInspector } from './generic/ArrayInspector.js';
4
+ import { useEventDispatch } from '../components/useEventDispatch.js';
5
+ const Inspector = () => {
6
+ const yAxisDomain = useYAxisDomain();
7
+ useEventDispatch(yAxisDomain);
8
+ return React.createElement(ArrayInspector, { arr: yAxisDomain });
9
+ };
10
+ export const YAxisDomainInspector = {
11
+ Inspector,
12
+ };
@@ -0,0 +1,27 @@
1
+ import React, { useState } from 'react';
2
+ import { PrimitiveInspector, serializePrimitive } from './PrimitiveInspector.js';
3
+ function ArrayOfObjectsInspector({ arr, expand }) {
4
+ if (!expand) {
5
+ return React.createElement("code", null,
6
+ "Array(",
7
+ arr.length,
8
+ ")");
9
+ }
10
+ return (React.createElement("pre", null,
11
+ React.createElement("code", null,
12
+ "[",
13
+ arr.map(serializePrimitive).join(',\n'),
14
+ "]")));
15
+ }
16
+ export function ArrayInspector({ arr, expandByDefault, }) {
17
+ if (arr == null) {
18
+ return React.createElement(PrimitiveInspector, { value: arr });
19
+ }
20
+ const length = arr.length ?? 0;
21
+ const typeofArr = typeof arr[0];
22
+ const shouldExpandByDefault = expandByDefault || length <= 1 || (typeofArr !== 'object' && typeofArr !== 'function');
23
+ const [expand, setExpand] = useState(shouldExpandByDefault);
24
+ return (React.createElement(React.Fragment, null,
25
+ expand ? (React.createElement("button", { type: "button", onClick: () => setExpand(false) }, "Collapse")) : (React.createElement("button", { type: "button", onClick: () => setExpand(true) }, "Expand")),
26
+ React.createElement(ArrayOfObjectsInspector, { arr: arr, expand: expand })));
27
+ }
@@ -0,0 +1,43 @@
1
+ import React, { useState } from 'react';
2
+ import { PrimitiveInspector } from './PrimitiveInspector.js';
3
+ /*
4
+ * Values that are expanded by default:
5
+ * - primitives
6
+ * - objects with 0 or 1 or 2 keys
7
+ * - arrays with 0 or 1 elements
8
+ */
9
+ function shouldExpandByDefault(value) {
10
+ if (Array.isArray(value)) {
11
+ return value.length <= 1;
12
+ }
13
+ if (value != null && typeof value === 'object') {
14
+ return Object.keys(value).length <= 2;
15
+ }
16
+ return true;
17
+ }
18
+ function NotPrimitiveInspector({ value }) {
19
+ const [expand, setExpand] = useState(shouldExpandByDefault(value));
20
+ return (React.createElement(React.Fragment, null,
21
+ expand ? (React.createElement("button", { type: "button", onClick: () => setExpand(false) }, "Collapse")) : (React.createElement("button", { type: "button", onClick: () => setExpand(true) }, "Expand")),
22
+ expand ? (React.createElement("pre", null,
23
+ React.createElement("code", null, JSON.stringify(value, null, 2)))) : (React.createElement("code", null, typeof value))));
24
+ }
25
+ export function ValueInspector({ value }) {
26
+ if (value == null ||
27
+ typeof value === 'string' ||
28
+ typeof value === 'number' ||
29
+ typeof value === 'boolean') {
30
+ return React.createElement(PrimitiveInspector, { value: value });
31
+ }
32
+ return React.createElement(NotPrimitiveInspector, { value: value });
33
+ }
34
+ export function ObjectInspector({ obj }) {
35
+ if (obj == null) {
36
+ return React.createElement("code", null, JSON.stringify(obj));
37
+ }
38
+ const keys = Object.keys(obj);
39
+ return (React.createElement("div", null, keys.map((key) => (React.createElement("div", { key: key },
40
+ React.createElement("strong", null, key),
41
+ ": ",
42
+ React.createElement(ValueInspector, { value: obj[key] }))))));
43
+ }
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ export function serializePrimitive(value) {
3
+ return value === undefined ? 'undefined' : JSON.stringify(value, null, 2);
4
+ }
5
+ export function PrimitiveInspector({ value }) {
6
+ const str = serializePrimitive(value);
7
+ return React.createElement("code", null, str);
8
+ }
@@ -0,0 +1,60 @@
1
+ import React from 'react';
2
+ /**
3
+ * Renders a line with arrows on left and right.
4
+ * @constructor
5
+ */
6
+ function HorizontalLineWithArrows({ x1, x2, y, label, stroke = 'black', }) {
7
+ const strokeWidth = 2;
8
+ const arrowSize = 7;
9
+ const width = x2 - x1;
10
+ return (React.createElement(React.Fragment, null,
11
+ React.createElement("line", { x1: x1, y1: y, x2: x2, y2: y, stroke: stroke, strokeWidth: strokeWidth }),
12
+ React.createElement("line", { x1: x1, y1: y, x2: x1 + arrowSize, y2: y - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
13
+ React.createElement("line", { x1: x1, y1: y, x2: x1 + arrowSize, y2: y + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
14
+ React.createElement("line", { x1: x2, y1: y, x2: x2 - arrowSize, y2: y - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
15
+ React.createElement("line", { x1: x2, y1: y, x2: x2 - arrowSize, y2: y + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
16
+ React.createElement("text", { x: width * 0.9, y: y + 8, textAnchor: "end", dominantBaseline: "hanging", stroke: "red" }, label)));
17
+ }
18
+ function Background({ width, height, label, }) {
19
+ const patternId = `pattern+${label.replace(/\s+/g, '-')}`;
20
+ return (React.createElement("svg", { width: width, height: height, style: { position: 'absolute', top: 0, left: 0 } },
21
+ React.createElement("defs", null,
22
+ React.createElement("pattern", { id: patternId, x: 0, y: 0, width: 120, height: 50, patternUnits: "userSpaceOnUse", patternTransform: "rotate(30)" },
23
+ React.createElement("text", { x: 0, y: 10, fontSize: 24, stroke: "#eee", fill: "#eee", dominantBaseline: "hanging" }, label.toUpperCase()))),
24
+ React.createElement("rect", { x: 0, y: 0, width: width, height: height, fill: `url(#${patternId})` })));
25
+ }
26
+ function VerticalLineWithArrows({ x, y1, y2, label, stroke = 'black', }) {
27
+ const strokeWidth = 2;
28
+ const arrowSize = 7;
29
+ const height = y2 - y1;
30
+ return (React.createElement(React.Fragment, null,
31
+ React.createElement("line", { x1: x, y1: y1, x2: x, y2: y2, stroke: stroke, strokeWidth: strokeWidth }),
32
+ React.createElement("line", { x1: x, y1: y1, x2: x - arrowSize, y2: y1 + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
33
+ React.createElement("line", { x1: x, y1: y1, x2: x + arrowSize, y2: y1 + arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
34
+ React.createElement("line", { x1: x, y1: y2, x2: x - arrowSize, y2: y2 - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
35
+ React.createElement("line", { x1: x, y1: y2, x2: x + arrowSize, y2: y2 - arrowSize, stroke: stroke, strokeWidth: strokeWidth }),
36
+ React.createElement("text", { x: x + 8, y: height * 0.7, textAnchor: "start", dominantBaseline: "middle", stroke: "blue" }, label)));
37
+ }
38
+ /**
39
+ * Creates a label for the horizontal line.
40
+ * It will trim information to save space if the width is too small.
41
+ * @param width number of pixels to display
42
+ * @param label description of the width
43
+ * @return the label to display
44
+ */
45
+ function makeHorizontalLabel(width, label) {
46
+ if (width < 50) {
47
+ return String(width);
48
+ }
49
+ if (width < 100) {
50
+ return `${width}px`;
51
+ }
52
+ return `${label}: ${width}px`;
53
+ }
54
+ export function SvgDimensionShower({ x = 0, y = 0, width, height, labels, }) {
55
+ return (React.createElement("svg", { width: width, height: height, x: x, y: y, style: { pointerEvents: 'none' } },
56
+ React.createElement(Background, { width: width, height: height, label: labels.background }),
57
+ React.createElement("rect", { x: 0, y: 0, width: width, height: height, fill: "none", stroke: "green", strokeWidth: 5, strokeDasharray: "3 3" }),
58
+ labels.horizontal && (React.createElement(HorizontalLineWithArrows, { x1: 0, y: height * 0.15, x2: width, stroke: "red", label: makeHorizontalLabel(width, labels.horizontal) })),
59
+ labels.vertical && (React.createElement(VerticalLineWithArrows, { x: width * 0.1, y1: 0, y2: height, stroke: "blue", label: `${labels.vertical}: ${height}px` }))));
60
+ }
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.random = random;
4
- exports.between = between;
5
1
  /**
6
2
  * Returns a random number generator.
7
3
  *
@@ -16,7 +12,7 @@ exports.between = between;
16
12
  * @param {number} seed use this for deterministic random generators in tests or storybooks
17
13
  * @return {Function} generator
18
14
  */
19
- function* random(seed) {
15
+ export function* random(seed) {
20
16
  // LCG good enough for our purposes https://en.wikipedia.org/wiki/Linear_congruential_generator
21
17
  const m = 2 ** 16 + 1;
22
18
  const a = 75;
@@ -34,7 +30,7 @@ function* random(seed) {
34
30
  yield Math.round(x);
35
31
  }
36
32
  }
37
- function between(rng, min, max) {
33
+ export function between(rng, min, max) {
38
34
  const val = rng.next().value;
39
35
  return (val % (max - min)) + min;
40
36
  }
package/package.json CHANGED
@@ -3,10 +3,11 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.0.4",
6
+ "type": "module",
7
+ "version": "0.0.5",
7
8
  "description": "Devtools and helper functions for Recharts",
8
- "main": "dist/src/index.js",
9
- "types": "dist/src/index.d.ts",
9
+ "main": "dist/index.js",
10
+ "types": "dist/index.d.ts",
10
11
  "files": [
11
12
  "dist"
12
13
  ],
@@ -14,9 +15,10 @@
14
15
  "url": "https://github.com/recharts/devtools"
15
16
  },
16
17
  "scripts": {
17
- "build": "tsc",
18
+ "build": "tsc -p tsconfig.build.json",
19
+ "type-check": "tsc --noEmit",
18
20
  "test": "vitest run",
19
- "lint": "eslint src test --ext .ts",
21
+ "lint": "eslint src test",
20
22
  "format": "prettier --write \"{src,test}/**/*.ts\"",
21
23
  "release": "node ./scripts/publish.js"
22
24
  },
@@ -31,11 +33,12 @@
31
33
  "@types/node": "^20.0.0",
32
34
  "@types/react": "^18.0.0",
33
35
  "@types/react-dom": "^18.0.0",
34
- "@typescript-eslint/eslint-plugin": "^6.0.0",
35
- "@typescript-eslint/parser": "^6.0.0",
36
- "eslint": "^8.0.0",
36
+ "eslint": "^9.0.0",
37
+ "eslint-plugin-import": "^2.31.0",
38
+ "globals": "^15.0.0",
37
39
  "prettier": "^3.0.0",
38
40
  "typescript": "^5.0.0",
41
+ "typescript-eslint": "^8.0.0",
39
42
  "vitest": "^1.0.0",
40
43
  "react": "^19.0.0",
41
44
  "react-dom": "^19.0.0",