@elastic/eui
Version:
Elastic UI Component Library
126 lines (121 loc) • 6.35 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["children", "disabled", "clickOutsideDisables", "returnFocus", "noIsolation", "crossFrame", "scrollLock", "initialFocus", "gapMode", "closeOnMouseup", "onClickOutside"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { useState, useEffect, useCallback, useRef } from 'react';
import { FocusOn } from 'react-focus-on';
import { RemoveScrollBar } from 'react-remove-scroll-bar';
import { findElementBySelectorOrRef, useUpdateEffect } from '../../services';
import { usePropsWithComponentDefaults } from '../provider/component_defaults';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var EuiFocusTrap = function EuiFocusTrap(_props) {
var props = usePropsWithComponentDefaults('EuiFocusTrap', _props);
var children = props.children,
disabled = props.disabled,
_props$clickOutsideDi = props.clickOutsideDisables,
clickOutsideDisables = _props$clickOutsideDi === void 0 ? false : _props$clickOutsideDi,
_props$returnFocus = props.returnFocus,
returnFocus = _props$returnFocus === void 0 ? true : _props$returnFocus,
_props$noIsolation = props.noIsolation,
noIsolation = _props$noIsolation === void 0 ? true : _props$noIsolation,
_props$crossFrame = props.crossFrame,
crossFrame = _props$crossFrame === void 0 ? false : _props$crossFrame,
_props$scrollLock = props.scrollLock,
scrollLock = _props$scrollLock === void 0 ? false : _props$scrollLock,
initialFocus = props.initialFocus,
_props$gapMode = props.gapMode,
gapMode = _props$gapMode === void 0 ? 'padding' : _props$gapMode,
closeOnMouseup = props.closeOnMouseup,
onClickOutside = props.onClickOutside,
rest = _objectWithoutProperties(props, _excluded);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
hasBeenDisabledByClick = _useState2[0],
setHasBeenDisabledByClick = _useState2[1];
var isDisabled = disabled || hasBeenDisabledByClick;
// Programmatically sets focus on a nested DOM node; optional
var setInitialFocus = function setInitialFocus(initialFocus) {
if (!initialFocus) return;
var node = findElementBySelectorOrRef(initialFocus);
if (!node) return;
// `data-autofocus` is part of the 'react-focus-on' API
node.setAttribute('data-autofocus', 'true');
};
// Stabilize the onClickOutside callback
var onClickOutsideRef = useRef(onClickOutside);
onClickOutsideRef.current = onClickOutside;
// We use a ref to store the listener to prevent circular dependencies
// while still ensuring the listeners can properly be cleaned up
var mouseupListenerRef = useRef(null);
var removeMouseupListener = useCallback(function () {
if (mouseupListenerRef.current) {
document.removeEventListener('mouseup', mouseupListenerRef.current);
document.removeEventListener('touchend', mouseupListenerRef.current);
mouseupListenerRef.current = null;
}
}, []);
var addMouseupListener = useCallback(function () {
removeMouseupListener();
mouseupListenerRef.current = function (e) {
removeMouseupListener();
// Timeout gives precedence to the consumer to initiate close if it has toggle behavior.
// Otherwise this event may occur first and the consumer toggle will reopen the flyout.
setTimeout(function () {
var _onClickOutsideRef$cu;
return (_onClickOutsideRef$cu = onClickOutsideRef.current) === null || _onClickOutsideRef$cu === void 0 ? void 0 : _onClickOutsideRef$cu.call(onClickOutsideRef, e);
});
};
document.addEventListener('mouseup', mouseupListenerRef.current);
document.addEventListener('touchend', mouseupListenerRef.current);
}, [removeMouseupListener]);
var handleOutsideClick = useCallback(function (event) {
if (clickOutsideDisables) {
setHasBeenDisabledByClick(true);
}
if (onClickOutside) {
closeOnMouseup ? addMouseupListener() : onClickOutside(event);
}
}, [clickOutsideDisables, closeOnMouseup, onClickOutside, addMouseupListener]);
useEffect(function () {
setInitialFocus(initialFocus);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useUpdateEffect(function () {
if (!disabled) {
setHasBeenDisabledByClick(false);
}
}, [disabled]);
// listener cleanup on unmount
useEffect(function () {
return function () {
return removeMouseupListener();
};
}, [removeMouseupListener]);
var focusOnProps = _objectSpread(_objectSpread({
returnFocus: returnFocus,
noIsolation: noIsolation,
initialFocus: initialFocus,
crossFrame: crossFrame,
enabled: !isDisabled
}, rest), {}, {
onClickOutside: handleOutsideClick,
/**
* `scrollLock` should always be unset on FocusOn, as it can prevent scrolling on
* portals (i.e. popovers, comboboxes, dropdown menus, etc.) within modals & flyouts
* @see https://github.com/theKashey/react-focus-on/issues/49
*/
scrollLock: false
});
return ___EmotionJSX(FocusOn, focusOnProps, children, !isDisabled && scrollLock && ___EmotionJSX(RemoveScrollBar, {
gapMode: gapMode
}));
};