Skip to content

Commit 47b2b79

Browse files
authored
Merge pull request #32 from vev-design/e-397-embed-anything-receives-events-from-other-instances
Make sure EmbedAnything receives only its own events
2 parents 31bd3cb + 5a38f00 commit 47b2b79

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

embed-anything/src/EmbedAnything.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useRef, useState } from 'react';
22
import styles from './EmbedAnything.module.css';
3-
import { registerVevComponent, useEditorState, useVisible } from '@vev/react';
3+
import { registerVevComponent, useEditorState, useModel, useVisible } from '@vev/react';
44

55
type Props = {
66
html: string;
@@ -43,8 +43,9 @@ function EmbedAnything({
4343
);
4444
}
4545

46-
if (isStatic) return <StaticHTML html={html} allowScroll={allowScroll} />;
4746
if (encapsulate) return <EmbedIframe html={html} />;
47+
if (isStatic) return <StaticHTML html={html} allowScroll={allowScroll} />;
48+
4849
return (
4950
<EmbedScript
5051
html={html}
@@ -56,12 +57,13 @@ function EmbedAnything({
5657
}
5758

5859
function EmbedIframe({ html }: { html: string }) {
60+
const { key: messageFrom } = useModel() || { key: 'none' };
5961
const iframeRef = useRef(null);
6062
const [iframeHeight, setIframeHeight] = useState('auto');
6163

6264
useEffect(() => {
6365
function handleIframeMessage(event) {
64-
if (event.data.iframeHeight) {
66+
if (event.data.iframeHeight && event.data.messageFrom === messageFrom) {
6567
setIframeHeight(`${event.data.iframeHeight}px`);
6668
}
6769
}
@@ -99,7 +101,7 @@ function EmbedIframe({ html }: { html: string }) {
99101
const height = document.body.scrollHeight;
100102
if(prevHeight !== height){
101103
prevHeight = height;
102-
window.parent.postMessage({ iframeHeight: height }, '*');
104+
window.parent.postMessage({ iframeHeight: height, messageFrom: '${messageFrom}' }, '*');
103105
}
104106
}
105107
@@ -221,6 +223,9 @@ registerVevComponent(EmbedAnything, {
221223
type: 'boolean',
222224
description: 'Static HTML (mounted directly)',
223225
initialValue: false,
226+
hidden: (context) => {
227+
return context.value.encapsulate;
228+
},
224229
},
225230
{
226231
title: 'Allow inner scroll',

0 commit comments

Comments
 (0)