UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

141 lines 7.29 kB
import * as tslib_1 from "tslib"; import * as React from 'react'; import { BaseComponent, elementContains, getNativeProps, divProperties, getFirstTabbable, 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._onFocusCapture = function (ev) { if (_this.props.onFocusCapture) { _this.props.onFocusCapture(ev); } if (ev.target !== ev.currentTarget) { // every time focus changes within the trap zone, remember the focused element so that // it can be restored if focus leaves the pane and returns via keystroke (i.e. via a call to this.focus(true)) _this._previouslyFocusedElementInTrapZone = ev.target; } }; _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 _firstTabbableChild = getFirstTabbable(_this._root.current, _this._root.current.firstChild, true); var _lastTabbableChild = getLastTabbable(_this._root.current, _this._root.current.lastChild, true); if (ev.shiftKey && _firstTabbableChild === ev.target) { focusAsync(_lastTabbableChild); ev.preventDefault(); ev.stopPropagation(); } else if (!ev.shiftKey && _lastTabbableChild === ev.target) { focusAsync(_firstTabbableChild); ev.preventDefault(); ev.stopPropagation(); } }; return _this; } FocusTrapZone.prototype.componentWillMount = function () { FocusTrapZone._focusStack.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._previouslyFocusedElementOutsideTrapZone = elementToFocusOnDismiss ? elementToFocusOnDismiss : document.activeElement; if (!elementContains(this._root.current, this._previouslyFocusedElementOutsideTrapZone) && !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._previouslyFocusedElementOutsideTrapZone !== elementToFocusOnDismiss) { this._previouslyFocusedElementOutsideTrapZone = elementToFocusOnDismiss; } }; FocusTrapZone.prototype.componentWillUnmount = function () { var _this = this; var ignoreExternalFocusing = this.props.ignoreExternalFocusing; this._events.dispose(); FocusTrapZone._focusStack = FocusTrapZone._focusStack.filter(function (value) { return _this !== value; }); var activeElement = document.activeElement; if (!ignoreExternalFocusing && this._previouslyFocusedElementOutsideTrapZone && typeof this._previouslyFocusedElementOutsideTrapZone.focus === 'function' && (elementContains(this._root.value, activeElement) || activeElement === document.body)) { focusAsync(this._previouslyFocusedElementOutsideTrapZone); } }; 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, onFocusCapture: this._onFocusCapture }), this.props.children)); }; FocusTrapZone.prototype.focus = function () { var _a = this.props, focusPreviouslyFocusedInnerElement = _a.focusPreviouslyFocusedInnerElement, firstFocusableSelector = _a.firstFocusableSelector; if (focusPreviouslyFocusedInnerElement && this._previouslyFocusedElementInTrapZone && elementContains(this._root.value, this._previouslyFocusedElementInTrapZone)) { // focus on the last item that had focus in the zone before we left the zone focusAsync(this._previouslyFocusedElementInTrapZone); return; } 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._focusStack.length && this === FocusTrapZone._focusStack[FocusTrapZone._focusStack.length - 1]) { var clickedElement = ev.target; if (clickedElement && !elementContains(this._root.current, clickedElement)) { this.focus(); ev.preventDefault(); ev.stopPropagation(); } } }; FocusTrapZone._focusStack = []; return FocusTrapZone; }(BaseComponent)); export { FocusTrapZone }; //# sourceMappingURL=FocusTrapZone.js.map