@zargaryanvh/react-component-inspector 1.0.0 → 1.0.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.
@@ -1,5 +1,5 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
- import React, { createContext, useContext, useState, useCallback, useEffect } from "react";
2
+ import React, { createContext, useContext, useState, useCallback, useEffect, useRef } from "react";
3
3
  import { setInspectionActive } from "./inspectionInterceptors";
4
4
  import { setupAutoInspection } from "./autoInspection";
5
5
  const InspectionContext = createContext(undefined);
@@ -11,34 +11,47 @@ export const InspectionProvider = ({ children }) => {
11
11
  const [isLocked, setIsLocked] = useState(false);
12
12
  const [hoveredComponent, setHoveredComponentState] = useState(null);
13
13
  const [hoveredElement, setHoveredElement] = useState(null);
14
+ // Use refs to always access latest state values in event handlers
15
+ const isInspectionActiveRef = useRef(isInspectionActive);
16
+ const isLockedRef = useRef(isLocked);
17
+ const hoveredComponentRef = useRef(hoveredComponent);
18
+ const hKeyPressedRef = useRef(false); // Track if H key is currently being held
19
+ // Keep refs in sync with state
20
+ useEffect(() => {
21
+ isInspectionActiveRef.current = isInspectionActive;
22
+ }, [isInspectionActive]);
23
+ useEffect(() => {
24
+ isLockedRef.current = isLocked;
25
+ }, [isLocked]);
26
+ useEffect(() => {
27
+ hoveredComponentRef.current = hoveredComponent;
28
+ }, [hoveredComponent]);
14
29
  // Track CTRL key state and CTRL+H for locking
15
30
  React.useEffect(() => {
16
31
  if (process.env.NODE_ENV !== "development") {
17
32
  return; // Only in development
18
33
  }
19
34
  const handleKeyDown = (e) => {
20
- // H key pressed (with or without CTRL) to lock tooltip position while hovering
21
- if (e.key.toLowerCase() === "h") {
22
- // Only lock if inspection is active, we have a hovered component, and it's not already locked
23
- if (isInspectionActive && hoveredComponent && !isLocked) {
24
- e.preventDefault();
25
- setIsLocked(true);
26
- if (process.env.NODE_ENV === "development") {
27
- console.log("[Inspection] Tooltip locked - position fixed. Press H again or release CTRL to unlock.");
28
- }
35
+ // H key pressed while CTRL is held - lock tooltip position
36
+ if (e.key.toLowerCase() === "h" && e.ctrlKey) {
37
+ e.preventDefault();
38
+ e.stopPropagation();
39
+ // Ignore repeated keydown events (when key is held down)
40
+ if (e.repeat) {
41
+ return;
29
42
  }
30
- // If already locked and H is pressed again, unlock (toggle behavior)
31
- else if (isLocked && isInspectionActive) {
32
- e.preventDefault();
33
- setIsLocked(false);
43
+ // Only lock if inspection is active, we have a hovered component, and H is not already being held
44
+ if (isInspectionActiveRef.current && hoveredComponentRef.current && !hKeyPressedRef.current) {
45
+ hKeyPressedRef.current = true;
46
+ setIsLocked(true);
34
47
  if (process.env.NODE_ENV === "development") {
35
- console.log("[Inspection] Tooltip unlocked - inspection continues while CTRL is held.");
48
+ console.log("[Inspection] Tooltip LOCKED - Hold H to keep locked. Release H to unlock.");
36
49
  }
37
50
  }
38
51
  return;
39
52
  }
40
53
  // CTRL key pressed
41
- if (e.key === "Control" || e.ctrlKey) {
54
+ if (e.key === "Control" && !e.repeat) {
42
55
  setIsInspectionActive(true);
43
56
  setInspectionActive(true);
44
57
  if (process.env.NODE_ENV === "development") {
@@ -47,10 +60,29 @@ export const InspectionProvider = ({ children }) => {
47
60
  }
48
61
  };
49
62
  const handleKeyUp = (e) => {
63
+ // H key released - unlock tooltip but keep inspection active if CTRL is still held
64
+ if (e.key.toLowerCase() === "h") {
65
+ e.preventDefault();
66
+ e.stopPropagation();
67
+ // Only process if H was actually being held
68
+ if (hKeyPressedRef.current) {
69
+ const wasLocked = isLockedRef.current;
70
+ hKeyPressedRef.current = false;
71
+ // Only unlock if we were locked and CTRL is still held
72
+ if (wasLocked && e.ctrlKey && isInspectionActiveRef.current) {
73
+ setIsLocked(false);
74
+ if (process.env.NODE_ENV === "development") {
75
+ console.log("[Inspection] Tooltip UNLOCKED - inspection continues while CTRL is held.");
76
+ }
77
+ }
78
+ }
79
+ return;
80
+ }
50
81
  // CTRL key released - unlock and clear
51
- if (e.key === "Control" || (!e.ctrlKey && e.key !== "h")) {
82
+ if (e.key === "Control") {
52
83
  setIsInspectionActive(false);
53
84
  setIsLocked(false);
85
+ hKeyPressedRef.current = false;
54
86
  setInspectionActive(false);
55
87
  setHoveredComponentState(null);
56
88
  setHoveredElement(null);
@@ -58,7 +90,6 @@ export const InspectionProvider = ({ children }) => {
58
90
  console.log("[Inspection] Deactivated");
59
91
  }
60
92
  }
61
- // Note: H key release doesn't unlock anymore - use H key press to toggle lock state
62
93
  };
63
94
  window.addEventListener("keydown", handleKeyDown);
64
95
  window.addEventListener("keyup", handleKeyUp);
@@ -66,7 +97,7 @@ export const InspectionProvider = ({ children }) => {
66
97
  window.removeEventListener("keydown", handleKeyDown);
67
98
  window.removeEventListener("keyup", handleKeyUp);
68
99
  };
69
- }, [isInspectionActive, hoveredComponent, isLocked]);
100
+ }, []); // Empty deps - refs handle state access
70
101
  const setHoveredComponent = useCallback((component, element) => {
71
102
  if (process.env.NODE_ENV !== "development") {
72
103
  return; // Only in development
@@ -224,7 +224,7 @@ export const InspectionTooltip = () => {
224
224
  backdropFilter: "blur(8px)",
225
225
  border: "1px solid rgba(255, 255, 255, 0.1)",
226
226
  transition: stablePosition ? "none" : "left 0.1s ease-out, top 0.1s ease-out",
227
- }, children: [_jsxs(Box, { sx: { display: "flex", justifyContent: "space-between", alignItems: "flex-start", mb: 1 }, children: [_jsxs(Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: [_jsx(Typography, { variant: "subtitle2", sx: { color: "#fff", fontWeight: 600, fontSize: "0.875rem" }, children: "Component Inspector" }), isLocked && (_jsx(Typography, { variant: "caption", sx: { color: "#4caf50", fontSize: "0.7rem", fontStyle: "italic" }, children: "(Locked - Release CTRL to unlock)" }))] }), _jsx(MuiTooltip, { title: copied ? "Copied!" : "Copy metadata", children: _jsx(IconButton, { size: "small", onClick: handleCopy, sx: {
227
+ }, children: [_jsxs(Box, { sx: { display: "flex", justifyContent: "space-between", alignItems: "flex-start", mb: 1 }, children: [_jsxs(Box, { sx: { display: "flex", alignItems: "center", gap: 1 }, children: [_jsx(Typography, { variant: "subtitle2", sx: { color: "#fff", fontWeight: 600, fontSize: "0.875rem" }, children: "Component Inspector" }), isLocked && (_jsx(Typography, { variant: "caption", sx: { color: "#4caf50", fontSize: "0.7rem", fontStyle: "italic" }, children: "(Locked - Release H to unlock)" }))] }), _jsx(MuiTooltip, { title: copied ? "Copied!" : "Copy metadata", children: _jsx(IconButton, { size: "small", onClick: handleCopy, sx: {
228
228
  color: copied ? "#4caf50" : "#fff",
229
229
  "&:hover": { backgroundColor: "rgba(255, 255, 255, 0.1)" },
230
230
  p: 0.5,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zargaryanvh/react-component-inspector",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A development tool for inspecting React components with AI-friendly metadata extraction",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",