react-frame-component 5.1.0 → 5.2.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -1
- package/index.d.ts +1 -0
- package/lib/Frame.js +25 -10
- package/lib/index.js +10 -8
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -75,7 +75,32 @@ lifecycle calls. This set of lifecycle calls are sometimes triggered after the
|
|
|
75
75
|
lifecycle of the parent component, so these callbacks provide a hook to know
|
|
76
76
|
when the frame contents are mounted and updated.
|
|
77
77
|
|
|
78
|
-
######
|
|
78
|
+
###### ref
|
|
79
|
+
`ref: PropTypes.oneOfType([ PropTypes.func, PropTypes.shape({ current: PropTypes.instanceOf(Element) }) ])`
|
|
80
|
+
|
|
81
|
+
The `ref` prop provides a way to access inner iframe DOM node. To utilitize this prop use, for example, one of the React's built-in methods to create a ref: [`React.createRef()`](https://reactjs.org/docs/refs-and-the-dom.html#creating-refs) or [`React.useRef()`](https://reactjs.org/docs/hooks-reference.html#useref).
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
const MyComponent = (props) => {
|
|
85
|
+
const iframeRef = React.useRef();
|
|
86
|
+
|
|
87
|
+
React.useEffect(() => {
|
|
88
|
+
// Use iframeRef for:
|
|
89
|
+
// - focus managing
|
|
90
|
+
// - triggering imperative animations
|
|
91
|
+
// - integrating with third-party DOM libraries
|
|
92
|
+
iframeRef.current.focus()
|
|
93
|
+
}, [])
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<Frame ref={iframeRef}>
|
|
97
|
+
<InnerComponent />
|
|
98
|
+
</Frame>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
##### Accessing the iframe's window and document
|
|
79
104
|
The iframe's `window` and `document` may be accessed via the `FrameContextConsumer` or the `useFrame` hook.
|
|
80
105
|
|
|
81
106
|
The example with `FrameContextConsumer`:
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module 'react-frame-component';
|
package/lib/Frame.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.Frame = undefined;
|
|
6
7
|
|
|
7
8
|
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
|
8
9
|
|
|
@@ -34,7 +35,7 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
|
|
|
34
35
|
|
|
35
36
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
36
37
|
|
|
37
|
-
var Frame = function (_Component) {
|
|
38
|
+
var Frame = exports.Frame = function (_Component) {
|
|
38
39
|
_inherits(Frame, _Component);
|
|
39
40
|
|
|
40
41
|
// React warns when you render directly into the body since browser extensions
|
|
@@ -47,14 +48,26 @@ var Frame = function (_Component) {
|
|
|
47
48
|
var _this = _possibleConstructorReturn(this, (Frame.__proto__ || Object.getPrototypeOf(Frame)).call(this, props, context));
|
|
48
49
|
|
|
49
50
|
_this.setRef = function (node) {
|
|
50
|
-
_this.
|
|
51
|
+
_this.nodeRef.current = node;
|
|
52
|
+
|
|
53
|
+
var forwardedRef = _this.props.forwardedRef; // eslint-disable-line react/prop-types
|
|
54
|
+
|
|
55
|
+
if (typeof forwardedRef === 'function') {
|
|
56
|
+
forwardedRef(node);
|
|
57
|
+
} else if (forwardedRef) {
|
|
58
|
+
forwardedRef.current = node;
|
|
59
|
+
}
|
|
51
60
|
};
|
|
52
61
|
|
|
53
62
|
_this.handleLoad = function () {
|
|
54
|
-
|
|
63
|
+
// Bail update as some browsers will trigger on both DOMContentLoaded & onLoad ala firefox
|
|
64
|
+
if (!_this.state.iframeLoaded) {
|
|
65
|
+
_this.setState({ iframeLoaded: true });
|
|
66
|
+
}
|
|
55
67
|
};
|
|
56
68
|
|
|
57
69
|
_this._isMounted = false;
|
|
70
|
+
_this.nodeRef = _react2.default.createRef();
|
|
58
71
|
_this.state = { iframeLoaded: false };
|
|
59
72
|
return _this;
|
|
60
73
|
}
|
|
@@ -65,10 +78,9 @@ var Frame = function (_Component) {
|
|
|
65
78
|
this._isMounted = true;
|
|
66
79
|
|
|
67
80
|
var doc = this.getDoc();
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.node.addEventListener('load', this.handleLoad);
|
|
81
|
+
|
|
82
|
+
if (doc) {
|
|
83
|
+
this.nodeRef.current.contentWindow.addEventListener('DOMContentLoaded', this.handleLoad);
|
|
72
84
|
}
|
|
73
85
|
}
|
|
74
86
|
}, {
|
|
@@ -76,12 +88,12 @@ var Frame = function (_Component) {
|
|
|
76
88
|
value: function componentWillUnmount() {
|
|
77
89
|
this._isMounted = false;
|
|
78
90
|
|
|
79
|
-
this.
|
|
91
|
+
this.nodeRef.current.removeEventListener('load', this.handleLoad);
|
|
80
92
|
}
|
|
81
93
|
}, {
|
|
82
94
|
key: 'getDoc',
|
|
83
95
|
value: function getDoc() {
|
|
84
|
-
return this.
|
|
96
|
+
return this.nodeRef.current ? this.nodeRef.current.contentDocument : null; // eslint-disable-line
|
|
85
97
|
}
|
|
86
98
|
}, {
|
|
87
99
|
key: 'getMountTarget',
|
|
@@ -142,6 +154,7 @@ var Frame = function (_Component) {
|
|
|
142
154
|
delete props.mountTarget;
|
|
143
155
|
delete props.contentDidMount;
|
|
144
156
|
delete props.contentDidUpdate;
|
|
157
|
+
delete props.forwardedRef;
|
|
145
158
|
return _react2.default.createElement(
|
|
146
159
|
'iframe',
|
|
147
160
|
_extends({}, props, { ref: this.setRef, onLoad: this.handleLoad }),
|
|
@@ -171,4 +184,6 @@ Frame.defaultProps = {
|
|
|
171
184
|
contentDidUpdate: function contentDidUpdate() {},
|
|
172
185
|
initialContent: '<!DOCTYPE html><html><head></head><body><div class="frame-root"></div></body></html>'
|
|
173
186
|
};
|
|
174
|
-
exports.default =
|
|
187
|
+
exports.default = _react2.default.forwardRef(function (props, ref) {
|
|
188
|
+
return _react2.default.createElement(Frame, _extends({}, props, { forwardedRef: ref }));
|
|
189
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,15 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
var _Frame = require('./Frame');
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, 'default', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function get() {
|
|
12
|
+
return _interopRequireDefault(_Frame).default;
|
|
13
|
+
}
|
|
14
|
+
});
|
|
7
15
|
|
|
8
16
|
var _Context = require('./Context');
|
|
9
17
|
|
|
@@ -26,10 +34,4 @@ Object.defineProperty(exports, 'useFrame', {
|
|
|
26
34
|
}
|
|
27
35
|
});
|
|
28
36
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var _Frame2 = _interopRequireDefault(_Frame);
|
|
32
|
-
|
|
33
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
34
|
-
|
|
35
|
-
exports.default = _Frame2.default;
|
|
37
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-frame-component",
|
|
3
|
-
"version": "5.1
|
|
3
|
+
"version": "5.2.2-alpha.1",
|
|
4
4
|
"description": "React component to wrap your application or component in an iFrame for encapsulation purposes",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
7
|
-
"lib"
|
|
7
|
+
"lib",
|
|
8
|
+
"index.d.ts"
|
|
8
9
|
],
|
|
9
10
|
"scripts": {
|
|
10
11
|
"precommit": "lint-staged",
|