Skip to content

Commit

Permalink
Prefer document.hasFocus() to tracking focus/blur events
Browse files Browse the repository at this point in the history
  • Loading branch information
mikol committed Sep 14, 2022
1 parent 295c444 commit a338d4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 8 additions & 5 deletions lib/components/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down Expand Up @@ -42,15 +42,17 @@ 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) {
return false;
}

return true;
}
};

exports.checkNotificationPromise = checkNotificationPromise;

var Notification =
/*#__PURE__*/
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/components/Notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit a338d4f

Please sign in to comment.