@recharts/devtools 0.0.5 → 0.0.6

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.
@@ -1,2 +1,6 @@
1
1
  import React from 'react';
2
+ import { InspectorDef } from '../types.js';
3
+ declare const INSPECTORS: Record<string, InspectorDef>;
4
+ export type InspectorKey = keyof typeof INSPECTORS;
2
5
  export declare const RechartsDevtools: () => React.JSX.Element | null;
6
+ export {};
@@ -21,6 +21,9 @@ const INSPECTORS = {
21
21
  useYAxisDomain: YAxisDomainInspector,
22
22
  useActiveTooltipLabel: ActiveTooltipLabelInspector,
23
23
  };
24
+ const isValidInspectorKey = (key) => {
25
+ return key != null && key in INSPECTORS;
26
+ };
24
27
  function useSelectedInspector() {
25
28
  const [selectedInspectorId, setSelectedInspectorId] = useSessionStorageState('useChartWidth | useChartHeight');
26
29
  const selectedInspector = INSPECTORS[selectedInspectorId];
@@ -32,11 +35,18 @@ function useSelectedInspector() {
32
35
  export const RechartsDevtools = () => {
33
36
  const contextId = useRechartsDevtoolsContext();
34
37
  const portalId = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
38
+ const { selectedInspectorId, setSelectedInspectorId, selectedInspector } = useSelectedInspector();
35
39
  const [container, setContainer] = React.useState(null);
36
40
  React.useEffect(() => {
37
- setContainer(document.getElementById(portalId));
38
- }, [portalId]);
39
- const { selectedInspectorId, setSelectedInspectorId, selectedInspector } = useSelectedInspector();
41
+ const el = document.getElementById(portalId);
42
+ setContainer(el);
43
+ if (el) {
44
+ const initialTab = el.getAttribute('data-initial-tab');
45
+ if (isValidInspectorKey(initialTab)) {
46
+ setSelectedInspectorId(initialTab);
47
+ }
48
+ }
49
+ }, [portalId, setSelectedInspectorId]);
40
50
  const [isOverlayEnabled, setIsOverlayEnabled] = useSessionStorageState(false);
41
51
  if (!container || !selectedInspector) {
42
52
  return null;
@@ -1,8 +1,11 @@
1
1
  import React from 'react';
2
+ import { InspectorKey } from '../components/RechartsDevtools.js';
2
3
  interface RechartsDevtoolsContextProps {
3
4
  children: React.ReactNode;
4
5
  }
5
6
  export declare const RechartsDevtoolsContext: React.FC<RechartsDevtoolsContextProps>;
6
7
  export declare const useRechartsDevtoolsContext: () => string | null;
7
- export declare const RechartsDevtoolsPortal: React.FC<React.HTMLAttributes<HTMLDivElement>>;
8
+ export declare const RechartsDevtoolsPortal: React.FC<React.HTMLAttributes<HTMLDivElement> & {
9
+ initialTab?: InspectorKey;
10
+ }>;
8
11
  export {};
@@ -12,8 +12,8 @@ export const RechartsDevtoolsContext = ({ children }) => {
12
12
  export const useRechartsDevtoolsContext = () => {
13
13
  return useContext(Context);
14
14
  };
15
- export const RechartsDevtoolsPortal = (props) => {
15
+ export const RechartsDevtoolsPortal = ({ initialTab, ...props }) => {
16
16
  const contextId = useRechartsDevtoolsContext();
17
17
  const id = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
18
- return React.createElement("div", { id: id, ...props });
18
+ return React.createElement("div", { id: id, "data-initial-tab": initialTab, ...props });
19
19
  };
@@ -0,0 +1,8 @@
1
+ export type MarketCandle = {
2
+ time: number;
3
+ open: number;
4
+ close: number;
5
+ low: number;
6
+ high: number;
7
+ };
8
+ export declare function generateMockMarketData(length: number, seed: number, startPrice?: number, startTime?: number): MarketCandle[];
@@ -0,0 +1,24 @@
1
+ import { between, random } from './random.js';
2
+ export function generateMockMarketData(length, seed, startPrice = 100, startTime = Date.now()) {
3
+ const gen = random(seed);
4
+ const candles = [];
5
+ const interval = 15 * 60 * 1000; // 15 minutes in ms
6
+ let lastClose = startPrice;
7
+ for (let i = 0; i < length; i++) {
8
+ const time = startTime + i * interval;
9
+ const open = lastClose;
10
+ // small random move for close relative to open (in currency units)
11
+ const move = between(gen, -200, 200) / 100; // -2.00 .. +2.00
12
+ const close = Math.max(0.01, +(open + move));
13
+ // ensure high is at least max(open, close) and low is at most min(open, close)
14
+ const baseHigh = Math.max(open, close);
15
+ const baseLow = Math.min(open, close);
16
+ const extraHigh = between(gen, 0, 100) / 100; // up to +1.00
17
+ const extraLow = between(gen, 0, 100) / 100; // up to -1.00
18
+ const high = +(baseHigh + extraHigh);
19
+ const low = Math.max(0.01, +(baseLow - extraLow));
20
+ candles.push({ time, open, close, low, high });
21
+ lastClose = close;
22
+ }
23
+ return candles;
24
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { between, random } from './random.js';
2
2
  export { generateMockData } from './generateMockData.js';
3
+ export { generateMockMarketData } from './generateMockMarketData.js';
3
4
  export { RechartsDevtools } from './components/RechartsDevtools.js';
4
5
  export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
5
6
  export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { between, random } from './random.js';
2
2
  export { generateMockData } from './generateMockData.js';
3
+ export { generateMockMarketData } from './generateMockMarketData.js';
3
4
  export { RechartsDevtools } from './components/RechartsDevtools.js';
4
5
  export { RechartsDevtoolsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
5
6
  export { RECHARTS_DEVTOOLS_PORTAL_ID } from './constants.js';
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "type": "module",
7
- "version": "0.0.5",
7
+ "version": "0.0.6",
8
8
  "description": "Devtools and helper functions for Recharts",
9
9
  "main": "dist/index.js",
10
10
  "types": "dist/index.d.ts",
@@ -49,4 +49,4 @@
49
49
  "react-dom": "^18.0.0 || ^19.0.0",
50
50
  "recharts": "^3.3.0"
51
51
  }
52
- }
52
+ }