Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Prevent hash links from navigating away in iframe preview

Claude2fd991d87e30
diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsx
index b9bc2561..f01d29bf 100644
--- a/frontend/src/components/preview/PreviewComponent.tsx
+++ b/frontend/src/components/preview/PreviewComponent.tsx
@@ -35,6 +35,15 @@ function PreviewComponent({
setClickEvent(event);
}, []);
+ const handleIframeLinkClick = useCallback((event: MouseEvent) => {
+ const target = (event.target as HTMLElement).closest("a");
+ if (!target) return;
+ const href = target.getAttribute("href");
+ if (href && href.startsWith("#")) {
+ event.preventDefault();
+ }
+ }, []);
+
// Apply a fixed viewport per device and scale to fit the available pane.
useEffect(() => {
const updateScale = () => {
@@ -90,6 +99,7 @@ function PreviewComponent({
const body = iframe.contentWindow?.document.body;
if (!body) return;
body.addEventListener("click", handleIframeClick);
+ body.addEventListener("click", handleIframeLinkClick);
};
iframe.addEventListener("load", handleLoad);
@@ -99,9 +109,10 @@ function PreviewComponent({
const body = iframe.contentWindow?.document.body;
if (body) {
body.removeEventListener("click", handleIframeClick);
+ body.removeEventListener("click", handleIframeLinkClick);
}
};
- }, [handleIframeClick]);
+ }, [handleIframeClick, handleIframeLinkClick]);
useEffect(() => {
const iframe = iframeRef.current;