@platform/react
Version:
React refs and helpers.
35 lines (34 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.focus$ = void 0;
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var util_is_1 = require("@platform/util.is");
var _focus$ = new rxjs_1.Subject();
exports.focus$ = _focus$.pipe((0, operators_1.share)());
(function () {
if (!util_is_1.is.browser) {
return;
}
var last = document.activeElement;
var hasChanged = function () {
var current = document.activeElement;
var result = last !== current;
last = current;
return result;
};
var onEvent = function (type) {
return function () {
var e = {
type: type,
from: last || undefined,
to: document.activeElement || undefined,
};
if (hasChanged()) {
_focus$.next(e);
}
};
};
window.addEventListener('focus', onEvent('FOCUS'), true);
window.addEventListener('blur', onEvent('BLUR'), true);
})();