office-ui-fabric-react
Version:
Reusable React components for building experiences for Office 365.
144 lines • 7.12 kB
JavaScript
import * as tslib_1 from "tslib";
import * as React from 'react';
import { BaseComponent, elementContains, getNativeProps, divProperties, getFirstFocusable, getLastTabbable, getNextElement, focusAsync, createRef } from '../../Utilities';
var FocusTrapZone = /** @class */ (function (_super) {
tslib_1.__extends(FocusTrapZone, _super);
function FocusTrapZone() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._root = createRef();
_this._isInFocusStack = false;
_this._isInClickStack = false;
_this._onKeyboardHandler = function (ev) {
if (_this.props.onKeyDown) {
_this.props.onKeyDown(ev);
}
// If the default has been prevented, do not process keyboard events.
if (ev.isDefaultPrevented()) {
return;
}
if (ev.which !== 9 /* tab */) {
return;
}
if (!_this._root.current) {
return;
}
var _firstFocusableChild = getFirstFocusable(_this._root.current, _this._root.current.firstChild, true);
var _lastFocusableChild = getLastTabbable(_this._root.current, _this._root.current.lastChild, true);
if (ev.shiftKey && _firstFocusableChild === ev.target) {
focusAsync(_lastFocusableChild);
ev.preventDefault();
ev.stopPropagation();
}
else if (!ev.shiftKey && _lastFocusableChild === ev.target) {
focusAsync(_firstFocusableChild);
ev.preventDefault();
ev.stopPropagation();
}
};
return _this;
}
FocusTrapZone.prototype.componentWillMount = function () {
var _a = this.props, _b = _a.isClickableOutsideFocusTrap, isClickableOutsideFocusTrap = _b === void 0 ? false : _b, _c = _a.forceFocusInsideTrap, forceFocusInsideTrap = _c === void 0 ? true : _c;
if (forceFocusInsideTrap) {
this._isInFocusStack = true;
FocusTrapZone._focusStack.push(this);
}
if (!isClickableOutsideFocusTrap) {
this._isInClickStack = true;
FocusTrapZone._clickStack.push(this);
}
};
FocusTrapZone.prototype.componentDidMount = function () {
var _a = this.props, _b = _a.isClickableOutsideFocusTrap, isClickableOutsideFocusTrap = _b === void 0 ? false : _b, _c = _a.forceFocusInsideTrap, forceFocusInsideTrap = _c === void 0 ? true : _c, elementToFocusOnDismiss = _a.elementToFocusOnDismiss, _d = _a.disableFirstFocus, disableFirstFocus = _d === void 0 ? false : _d;
this._previouslyFocusedElement = elementToFocusOnDismiss ? elementToFocusOnDismiss : document.activeElement;
if (!elementContains(this._root.current, this._previouslyFocusedElement) && !disableFirstFocus) {
this.focus();
}
if (forceFocusInsideTrap) {
this._events.on(window, 'focus', this._forceFocusInTrap, true);
}
if (!isClickableOutsideFocusTrap) {
this._events.on(window, 'click', this._forceClickInTrap, true);
}
};
FocusTrapZone.prototype.componentWillReceiveProps = function (nextProps) {
var elementToFocusOnDismiss = nextProps.elementToFocusOnDismiss;
if (elementToFocusOnDismiss && this._previouslyFocusedElement !== elementToFocusOnDismiss) {
this._previouslyFocusedElement = elementToFocusOnDismiss;
}
};
FocusTrapZone.prototype.componentWillUnmount = function () {
var _this = this;
var ignoreExternalFocusing = this.props.ignoreExternalFocusing;
this._events.dispose();
if (this._isInFocusStack || this._isInClickStack) {
var filter = function (value) {
return _this !== value;
};
if (this._isInFocusStack) {
FocusTrapZone._focusStack = FocusTrapZone._focusStack.filter(filter);
}
if (this._isInClickStack) {
FocusTrapZone._clickStack = FocusTrapZone._clickStack.filter(filter);
}
}
var activeElement = document.activeElement;
if (!ignoreExternalFocusing &&
this._previouslyFocusedElement &&
typeof this._previouslyFocusedElement.focus === 'function' &&
(elementContains(this._root.value, activeElement) || activeElement === document.body)) {
focusAsync(this._previouslyFocusedElement);
}
};
FocusTrapZone.prototype.render = function () {
var _a = this.props, className = _a.className, ariaLabelledBy = _a.ariaLabelledBy;
var divProps = getNativeProps(this.props, divProperties);
return (React.createElement("div", tslib_1.__assign({}, divProps, { className: className, ref: this._root, "aria-labelledby": ariaLabelledBy, onKeyDown: this._onKeyboardHandler }), this.props.children));
};
/**
* Need to expose this method in case of popups since focus needs to be set when popup is opened
*/
FocusTrapZone.prototype.focus = function () {
var firstFocusableSelector = this.props.firstFocusableSelector;
var focusSelector = typeof firstFocusableSelector === 'string'
? firstFocusableSelector
: firstFocusableSelector && firstFocusableSelector();
var _firstFocusableChild;
if (this._root.current) {
if (focusSelector) {
_firstFocusableChild = this._root.current.querySelector('.' + focusSelector);
}
else {
_firstFocusableChild = getNextElement(this._root.current, this._root.current.firstChild, true, false, false, true);
}
}
if (_firstFocusableChild) {
focusAsync(_firstFocusableChild);
}
};
FocusTrapZone.prototype._forceFocusInTrap = function (ev) {
if (FocusTrapZone._focusStack.length && this === FocusTrapZone._focusStack[FocusTrapZone._focusStack.length - 1]) {
var focusedElement = document.activeElement;
if (!elementContains(this._root.current, focusedElement)) {
this.focus();
ev.preventDefault();
ev.stopPropagation();
}
}
};
FocusTrapZone.prototype._forceClickInTrap = function (ev) {
if (FocusTrapZone._clickStack.length && this === FocusTrapZone._clickStack[FocusTrapZone._clickStack.length - 1]) {
var clickedElement = ev.target;
if (clickedElement && !elementContains(this._root.current, clickedElement)) {
this.focus();
ev.preventDefault();
ev.stopPropagation();
}
}
};
FocusTrapZone._focusStack = [];
FocusTrapZone._clickStack = [];
return FocusTrapZone;
}(BaseComponent));
export { FocusTrapZone };
//# sourceMappingURL=FocusTrapZone.js.map