UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

130 lines 6.44 kB
import * as tslib_1 from "tslib"; import * as React from 'react'; import { BaseComponent, autobind, elementContains, getNativeProps, divProperties, getFirstFocusable, getLastTabbable, getNextElement } from '../../Utilities'; var FocusTrapZone = /** @class */ (function (_super) { tslib_1.__extends(FocusTrapZone, _super); function FocusTrapZone() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._isInFocusStack = false; _this._isInClickStack = false; 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, 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); } } if (!ignoreExternalFocusing && this._previouslyFocusedElement && typeof this._previouslyFocusedElement.focus === 'function') { this._previouslyFocusedElement.focus(); } }; 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._resolveRef('_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 (focusSelector) { _firstFocusableChild = this._root.querySelector('.' + focusSelector); } else { _firstFocusableChild = getNextElement(this._root, this._root.firstChild, true, false, false, true); } if (_firstFocusableChild) { _firstFocusableChild.focus(); } }; FocusTrapZone.prototype._onKeyboardHandler = function (ev) { if (ev.which !== 9 /* tab */) { return; } var _firstFocusableChild = getFirstFocusable(this._root, this._root.firstChild, true); var _lastFocusableChild = getLastTabbable(this._root, this._root.lastChild, true); if (ev.shiftKey && _firstFocusableChild === ev.target) { _lastFocusableChild.focus(); ev.preventDefault(); ev.stopPropagation(); } else if (!ev.shiftKey && _lastFocusableChild === ev.target) { _firstFocusableChild.focus(); ev.preventDefault(); ev.stopPropagation(); } }; FocusTrapZone.prototype._forceFocusInTrap = function (ev) { if (FocusTrapZone._focusStack.length && this === FocusTrapZone._focusStack[FocusTrapZone._focusStack.length - 1]) { var focusedElement = document.activeElement; if (!elementContains(this._root, 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, clickedElement)) { this.focus(); ev.preventDefault(); ev.stopPropagation(); } } }; FocusTrapZone._focusStack = []; FocusTrapZone._clickStack = []; tslib_1.__decorate([ autobind ], FocusTrapZone.prototype, "_onKeyboardHandler", null); return FocusTrapZone; }(BaseComponent)); export { FocusTrapZone }; //# sourceMappingURL=FocusTrapZone.js.map