Skip to content

Commit b20eef0

Browse files
authored
Merge pull request #2685 from PolicyEngine/MaxGhenis/issue2676
Allow clipboard access for OBBBA iframe
2 parents 70b0dc0 + cc1d6cd commit b20eef0

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

src/applets/OBBBAHouseholdExplorer.jsx

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,52 @@ import Header from "../layout/Header";
22
import { Helmet } from "react-helmet";
33
import style from "../style";
44
import { useWindowHeight } from "../hooks/useWindow";
5+
import { useEffect, useRef, useMemo } from "react";
6+
import { useNavigate, useLocation } from "react-router-dom";
57

68
export default function OBBBAHouseholdExplorer() {
79
const windowHeight = useWindowHeight();
10+
const navigate = useNavigate();
11+
const location = useLocation();
12+
const iframeRef = useRef(null);
13+
14+
// Memoize baseUrl to prevent unnecessary re-renders
15+
const baseUrl = useMemo(
16+
() =>
17+
process.env.REACT_APP_OBBBA_IFRAME_URL ||
18+
"https://policyengine.github.io/obbba-scatter",
19+
[],
20+
);
21+
22+
// Memoize iframe origin for efficient message verification
23+
const iframeOrigin = useMemo(() => new URL(baseUrl).origin, [baseUrl]);
24+
25+
// Get current URL parameters to forward to iframe using location hook
26+
const urlParams = new URLSearchParams(location.search);
27+
28+
// Construct iframe URL with forwarded parameters
29+
const iframeUrl = urlParams.toString()
30+
? `${baseUrl}?${urlParams.toString()}`
31+
: baseUrl;
32+
33+
useEffect(() => {
34+
// Listen for messages from the iframe
35+
const handleMessage = (event) => {
36+
// Verify the message is from our iframe
37+
if (event.origin !== iframeOrigin) return;
38+
39+
// Handle URL update messages from the iframe
40+
if (event.data?.type === "urlUpdate" && event.data?.params) {
41+
const newParams = new URLSearchParams(event.data.params);
42+
navigate(`${location.pathname}?${newParams.toString()}`, {
43+
replace: true,
44+
});
45+
}
46+
};
47+
48+
window.addEventListener("message", handleMessage);
49+
return () => window.removeEventListener("message", handleMessage);
50+
}, [navigate, location.pathname, iframeOrigin]);
851

952
return (
1053
<>
@@ -21,14 +64,13 @@ export default function OBBBAHouseholdExplorer() {
2164
}}
2265
>
2366
<iframe
24-
src={
25-
process.env.REACT_APP_OBBBA_IFRAME_URL ||
26-
"https://policyengine.github.io/obbba-scatter"
27-
}
67+
ref={iframeRef}
68+
src={iframeUrl}
2869
title="OBBBA Household Explorer"
2970
height={`calc(100vh - ${style.spacing.HEADER_HEIGHT})`}
3071
width="100%"
3172
style={{ overflow: "hidden" }}
73+
allow="clipboard-write"
3274
/>
3375
</div>
3476
</>

0 commit comments

Comments
 (0)