@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 CHANGED
@@ -1,39 +1,66 @@
1
1
  # @recharts/devtools
2
2
 
3
- Shared helper functions and devtools for Recharts projects.
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
- ```typescript
16
- import { random, generateMockData } from '@recharts/devtools';
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
- const gen = random(123); // seeded random generator
19
- const data = generateMockData(10, 123); // generates 10 mock data points
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
- ## Development
37
+ ### Multiple Instances (Context Mode)
23
38
 
24
- - `npm run build`: Build the package
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
- ## Publishing
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
- To publish a new version:
45
+ ```tsx
46
+ import { RechartsDevtools, RechartsDevtoolsContext, RechartsDevtoolsPortal } from '@recharts/devtools';
47
+ import { AreaChart, LineChart } from 'recharts';
31
48
 
32
- 1. Update the version in `package.json` (e.g., `1.0.1`).
33
- 2. Commit the change.
34
- 3. Create a git tag matching the version:
35
- ```bash
36
- git tag v1.0.1
37
- git push origin v1.0.1
38
- ```
39
- 4. The GitHub Action `Publish Package` will automatically publish to NPM.
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 __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
- })();
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 = __importStar(require("react"));
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 container = document.getElementById(constants_1.RECHARTS_DEVTOOLS_PORTAL_ID);
60
- const [selectedInspector, setSelectedInspector] = (0, react_1.useState)('useChartWidth | useChartHeight');
61
- const [isOverlayEnabled, setIsOverlayEnabled] = (0, react_1.useState)(true);
62
- if (!container) {
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 } = INSPECTORS[selectedInspector];
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: selectedInspector, onChange: (e) => setSelectedInspector(e.target.value), style: { padding: '4px' } }, Object.keys(INSPECTORS).map((key) => (react_1.default.createElement("option", { key: key, value: key }, key)))),
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;
@@ -1,4 +1,5 @@
1
1
  export * from './random';
2
2
  export * from './generateMockData';
3
3
  export * from './components/RechartsDevtools';
4
+ export * from './context/RechartsDevtoolsContext';
4
5
  export * from './constants';
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);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.0.3",
6
+ "version": "0.0.4",
7
7
  "description": "Devtools and helper functions for Recharts",
8
8
  "main": "dist/src/index.js",
9
9
  "types": "dist/src/index.d.ts",