react-frame-component 5.0.1 → 5.2.2-alpha.0

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 CHANGED
@@ -75,8 +75,35 @@ 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
- ###### Accessing the iframe's window and document
79
- The iframe's `window` and `document` may be accessed via the FrameContextConsumer.
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
104
+ The iframe's `window` and `document` may be accessed via the `FrameContextConsumer` or the `useFrame` hook.
105
+
106
+ The example with `FrameContextConsumer`:
80
107
 
81
108
  ```js
82
109
  import Frame, { FrameContextConsumer } from 'react-frame-component'
@@ -96,6 +123,25 @@ const MyComponent = (props, context) => (
96
123
 
97
124
  ```
98
125
 
126
+ The example with `useFrame` hook:
127
+
128
+ ```js
129
+ import Frame, { useFrame } from 'react-frame-component';
130
+
131
+ const InnerComponent = () => {
132
+ // Hook returns iframe's window and document instances from Frame context
133
+ const { document, window } = useFrame();
134
+
135
+ return null;
136
+ };
137
+
138
+ const OuterComponent = () => (
139
+ <Frame>
140
+ <InnerComponent />
141
+ </Frame>
142
+ );
143
+ ```
144
+
99
145
  ## More info
100
146
 
101
147
  I wrote a [blog post][blog-url] about building this component.
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ declare module 'react-frame-component';
package/lib/Context.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.FrameContextConsumer = exports.FrameContextProvider = exports.FrameContext = undefined;
6
+ exports.FrameContextConsumer = exports.FrameContextProvider = exports.useFrame = exports.FrameContext = undefined;
7
7
 
8
8
  var _react = require('react');
9
9
 
@@ -22,6 +22,10 @@ if (typeof window !== 'undefined') {
22
22
 
23
23
  var FrameContext = exports.FrameContext = _react2.default.createContext({ document: doc, window: win });
24
24
 
25
+ var useFrame = exports.useFrame = function useFrame() {
26
+ return _react2.default.useContext(FrameContext);
27
+ };
28
+
25
29
  var FrameContextProvider = FrameContext.Provider,
26
30
  FrameContextConsumer = FrameContext.Consumer;
27
31
  exports.FrameContextProvider = FrameContextProvider;
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,7 +48,15 @@ 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.node = node;
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 () {
@@ -55,6 +64,7 @@ var Frame = function (_Component) {
55
64
  };
56
65
 
57
66
  _this._isMounted = false;
67
+ _this.nodeRef = _react2.default.createRef();
58
68
  _this.state = { iframeLoaded: false };
59
69
  return _this;
60
70
  }
@@ -67,8 +77,10 @@ var Frame = function (_Component) {
67
77
  var doc = this.getDoc();
68
78
  if (doc && doc.readyState === 'complete') {
69
79
  this.forceUpdate();
70
- } else {
71
- this.node.addEventListener('load', this.handleLoad);
80
+ }
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.node.removeEventListener('load', this.handleLoad);
91
+ this.nodeRef.current.removeEventListener('load', this.handleLoad);
80
92
  }
81
93
  }, {
82
94
  key: 'getDoc',
83
95
  value: function getDoc() {
84
- return this.node ? this.node.contentDocument : null; // eslint-disable-line
96
+ return this.nodeRef.current ? this.nodeRef.current.contentDocument : null; // eslint-disable-line
85
97
  }
86
98
  }, {
87
99
  key: 'getMountTarget',
@@ -142,9 +154,10 @@ 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
- _extends({}, props, { ref: this.setRef, onLoad: this.handleLoad }),
160
+ _extends({}, props, { ref: this.setRef }),
148
161
  this.state.iframeLoaded && this.renderFrameContents()
149
162
  );
150
163
  }
@@ -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 = Frame;
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
- exports.FrameContextConsumer = exports.FrameContext = undefined;
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
 
@@ -19,11 +27,11 @@ Object.defineProperty(exports, 'FrameContextConsumer', {
19
27
  return _Context.FrameContextConsumer;
20
28
  }
21
29
  });
30
+ Object.defineProperty(exports, 'useFrame', {
31
+ enumerable: true,
32
+ get: function get() {
33
+ return _Context.useFrame;
34
+ }
35
+ });
22
36
 
23
- var _Frame = require('./Frame');
24
-
25
- var _Frame2 = _interopRequireDefault(_Frame);
26
-
27
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
28
-
29
- 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.0.1",
3
+ "version": "5.2.2-alpha.0",
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",