Skip to content

Commit fa2d092

Browse files
committed
convert to hooks api
1 parent c445f87 commit fa2d092

File tree

15 files changed

+3692
-2975
lines changed

15 files changed

+3692
-2975
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["es2015", "react"]
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
33
}

demo/src/App.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

dist/index.js

Lines changed: 26 additions & 265 deletions
Original file line numberDiff line numberDiff line change
@@ -3,276 +3,37 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.Detector = exports.Offline = exports.Online = undefined;
7-
8-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9-
10-
var _react = require("react");
11-
12-
var _propTypes = require("prop-types");
13-
14-
var _propTypes2 = _interopRequireDefault(_propTypes);
15-
16-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17-
18-
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
19-
20-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21-
22-
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
23-
24-
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; }
25-
26-
var inBrowser = typeof navigator !== "undefined";
27-
28-
// these browsers don't fully support navigator.onLine, so we need to use a polling backup
29-
var unsupportedUserAgentsPattern = /Windows.*Chrome|Windows.*Firefox|Linux.*Chrome/;
30-
31-
var ping = function ping(_ref) {
32-
var url = _ref.url,
33-
timeout = _ref.timeout;
34-
35-
return new Promise(function (resolve) {
36-
var isOnline = function isOnline() {
37-
return resolve(true);
38-
};
39-
var isOffline = function isOffline() {
40-
return resolve(false);
41-
};
42-
43-
var xhr = new XMLHttpRequest();
44-
45-
xhr.onerror = isOffline;
46-
xhr.ontimeout = isOffline;
47-
xhr.onreadystatechange = function () {
48-
if (xhr.readyState === xhr.HEADERS_RECEIVED) {
49-
if (xhr.status) {
50-
isOnline();
51-
} else {
52-
isOffline();
53-
}
54-
}
55-
};
56-
57-
xhr.open("HEAD", url);
58-
xhr.timeout = timeout;
59-
xhr.send();
60-
});
61-
};
62-
63-
var propTypes = {
64-
children: _propTypes2.default.node,
65-
onChange: _propTypes2.default.func,
66-
polling: _propTypes2.default.oneOfType([_propTypes2.default.shape({
67-
url: _propTypes2.default.string,
68-
interval: _propTypes2.default.number,
69-
timeout: _propTypes2.default.number
70-
}), _propTypes2.default.bool]),
71-
wrapperType: _propTypes2.default.string
72-
};
73-
74-
var defaultProps = {
75-
polling: true,
76-
wrapperType: "span"
77-
};
78-
79-
var defaultPollingConfig = {
80-
enabled: inBrowser && unsupportedUserAgentsPattern.test(navigator.userAgent),
81-
url: "https://ipv4.icanhazip.com/",
82-
timeout: 5000,
83-
interval: 5000
84-
};
85-
86-
// base class that detects offline/online changes
87-
88-
var Base = function (_Component) {
89-
_inherits(Base, _Component);
90-
91-
function Base() {
92-
_classCallCheck(this, Base);
93-
94-
var _this = _possibleConstructorReturn(this, (Base.__proto__ || Object.getPrototypeOf(Base)).call(this));
95-
96-
_this.state = {
97-
online: inBrowser && typeof navigator.onLine === "boolean" ? navigator.onLine : true
98-
};
99-
// bind event handlers
100-
_this.goOnline = _this.goOnline.bind(_this);
101-
_this.goOffline = _this.goOffline.bind(_this);
102-
return _this;
6+
Object.defineProperty(exports, "Online", {
7+
enumerable: true,
8+
get: function get() {
9+
return _Online["default"];
10310
}
104-
105-
_createClass(Base, [{
106-
key: "componentDidMount",
107-
value: function componentDidMount() {
108-
window.addEventListener("online", this.goOnline);
109-
window.addEventListener("offline", this.goOffline);
110-
111-
if (this.getPollingConfig().enabled) {
112-
this.startPolling();
113-
}
114-
}
115-
}, {
116-
key: "componentWillUnmount",
117-
value: function componentWillUnmount() {
118-
window.removeEventListener("online", this.goOnline);
119-
window.removeEventListener("offline", this.goOffline);
120-
121-
if (this.pollingId) {
122-
this.stopPolling();
123-
}
124-
}
125-
}, {
126-
key: "renderChildren",
127-
value: function renderChildren() {
128-
var _props = this.props,
129-
children = _props.children,
130-
wrapperType = _props.wrapperType;
131-
132-
// usual case: one child that is a react Element
133-
134-
if ((0, _react.isValidElement)(children)) {
135-
return children;
136-
}
137-
138-
// no children
139-
if (!children) {
140-
return null;
141-
}
142-
143-
// string children, multiple children, or something else
144-
return _react.createElement.apply(undefined, [wrapperType, {}].concat(_toConsumableArray(_react.Children.toArray(children))));
145-
}
146-
}, {
147-
key: "getPollingConfig",
148-
value: function getPollingConfig() {
149-
switch (this.props.polling) {
150-
case true:
151-
return defaultPollingConfig;
152-
case false:
153-
return { enabled: false };
154-
default:
155-
return Object.assign({}, defaultPollingConfig, this.props.polling);
156-
}
157-
}
158-
}, {
159-
key: "goOnline",
160-
value: function goOnline() {
161-
if (!this.state.online) {
162-
this.callOnChangeHandler(true);
163-
this.setState({ online: true });
164-
}
165-
}
166-
}, {
167-
key: "goOffline",
168-
value: function goOffline() {
169-
if (this.state.online) {
170-
this.callOnChangeHandler(false);
171-
this.setState({ online: false });
172-
}
173-
}
174-
}, {
175-
key: "callOnChangeHandler",
176-
value: function callOnChangeHandler(online) {
177-
if (this.props.onChange) {
178-
this.props.onChange(online);
179-
}
180-
}
181-
}, {
182-
key: "startPolling",
183-
value: function startPolling() {
184-
var _this2 = this;
185-
186-
var _getPollingConfig = this.getPollingConfig(),
187-
interval = _getPollingConfig.interval;
188-
189-
this.pollingId = setInterval(function () {
190-
var _getPollingConfig2 = _this2.getPollingConfig(),
191-
url = _getPollingConfig2.url,
192-
timeout = _getPollingConfig2.timeout;
193-
194-
ping({ url: url, timeout: timeout }).then(function (online) {
195-
online ? _this2.goOnline() : _this2.goOffline();
196-
});
197-
}, interval);
198-
}
199-
}, {
200-
key: "stopPolling",
201-
value: function stopPolling() {
202-
clearInterval(this.pollingId);
203-
}
204-
}]);
205-
206-
return Base;
207-
}(_react.Component);
208-
209-
Base.propTypes = propTypes;
210-
Base.defaultProps = defaultProps;
211-
212-
var Online = exports.Online = function (_Base) {
213-
_inherits(Online, _Base);
214-
215-
function Online() {
216-
_classCallCheck(this, Online);
217-
218-
return _possibleConstructorReturn(this, (Online.__proto__ || Object.getPrototypeOf(Online)).apply(this, arguments));
11+
});
12+
Object.defineProperty(exports, "Offline", {
13+
enumerable: true,
14+
get: function get() {
15+
return _Offline["default"];
21916
}
220-
221-
_createClass(Online, [{
222-
key: "render",
223-
value: function render() {
224-
return this.state.online ? this.renderChildren() : null;
225-
}
226-
}]);
227-
228-
return Online;
229-
}(Base);
230-
231-
Online.propTypes = propTypes;
232-
Online.defaultProps = defaultProps;
233-
234-
var Offline = exports.Offline = function (_Base2) {
235-
_inherits(Offline, _Base2);
236-
237-
function Offline() {
238-
_classCallCheck(this, Offline);
239-
240-
return _possibleConstructorReturn(this, (Offline.__proto__ || Object.getPrototypeOf(Offline)).apply(this, arguments));
17+
});
18+
Object.defineProperty(exports, "Detector", {
19+
enumerable: true,
20+
get: function get() {
21+
return _Detector["default"];
24122
}
23+
});
24+
Object.defineProperty(exports, "useConnection", {
25+
enumerable: true,
26+
get: function get() {
27+
return _useConnection["default"];
28+
}
29+
});
24230

243-
_createClass(Offline, [{
244-
key: "render",
245-
value: function render() {
246-
return !this.state.online ? this.renderChildren() : null;
247-
}
248-
}]);
249-
250-
return Offline;
251-
}(Base);
252-
253-
Offline.propTypes = propTypes;
254-
Offline.defaultProps = defaultProps;
255-
256-
var Detector = exports.Detector = function (_Base3) {
257-
_inherits(Detector, _Base3);
258-
259-
function Detector() {
260-
_classCallCheck(this, Detector);
31+
var _Online = _interopRequireDefault(require("./Online"));
26132

262-
return _possibleConstructorReturn(this, (Detector.__proto__ || Object.getPrototypeOf(Detector)).apply(this, arguments));
263-
}
33+
var _Offline = _interopRequireDefault(require("./Offline"));
26434

265-
_createClass(Detector, [{
266-
key: "render",
267-
value: function render() {
268-
return this.props.render({ online: this.state.online });
269-
}
270-
}]);
35+
var _Detector = _interopRequireDefault(require("./Detector"));
27136

272-
return Detector;
273-
}(Base);
37+
var _useConnection = _interopRequireDefault(require("./useConnection"));
27438

275-
Detector.propTypes = Object.assign({}, propTypes, {
276-
render: _propTypes2.default.func.isRequired
277-
});
278-
Detector.defaultProps = defaultProps;
39+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

package.json

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,29 @@
77
"type": "git",
88
"url": "git+https://github.com/chrisbolin/react-detect-offline.git"
99
},
10-
"files": [
11-
"/dist/"
12-
],
10+
"files": ["/dist/"],
1311
"author": "Chris Bolin",
1412
"license": "MIT",
13+
"types": "types.d.ts",
1514
"devDependencies": {
16-
"babel-cli": "^6.24.1",
17-
"babel-eslint": "^8.2.2",
18-
"babel-jest": "^21.0.0",
19-
"babel-preset-es2015": "^6.24.1",
20-
"babel-preset-react": "^6.24.1",
21-
"enzyme": "^2.9.1",
22-
"enzyme-to-json": "^1.5.1",
15+
"@testing-library/react": "^8.0.7",
16+
"@babel/cli": "^7.5.5",
17+
"@babel/preset-env": "^7.5.5",
18+
"@babel/preset-react": "^7.0.0",
19+
"babel-eslint": "^10.0.2",
20+
"babel-jest": "^24.8.0",
2321
"eslint": "^4.19.1",
2422
"eslint-config-formidable": "^3.0.0",
2523
"eslint-config-prettier": "^2.9.0",
2624
"eslint-plugin-filenames": "^1.2.0",
2725
"eslint-plugin-import": "^2.9.0",
2826
"eslint-plugin-prettier": "^2.6.0",
2927
"eslint-plugin-react": "^7.7.0",
30-
"jest": "^21.0.1",
28+
"jest": "^24.8.0",
3129
"prettier": "^1.11.1",
3230
"prop-types": "^15.6.1",
33-
"react": "^15.6.1",
34-
"react-dom": "^15.6.1",
35-
"react-test-renderer": "^15.6.1"
31+
"react": "^16.9.0-alpha.0",
32+
"react-dom": "^16.9.0-alpha.0"
3633
},
3734
"bugs": {
3835
"url": "https://github.com/chrisbolin/react-detect-offline/issues"
@@ -46,14 +43,9 @@
4643
"prettier": "prettier src/** demo/src/** --write",
4744
"test.cover": "jest spec --coverage"
4845
},
49-
"keywords": [
50-
"react",
51-
"offline",
52-
"online",
53-
"detect",
54-
"disconnect"
55-
],
46+
"keywords": ["react", "offline", "online", "detect", "disconnect"],
5647
"jest": {
48+
"setupFilesAfterEnv": ["@testing-library/react/cleanup-after-each"],
5749
"collectCoverageFrom": [
5850
"src/**/*.js",
5951
"!<rootDir>/node_modules/",

0 commit comments

Comments
 (0)