Skip to content

Commit 27dfa81

Browse files
add vanilla hover inspection
1 parent e0c619d commit 27dfa81

1 file changed

Lines changed: 54 additions & 9 deletions

File tree

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,69 @@
1-
import React from "react";
1+
import React, { useEffect, useRef } from "react";
22

33
export function VanillaRunner({
44
width,
55
height,
66
source,
7+
enableInspector = true,
78
}: {
89
width: string;
910
height: string;
1011
source: string;
1112
componentName: string;
13+
enableInspector?: boolean;
1214
}) {
15+
const ref = useRef<HTMLIFrameElement>();
16+
17+
useEffect(() => {
18+
if (ref.current && enableInspector) {
19+
ref.current.onload = () => {
20+
const matches = ref.current.contentDocument.querySelectorAll(
21+
"div, span, button, img, image, svg"
22+
);
23+
matches.forEach((el) => {
24+
const tint = "rgba(20, 0, 255, 0.2)";
25+
const tintl = "rgba(20, 0, 255, 0.5)";
26+
const originstyle = {
27+
//@ts-ignore
28+
...el.style,
29+
};
30+
31+
if (el.id.includes("RootWrapper")) {
32+
} else {
33+
el.addEventListener("mouseenter", (e) => {
34+
//@ts-ignore
35+
e.target.style.background = tint;
36+
//@ts-ignore
37+
e.target.style.color = tint;
38+
//@ts-ignore
39+
e.target.style.outline = `${tintl} solid 1px`;
40+
});
41+
el.addEventListener("mouseleave", (e) => {
42+
//@ts-ignore
43+
e.target.style.background = originstyle.background;
44+
//@ts-ignore
45+
e.target.style.color = originstyle.color;
46+
//@ts-ignore
47+
e.target.style.outline = originstyle.outline;
48+
});
49+
}
50+
});
51+
52+
ref.current.contentWindow.addEventListener("click", (e) => {
53+
console.log("click", e);
54+
});
55+
};
56+
}
57+
}, [ref.current, enableInspector]);
58+
1359
const inlinesource = source || `<div></div>`;
1460
return (
15-
<>
16-
<iframe
17-
sandbox="allow-same-origin"
18-
srcDoc={inlinesource}
19-
width={width}
20-
height={height}
21-
/>
22-
</>
61+
<iframe
62+
ref={ref}
63+
sandbox="allow-same-origin"
64+
srcDoc={inlinesource}
65+
width={width}
66+
height={height}
67+
/>
2368
);
2469
}

0 commit comments

Comments
 (0)