Skip to content

Commit 70787c9

Browse files
authored
Merge pull request #19 from anvilco/pl/update-anvil-embed-frame
Add style prop and add postMessage helper
2 parents 73908a8 + 7210ebd commit 70787c9

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

packages/anvil-embed-frame/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"build:changelog": "yarn auto-changelog",
1212
"pack": "yarn pack",
1313
"version": "yarn build:changelog && git add CHANGELOG.md",
14-
"test": "yarn mocha --config ./test/mocha.js"
14+
"test": "yarn mocha --config ./test/mocha.js",
15+
"watch": "nodemon --watch src --exec 'yarn build'"
1516
},
1617
"repository": {
1718
"type": "git",

packages/anvil-embed-frame/src/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import PropTypes from 'prop-types'
77
* @prop {Function} onEvent
88
* @prop {String} anvilURL
99
* @prop {String} scroll
10+
* @prop {object} style
1011
*/
1112

1213
/**
@@ -28,6 +29,10 @@ class AnvilEmbedFrame extends React.Component {
2829
window.removeEventListener('message', this.handleEvent)
2930
}
3031

32+
postMessage = (message) => {
33+
this.iframeRef.current.contentWindow.postMessage(message, '*')
34+
}
35+
3136
/**
3237
* @param {Object} options
3338
* @param {String} options.origin
@@ -42,14 +47,15 @@ class AnvilEmbedFrame extends React.Component {
4247
}
4348

4449
render () {
45-
const { iframeURL, onEvent, anvilURL, scroll, ...others } = this.props
50+
const { iframeURL, onEvent, anvilURL, scroll, style, ...others } = this.props
4651
return (
4752
<iframe
4853
id="anvil-embed-frame"
4954
name="AnvilEmbedFrame"
5055
{...others} // props above may be overriden
5156
src={iframeURL}
5257
ref={this.iframeRef}
58+
style={style}
5359
>
5460
<p id="anvil-iframe-warning">Your browser does not support iframes.</p>
5561
</iframe>
@@ -68,6 +74,7 @@ AnvilEmbedFrame.propTypes = {
6874
onEvent: PropTypes.func,
6975
anvilURL: PropTypes.string,
7076
scroll: PropTypes.oneOf(['auto', 'smooth']),
77+
style: PropTypes.object,
7178
}
7279

7380
export default AnvilEmbedFrame

packages/anvil-embed-frame/test/src/index.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,20 @@ describe('AnvilEmbedFrame', function () {
4444
expect($.handleEvent).to.have.been.calledWith(data)
4545
})
4646
})
47+
48+
it('calls postMessage successfully', () => {
49+
const wrapper = $.render
50+
const iframe = wrapper.find('iframe')
51+
const postMessage = sinon.spy()
52+
const iframeMock = {
53+
contentWindow: {
54+
postMessage,
55+
},
56+
}
57+
58+
iframe.getElement().ref.current = iframeMock
59+
60+
wrapper.instance().postMessage({ action: 'test' })
61+
expect(postMessage.called).to.be.equal(true)
62+
})
4763
})

packages/anvil-embed-frame/types/index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type Props = {
1313
* @prop {String} anvilURL
1414
* @prop {boolean} enableDefaultStyles
1515
* @prop {String} scroll
16+
* @prop {object} style
1617
*/
1718
/**
1819
* @extends React.Component<Props>
@@ -48,8 +49,9 @@ declare namespace AnvilEmbedFrame {
4849
const enableDefaultStyles_1: PropTypes.Requireable<boolean>;
4950
export { enableDefaultStyles_1 as enableDefaultStyles };
5051
export const scroll: PropTypes.Requireable<string>;
52+
export const style: PropTypes.Requireable<object>;
5153
}
5254
}
5355
import Reacat from "react";
5456
import PropTypes from "prop-types";
55-
//# sourceMappingURL=index.d.ts.map
57+
//# sourceMappingURL=index.d.ts.map

0 commit comments

Comments
 (0)