@awsui/components-react
Version:
AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A
45 lines (44 loc) • 1.76 kB
JavaScript
function containsOrEqual(container, node) {
return container === node || container.contains(node);
}
var FocusTracker = (function () {
function FocusTracker(node, _a) {
var _this = this;
var onFocusEnter = _a.onFocusEnter, onFocusLeave = _a.onFocusLeave;
this.node = node;
this.currentlyFocused = false;
this.focusInListener = function (event) {
var focusIsInside = containsOrEqual(_this.node, event.target);
if (!_this.currentlyFocused && focusIsInside) {
_this.triggerFocus();
}
};
this.focusOutListener = function (event) {
var nextFocused = event.relatedTarget;
if (_this.currentlyFocused && (nextFocused === null || !containsOrEqual(_this.node, nextFocused))) {
_this.triggerBlur();
}
};
this.onFocusEnter = onFocusEnter;
this.onFocusLeave = onFocusLeave;
}
FocusTracker.prototype.initialize = function () {
this.currentlyFocused = containsOrEqual(this.node, document.activeElement);
document.addEventListener('focusin', this.focusInListener);
document.addEventListener('focusout', this.focusOutListener);
};
FocusTracker.prototype.destroy = function () {
document.removeEventListener('focusin', this.focusInListener);
document.removeEventListener('focusout', this.focusOutListener);
};
FocusTracker.prototype.triggerBlur = function () {
this.currentlyFocused = false;
this.onFocusLeave();
};
FocusTracker.prototype.triggerFocus = function () {
this.currentlyFocused = true;
this.onFocusEnter();
};
return FocusTracker;
}());
export default FocusTracker;