-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathindex.tsx
More file actions
89 lines (83 loc) · 2.2 KB
/
index.tsx
File metadata and controls
89 lines (83 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { useLayoutEffect, useRef, useState } from "preact/hooks";
export const ScalingIframe = ({
width,
...props
}: {
width: number;
[key: string]: any;
}) => {
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const [scale, setScale] = useState(1);
const [error, setError] = useState(false);
useLayoutEffect(() => {
const fitToParent = () => {
const iframe = iframeRef.current;
// Two parents up to get out of the astro island
const parent = iframe?.parentElement?.parentElement;
if (!iframe || !parent) return;
const parentWidth = parent.getBoundingClientRect().width;
setScale(parentWidth / width);
};
fitToParent();
window.addEventListener("resize", fitToParent);
return () => window.removeEventListener("resize", fitToParent);
}, [width]);
const handleError = () => {
setError(true);
};
if (error) {
return (
<div
style={{
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#f0f0f0",
color: "#333",
fontSize: "16px",
textAlign: "center",
padding: "20px",
}}
>
<div>
<p>Unable to load embedded content</p>
<p style={{ fontSize: "14px", marginTop: "10px" }}>
Please visit{" "}
<a
href={props.src?.replace("/embed", "")}
target="_blank"
rel="noopener noreferrer"
style={{ color: "#d63384" }}
>
the original sketch
</a>
{" "}on OpenProcessing
</p>
</div>
</div>
);
}
return (
<iframe
width={width}
{...props}
sandbox="allow-scripts allow-popups allow-modals allow-forms allow-same-origin"
allow="fullscreen; clipboard-write"
loading="lazy"
onError={handleError}
style={{
position: "absolute",
top: 0,
left: 0,
transform: `scale(${scale.toFixed(4)})`,
transformOrigin: "top left",
}}
ref={iframeRef}
/>
);
};