@recharts/devtools 0.0.3 → 0.0.4
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/README.md +49 -22
- package/dist/src/components/RechartsDevtools.js +25 -40
- package/dist/src/context/RechartsDevtoolsContext.d.ts +8 -0
- package/dist/src/context/RechartsDevtoolsContext.js +58 -0
- package/dist/src/hooks/useSessionStorageState.d.ts +7 -0
- package/dist/src/hooks/useSessionStorageState.js +31 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,39 +1,66 @@
|
|
|
1
1
|
# @recharts/devtools
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Intended for development and debugging purpose only. We won't stop you from using this in prod but also we won't support it. Expect breaking changes in every release.
|
|
3
|
+
Devtools for Recharts.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
npm install @recharts/devtools
|
|
8
|
+
npm install @recharts/devtools --save-dev
|
|
11
9
|
```
|
|
12
10
|
|
|
13
11
|
## Usage
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
### Basic Usage
|
|
14
|
+
|
|
15
|
+
Import the `RechartsDevtools` component and render it inside of your chart.
|
|
16
|
+
|
|
17
|
+
Add a div with specific ID where the devtools will render a portal.
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
```tsx
|
|
20
|
+
import { RechartsDevtools, RECHARTS_DEVTOOLS_PORTAL_ID } from '@recharts/devtools';
|
|
21
|
+
import { AreaChart } from 'recharts';
|
|
22
|
+
|
|
23
|
+
function App() {
|
|
24
|
+
return (
|
|
25
|
+
<article>
|
|
26
|
+
<AreaChart>
|
|
27
|
+
{/* The DevTools component reads state from inside of the chart */}
|
|
28
|
+
<RechartsDevtools />
|
|
29
|
+
</AreaChart>
|
|
30
|
+
{/* The Portal component renders the devtools UI, debugging information, hooks and other stuff */}
|
|
31
|
+
<div id={RECHARTS_DEVTOOLS_PORTAL_ID} />
|
|
32
|
+
</article>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
20
35
|
```
|
|
21
36
|
|
|
22
|
-
|
|
37
|
+
### Multiple Instances (Context Mode)
|
|
23
38
|
|
|
24
|
-
|
|
25
|
-
- `npm run test`: Run tests with Vitest
|
|
26
|
-
- `npm run lint`: Lint code
|
|
39
|
+
If you have multiple charts or devtools instances on the same page (e.g., in a documentation site with live editors), you should use the `RechartsDevtoolsContext` and `RechartsDevtoolsPortal` to ensure each devtools instance targets the correct location without ID conflicts.
|
|
27
40
|
|
|
28
|
-
|
|
41
|
+
1. Wrap your chart and editor/devtools area with `RechartsDevtoolsContext`. One context for one devtools.
|
|
42
|
+
2. Place `RechartsDevtoolsPortal` where you want the devtools UI to be rendered.
|
|
43
|
+
3. `RechartsDevtools` will automatically detect the context and render into the associated portal.
|
|
29
44
|
|
|
30
|
-
|
|
45
|
+
```tsx
|
|
46
|
+
import { RechartsDevtools, RechartsDevtoolsContext, RechartsDevtoolsPortal } from '@recharts/devtools';
|
|
47
|
+
import { AreaChart, LineChart } from 'recharts';
|
|
31
48
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
49
|
+
function EditorWithPreview() {
|
|
50
|
+
return (
|
|
51
|
+
<RechartsDevtoolsContext>
|
|
52
|
+
<AreaChart>
|
|
53
|
+
<RechartsDevtools />
|
|
54
|
+
</AreaChart>
|
|
55
|
+
<RechartsDevtoolsPortal />
|
|
56
|
+
</RechartsDevtoolsContext>
|
|
57
|
+
|
|
58
|
+
<RechartsDevtoolsContext>
|
|
59
|
+
<LineChart>
|
|
60
|
+
<RechartsDevtools />
|
|
61
|
+
</LineChart>
|
|
62
|
+
<RechartsDevtoolsPortal />
|
|
63
|
+
</RechartsDevtoolsContext>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
```
|
|
@@ -1,40 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
35
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
6
|
exports.RechartsDevtools = void 0;
|
|
37
|
-
const react_1 =
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
38
8
|
const react_dom_1 = require("react-dom");
|
|
39
9
|
const constants_1 = require("../constants");
|
|
40
10
|
const ChartDimensionInspector_1 = require("../inspectors/ChartDimensionInspector");
|
|
@@ -45,6 +15,8 @@ const UseActiveTooltipDataPointsInspector_1 = require("../inspectors/UseActiveTo
|
|
|
45
15
|
const XAxisDomainInspector_1 = require("../inspectors/XAxisDomainInspector");
|
|
46
16
|
const YAxisDomainInspector_1 = require("../inspectors/YAxisDomainInspector");
|
|
47
17
|
const ActiveTooltipLabelInspector_1 = require("../inspectors/ActiveTooltipLabelInspector");
|
|
18
|
+
const useSessionStorageState_1 = require("../hooks/useSessionStorageState");
|
|
19
|
+
const RechartsDevtoolsContext_1 = require("../context/RechartsDevtoolsContext");
|
|
48
20
|
const INSPECTORS = {
|
|
49
21
|
'useChartWidth | useChartHeight': ChartDimensionInspector_1.ChartDimensionInspector,
|
|
50
22
|
useMargin: MarginInspector_1.MarginInspector,
|
|
@@ -55,21 +27,34 @@ const INSPECTORS = {
|
|
|
55
27
|
useYAxisDomain: YAxisDomainInspector_1.YAxisDomainInspector,
|
|
56
28
|
useActiveTooltipLabel: ActiveTooltipLabelInspector_1.ActiveTooltipLabelInspector,
|
|
57
29
|
};
|
|
30
|
+
function useSelectedInspector() {
|
|
31
|
+
const [selectedInspectorId, setSelectedInspectorId] = (0, useSessionStorageState_1.useSessionStorageState)('useChartWidth | useChartHeight');
|
|
32
|
+
const selectedInspector = INSPECTORS[selectedInspectorId];
|
|
33
|
+
if (!selectedInspector) {
|
|
34
|
+
setSelectedInspectorId('useChartWidth | useChartHeight');
|
|
35
|
+
}
|
|
36
|
+
return { selectedInspectorId, setSelectedInspectorId, selectedInspector };
|
|
37
|
+
}
|
|
58
38
|
const RechartsDevtools = () => {
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const [
|
|
62
|
-
|
|
39
|
+
const contextId = (0, RechartsDevtoolsContext_1.useRechartsDevtoolsContext)();
|
|
40
|
+
const portalId = contextId ?? constants_1.RECHARTS_DEVTOOLS_PORTAL_ID;
|
|
41
|
+
const [container, setContainer] = react_1.default.useState(null);
|
|
42
|
+
react_1.default.useEffect(() => {
|
|
43
|
+
setContainer(document.getElementById(portalId));
|
|
44
|
+
}, [portalId]);
|
|
45
|
+
const { selectedInspectorId, setSelectedInspectorId, selectedInspector } = useSelectedInspector();
|
|
46
|
+
const [isOverlayEnabled, setIsOverlayEnabled] = (0, useSessionStorageState_1.useSessionStorageState)(false);
|
|
47
|
+
if (!container || !selectedInspector) {
|
|
63
48
|
return null;
|
|
64
49
|
}
|
|
65
|
-
const { Inspector, Overlay } =
|
|
50
|
+
const { Inspector, Overlay } = selectedInspector;
|
|
66
51
|
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
67
52
|
Overlay && isOverlayEnabled && react_1.default.createElement(Overlay, null),
|
|
68
53
|
(0, react_dom_1.createPortal)(react_1.default.createElement("div", { className: "recharts-devtools", style: { fontFamily: 'monospace', fontSize: '12px' } },
|
|
69
54
|
react_1.default.createElement("div", { style: { marginBottom: '10px', paddingBottom: '10px', borderBottom: '1px solid #ccc' } },
|
|
70
55
|
react_1.default.createElement("div", { style: { display: 'flex', alignItems: 'center', marginBottom: '4px', gap: '8px' } },
|
|
71
56
|
react_1.default.createElement("label", null, "Inspect Hook:"),
|
|
72
|
-
react_1.default.createElement("select", { value:
|
|
57
|
+
react_1.default.createElement("select", { value: selectedInspectorId, onChange: (e) => setSelectedInspectorId(e.target.value), style: { padding: '4px' } }, Object.keys(INSPECTORS).map((key) => (react_1.default.createElement("option", { key: key, value: key }, key)))),
|
|
73
58
|
Overlay && (react_1.default.createElement("div", { style: { marginTop: '4px' } },
|
|
74
59
|
react_1.default.createElement("label", { style: { cursor: 'pointer', display: 'flex', alignItems: 'center' } },
|
|
75
60
|
react_1.default.createElement("input", { type: "checkbox", checked: isOverlayEnabled, onChange: (e) => setIsOverlayEnabled(e.target.checked), style: { marginRight: '4px' } }),
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
interface RechartsDevtoolsContextProps {
|
|
3
|
+
children: React.ReactNode;
|
|
4
|
+
}
|
|
5
|
+
export declare const RechartsDevtoolsContext: React.FC<RechartsDevtoolsContextProps>;
|
|
6
|
+
export declare const useRechartsDevtoolsContext: () => string | null;
|
|
7
|
+
export declare const RechartsDevtoolsPortal: React.FC<React.HTMLAttributes<HTMLDivElement>>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.RechartsDevtoolsPortal = exports.useRechartsDevtoolsContext = exports.RechartsDevtoolsContext = void 0;
|
|
37
|
+
const react_1 = __importStar(require("react"));
|
|
38
|
+
const constants_1 = require("../constants");
|
|
39
|
+
const Context = (0, react_1.createContext)(null);
|
|
40
|
+
const RechartsDevtoolsContext = ({ children }) => {
|
|
41
|
+
// Generate a unique ID for this instance.
|
|
42
|
+
// React.useId returns a string that includes colons, e.g., ":r0:"
|
|
43
|
+
// We sanitize it to make it a valid HTML ID and safer for CSS selectors if needed.
|
|
44
|
+
const rawId = (0, react_1.useId)();
|
|
45
|
+
const portalId = `recharts-devtools-portal-${rawId.replace(/:/g, '')}`;
|
|
46
|
+
return react_1.default.createElement(Context.Provider, { value: portalId }, children);
|
|
47
|
+
};
|
|
48
|
+
exports.RechartsDevtoolsContext = RechartsDevtoolsContext;
|
|
49
|
+
const useRechartsDevtoolsContext = () => {
|
|
50
|
+
return (0, react_1.useContext)(Context);
|
|
51
|
+
};
|
|
52
|
+
exports.useRechartsDevtoolsContext = useRechartsDevtoolsContext;
|
|
53
|
+
const RechartsDevtoolsPortal = (props) => {
|
|
54
|
+
const contextId = (0, exports.useRechartsDevtoolsContext)();
|
|
55
|
+
const id = contextId ?? constants_1.RECHARTS_DEVTOOLS_PORTAL_ID;
|
|
56
|
+
return react_1.default.createElement("div", { id: id, ...props });
|
|
57
|
+
};
|
|
58
|
+
exports.RechartsDevtoolsPortal = RechartsDevtoolsPortal;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This hook works just the same as `useState`, but it also stores the state in the session storage.
|
|
3
|
+
* It uses `useId` from React to generate a unique key for the state.
|
|
4
|
+
*
|
|
5
|
+
* In browsers without session storage support, it will fallback to `useState`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const useSessionStorageState: <T>(initialState: T) => readonly [any, import("react").Dispatch<any>];
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSessionStorageState = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* This hook works just the same as `useState`, but it also stores the state in the session storage.
|
|
7
|
+
* It uses `useId` from React to generate a unique key for the state.
|
|
8
|
+
*
|
|
9
|
+
* In browsers without session storage support, it will fallback to `useState`.
|
|
10
|
+
*/
|
|
11
|
+
const useSessionStorageState = (initialState) => {
|
|
12
|
+
const id = (0, react_1.useId)();
|
|
13
|
+
const [state, setState] = (0, react_1.useState)(() => {
|
|
14
|
+
try {
|
|
15
|
+
const storedState = sessionStorage.getItem(id);
|
|
16
|
+
return storedState ? JSON.parse(storedState) : initialState;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return initialState;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
(0, react_1.useEffect)(() => {
|
|
23
|
+
try {
|
|
24
|
+
sessionStorage.setItem(id, JSON.stringify(state));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
}
|
|
28
|
+
}, [state]);
|
|
29
|
+
return [state, setState];
|
|
30
|
+
};
|
|
31
|
+
exports.useSessionStorageState = useSessionStorageState;
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
|
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./random"), exports);
|
|
18
18
|
__exportStar(require("./generateMockData"), exports);
|
|
19
19
|
__exportStar(require("./components/RechartsDevtools"), exports);
|
|
20
|
+
__exportStar(require("./context/RechartsDevtoolsContext"), exports);
|
|
20
21
|
__exportStar(require("./constants"), exports);
|