From a338d4f0252f286d1d3408f644d85adc98787d50 Mon Sep 17 00:00:00 2001 From: Mikol Graves Date: Tue, 13 Sep 2022 19:10:47 -0500 Subject: [PATCH] Prefer document.hasFocus() to tracking focus/blur events Fixes #97 --- lib/components/Notification.js | 13 ++++++++----- src/components/Notification.js | 5 +++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/components/Notification.js b/lib/components/Notification.js index ae2444c..18ad15b 100644 --- a/lib/components/Notification.js +++ b/lib/components/Notification.js @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports["default"] = void 0; +exports["default"] = exports.checkNotificationPromise = void 0; var _react = _interopRequireDefault(require("react")); @@ -42,7 +42,7 @@ var seqGen = function seqGen() { var seq = seqGen(); // https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API/Using_the_Notifications_API // https://github.com/mobilusoss/react-web-notification/issues/66 -function checkNotificationPromise() { +var checkNotificationPromise = function checkNotificationPromise() { try { window.Notification.requestPermission().then(); } catch (e) { @@ -50,7 +50,9 @@ function checkNotificationPromise() { } return true; -} +}; + +exports.checkNotificationPromise = checkNotificationPromise; var Notification = /*#__PURE__*/ @@ -128,7 +130,7 @@ function (_React$Component) { }, { key: "componentDidMount", value: function componentDidMount() { - if (this.props.disableActiveWindow) { + if (this.props.disableActiveWindow && typeof document.hasFocus !== 'function') { window.addEventListener('focus', this.onWindowFocus); window.addEventListener('blur', this.onWindowBlur); } @@ -206,7 +208,8 @@ function (_React$Component) { }, { key: "render", value: function render() { - var doNotShowOnActiveWindow = this.props.disableActiveWindow && this.windowFocus; + var hasFocus = typeof document.hasFocus === 'function' ? document.hasFocus() : this.windowFocus; + var doNotShowOnActiveWindow = this.props.disableActiveWindow && hasFocus; if (!this.props.ignore && this.props.title && this.state.supported && this.state.granted && !doNotShowOnActiveWindow) { this.doNotification(); diff --git a/src/components/Notification.js b/src/components/Notification.js index d1e70cd..90842e2 100644 --- a/src/components/Notification.js +++ b/src/components/Notification.js @@ -80,7 +80,7 @@ class Notification extends React.Component { } componentDidMount(){ - if (this.props.disableActiveWindow) { + if (this.props.disableActiveWindow && typeof document.hasFocus !== 'function') { window.addEventListener('focus', this.onWindowFocus); window.addEventListener('blur', this.onWindowBlur); } @@ -140,7 +140,8 @@ class Notification extends React.Component { } render() { - let doNotShowOnActiveWindow = this.props.disableActiveWindow && this.windowFocus; + let hasFocus = typeof document.hasFocus === 'function' ? document.hasFocus() : this.windowFocus; + let doNotShowOnActiveWindow = this.props.disableActiveWindow && hasFocus; if (!this.props.ignore && this.props.title && this.state.supported && this.state.granted && !doNotShowOnActiveWindow) { this.doNotification(); }