|
1 | | -import React from "react"; |
| 1 | +import React, { useEffect, useRef } from "react"; |
2 | 2 |
|
3 | 3 | export function VanillaRunner({ |
4 | 4 | width, |
5 | 5 | height, |
6 | 6 | source, |
| 7 | + enableInspector = true, |
7 | 8 | }: { |
8 | 9 | width: string; |
9 | 10 | height: string; |
10 | 11 | source: string; |
11 | 12 | componentName: string; |
| 13 | + enableInspector?: boolean; |
12 | 14 | }) { |
| 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 | + |
13 | 59 | const inlinesource = source || `<div></div>`; |
14 | 60 | 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 | + /> |
23 | 68 | ); |
24 | 69 | } |
0 commit comments