-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Iframes#978 #984
Iframes#978 #984
Changes from 5 commits
e80f303
d72bfce
7fd27ac
b6ac4d7
a00120f
ad58ed0
c1d3b23
b7936b1
0be2a23
017db42
fe1040e
c2ca756
dab7704
c3fafd2
53cdb7f
3f69856
6d7500f
d86ec50
7515ce6
399640b
1c598ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from "react"; | ||
import isWithinIframe from "../utils/isWithinIframe.js"; | ||
|
||
// This is a wrapper component that works with https://www.npmjs.com/package/iframe-resizer | ||
// to allow the components detect that they are within an iframe who's parent is running iframeresizer | ||
// so they can change styles and act differently | ||
|
||
export default class IframeResizerInParent extends React.Component { | ||
// iframe-resizer will create window.iFrameResizer | ||
// this code adds additional properties to that | ||
|
||
static inParent(){ // true if component is running in an iframe and the parent is running iframe-resizer | ||
return window.iFrameResizer?.inParent; | ||
} | ||
|
||
static onInParent(cb){ // when iframe-resizer is started by the parent, execute the call back so the component can rerender or something | ||
// iframe-resizer will create window.iFrameResize when it runs, but it may not exist yet | ||
if(!window.iFrameResizer) window.iFrameResizer={}; | ||
if(!window.iFrameResizer.onInParent) window.iFrameResizer.onInParent=[]; | ||
window.iFrameResizer.onInParent.push(cb); | ||
} | ||
|
||
static _detected(...args){ | ||
// we are running in an iframe, and the parent has just started iframe-resizer | ||
if(!args[0].data.includes('[iFrameSizer]')) return; | ||
if(!window.iFrameResizer) window.iFrameResizer={}; | ||
window.iFrameResizer.inParent=true; | ||
/*There could be multiple components needing to be re-rendered after iframe resizer starts, each will add a callback | ||
* to the array: onInParent. | ||
* Components may need to rerender after iframeresizer starts because their style needs to change if rendering | ||
* in an iframe that resizes v. rendering in an iframe that doesn't resize. | ||
*/ | ||
let func; | ||
while((func=window.iFrameResizer.onInParent?.shift())) | ||
func(); | ||
window.removeEventListener('message',IframeResizerInParent._detected); | ||
} | ||
|
||
componentDidMount(){ | ||
// if page is not running within an iframe, this component is just a pass through | ||
if (window && document && isWithinIframe()) { | ||
// we are running within an iframe | ||
// allow parent frame to determine background color | ||
document.getElementsByTagName('body')[0].style.backgroundColor='transparent'; | ||
// be prepared to detect IframeResizer has been run in parent window | ||
window.addEventListener('message', IframeResizerInParent._detected); | ||
// add the child side (in the iframe) javascript code to this page -- it's only relevant if running within an iframe | ||
const script = document.createElement('script'); | ||
const body = document.getElementsByTagName('body')[0]; | ||
script.src = '/static/iframeResizer.contentWindow.min.js'; | ||
body.appendChild(script); | ||
} | ||
} | ||
render(): Array<React$Node> { | ||
return this.props.children; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd rename this file as just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// true if component is rendering within an iframe of another web page | ||
export default function isWithinIframe(){ | ||
return window?.parent !== window | ||
} |
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<!-- this file can be loaded directly from a browser and used to test embedding groups and projects within iframes | ||
see https://github.com/DemocracyLab/CivicTechExchange/issues/978 | ||
change groups/inframe to projects/inframe below to test embedding a project | ||
This if for testing with iframe resizer | ||
--> | ||
<!DOCTYPE html> | ||
<html lang="en" style="height: 100%"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
PeterBreen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<title>iframe test</title> | ||
</head> | ||
<body> | ||
<div style="width: 100vw;"> | ||
<h1>This is a landing page with Group projects:</h1> | ||
<style> | ||
iframe { | ||
width: 1px; | ||
min-width: 100%; | ||
PeterBreen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
</style> | ||
<script src="http://localhost:8000/static/iframeResizer.min.js"></script> | ||
<iframe id="democracylab-groups" | ||
src="http://localhost:8000/groups/inframe/1" | ||
></iframe> | ||
<h1>This is below the group</h1> | ||
<h1>This is a project</h1> | ||
<iframe id="democracylab-project" | ||
src="http://localhost:8000/projects/inframe/1" | ||
></iframe> | ||
<h1>This is below the project. </h1> | ||
<script> | ||
iFrameResize({ log: true }, '#democracylab-groups,#democracylab-project') | ||
</script> | ||
</div> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!-- this file can be loaded directly from a browser and used to test embedding groups and projects within iframes | ||
see https://github.com/DemocracyLab/CivicTechExchange/issues/978 | ||
change groups/inframe to projects/inframe below to test embedding a project | ||
This if for testing without using iframe resizer | ||
--> | ||
<!DOCTYPE html> | ||
<html lang="en" style="height: 100%"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
PeterBreen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<title>iframe test</title> | ||
</head> | ||
<body> | ||
<div style="width: 100vw;"> | ||
PeterBreen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<h1>This is a landing page with Group projects:</h1> | ||
<!-- see https://www.w3schools.com/howto/howto_css_responsive_iframes.asp --> | ||
<div style="position: relative; overflow: hidden; width: auto; padding-top: 40%; margin-left: 5rem; margin-right: 5rem; border-radius:1rem; border: 1px solid black;"> | ||
<iframe | ||
src="http://localhost:8000/groups/inframe/1" | ||
style="position: absolute; top: 0; left: 0; bottom: 0; right: 0; width: 100%; height: 100%; border: none; border-radius: 1rem;" | ||
></iframe> | ||
</div> | ||
<h1>This is the footer below the projects</h1> | ||
<h1>This is a project</h1> | ||
<div style="position: relative; overflow: hidden; width: auto; padding-top: 40%; margin-left: 5rem; margin-right: 5rem; border-radius:1rem; border: 1px solid black;"> | ||
<iframe | ||
src="http://localhost:8000/projects/inframe/1" | ||
style="position: absolute; top: 0; left: 0; bottom: 0; right: 0; width: 100%; height: 100%; border: none; border-radius: 1rem;" | ||
></iframe> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This statement is used a couple times, so should live in a helper method. Can also use helper methods to implement the logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the change to urlHelper both in this file and in ProjectCard. - but the code is different between the two and a common helper method wasn't possible. Plus both places read well with the urlHelper change. The ProjectCard code looks like this now: