Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make iframe resize according to oembed height and width #1491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/Learningpath/LearningpathEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ interface StyledIframeContainerProps {
}
const StyledIframeContainer = styled.div<StyledIframeContainerProps>`
margin-bottom: ${spacing.normal};
max-width: 720px;
margin-left: auto;
margin-right: auto;
& > iframe {
padding-top: 1em;
border: 0 none;
Expand Down Expand Up @@ -167,7 +170,7 @@ const LearningpathEmbed = ({
oembedWidth={oembed.width}
oembedHeight={oembed.height}
>
<LearningpathIframe html={oembed.html} url={embedUrl.url} />
<LearningpathIframe html={oembed.html} />
</StyledIframeContainer>
);
}
Expand Down
84 changes: 10 additions & 74 deletions src/components/Learningpath/LearningpathIframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { MutableRefObject, useEffect, useRef, useState } from 'react';
import { useEffect, useRef } from 'react';
import parse from 'html-react-parser';

export const urlIsNDLAApiUrl = (url: string) =>
Expand All @@ -20,86 +20,22 @@ export const urlIsNDLAUrl = (url: string) =>

interface Props {
html: string;
url: string;
}

const LearningpathIframe = ({ html, url }: Props) => {
const iframeRef = useRef() as MutableRefObject<HTMLInputElement>;
const [listeningToMessages, setListeningToMessages] = useState(true);

const handleIframeResizing = (url: string) => {
if (urlIsNDLAUrl(url)) {
enableIframeMessageListener();
} else {
disableIframeMessageListener();
}
};
const LearningpathIframe = ({ html }: Props) => {
const iframeWrapperRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
handleIframeResizing(url);
});

const getIframeDOM = () => {
return iframeRef.current?.children[0] as HTMLIFrameElement;
};

const enableIframeMessageListener = () => {
window.addEventListener('message', handleIframeMessages);
setListeningToMessages(true);
};

const disableIframeMessageListener = () => {
window.removeEventListener('message', handleIframeMessages);
setListeningToMessages(false);
};

const handleScrollTo = (evt: MessageEvent) => {
const iframe = getIframeDOM();
if (iframe) {
const rect = iframe.getBoundingClientRect();
const scrollTop =
window.pageYOffset || document.documentElement.scrollTop;

const top = evt.data.top + rect.top + scrollTop;
window.scroll({ top });
}
};

const handleResize = (evt: MessageEvent) => {
if (!evt.data.height) {
return;
}
const iframe = getIframeDOM();
if (iframe) {
const newHeight = parseInt(evt.data.height, 10);
iframe.style.height = `${newHeight}px`; // eslint-disable-line no-param-reassign
}
};

const handleIframeMessages = (event: MessageEvent) => {
const iframe = getIframeDOM();
/* Needed to enforce content to stay within iframe on Safari iOS */
const iframe = iframeWrapperRef.current?.querySelector('iframe');
if (iframe) {
iframe.setAttribute('scrolling', 'no');
}

if (!listeningToMessages || !event || !event.data) {
return;
}

switch (event.data.event) {
case 'resize':
handleResize(event);
break;
case 'scrollTo':
handleScrollTo(event);
break;
default:
break;
const [width, height] = [parseInt(iframe.width), parseInt(iframe.height)];
iframe.style.aspectRatio = `${width ? width : 16}/${height ? height : 9}`;
iframe.width = '';
iframe.height = '';
}
};
}, []);

return <div ref={iframeRef}>{parse(html)}</div>;
return <div ref={iframeWrapperRef}>{parse(html)}</div>;
};

export default LearningpathIframe;