-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for running an srcDoc style iFrame (#410)
* Add additional props to allow passing in of parent doc and window refs * Add temp log so we can ensure fork running in tests * Revert back to setting parent doc and window internally * Remove unnecessary reference check * An an iframed example to allow testing * Fix typing of iframe example component * Add a test to check that styles are being applied to iframe document
- Loading branch information
1 parent
a46a274
commit 6cc485a
Showing
4 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
describe('Iframed assertions', function () { | ||
beforeEach(function () { | ||
cy.viewport(1000, 600) | ||
cy.visit('/?iframed=true') | ||
}) | ||
|
||
const getIframeBody = () => { | ||
return cy | ||
.get('iframe[data-cy="iframe"]') | ||
.its('0.contentDocument.body') | ||
.should('not.be.empty') | ||
.then(cy.wrap) | ||
} | ||
|
||
it('Display the crop area with correct styles applied to the iframe', () => { | ||
getIframeBody() | ||
.find('.reactEasyCrop_CropArea') | ||
.should('exist') | ||
.should('have.css', 'color') | ||
.and('eq', 'rgba(0, 0, 0, 0.5)') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react' | ||
|
||
import './styles.css' | ||
import { createPortal } from 'react-dom' | ||
|
||
interface Props { | ||
children: React.ReactNode | ||
} | ||
|
||
export default function Iframe({ children }: Props) { | ||
const [iframeBody, setIframeBody] = React.useState<HTMLElement>() | ||
|
||
const iFrameRef = React.useRef<HTMLIFrameElement>(null) | ||
|
||
React.useEffect(() => { | ||
function setDocumentIfReady() { | ||
const { contentDocument } = iFrameRef.current as HTMLIFrameElement | ||
const { readyState, documentElement } = contentDocument as Document | ||
|
||
if (readyState !== 'interactive' && readyState !== 'complete') { | ||
return false | ||
} | ||
|
||
setIframeBody(documentElement.getElementsByTagName('body')[0]) | ||
|
||
return true | ||
} | ||
|
||
// Document set with srcDoc is not immediately ready. | ||
if (iFrameRef.current) { | ||
iFrameRef.current.addEventListener('load', setDocumentIfReady) | ||
} | ||
}, [iFrameRef]) | ||
|
||
return ( | ||
<> | ||
<iframe | ||
style={{ height: '100vh', width: '100vw' }} | ||
ref={iFrameRef} | ||
srcDoc="<!doctype html>" | ||
title="test iframed" | ||
data-cy="iframe" | ||
> | ||
<>{iframeBody && createPortal(children, iframeBody)}</> | ||
</iframe> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters