mongodb-chatbot-ui
Version:
UI React components for the MongoDB Chatbot Framework
835 lines (834 loc) • 33.5 kB
JavaScript
import { a as ae, g as pe } from "./jsx-runtime.js";
import be from "react";
import { p as he } from "./Transition.js";
/*!
* tabbable 5.3.3
* @license MIT, https://github.com/focus-trap/tabbable/blob/master/LICENSE
*/
var ne = ["input", "select", "textarea", "a[href]", "button", "[tabindex]:not(slot)", "audio[controls]", "video[controls]", '[contenteditable]:not([contenteditable="false"])', "details>summary:first-of-type", "details"], _ = /* @__PURE__ */ ne.join(","), re = typeof Element > "u", E = re ? function() {
} : Element.prototype.matches || Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector, M = !re && Element.prototype.getRootNode ? function(r) {
return r.getRootNode();
} : function(r) {
return r.ownerDocument;
}, ie = function(e, a, n) {
var t = Array.prototype.slice.apply(e.querySelectorAll(_));
return a && E.call(e, _) && t.unshift(e), t = t.filter(n), t;
}, oe = function r(e, a, n) {
for (var t = [], i = Array.from(e); i.length; ) {
var c = i.shift();
if (c.tagName === "SLOT") {
var l = c.assignedElements(), v = l.length ? l : c.children, d = r(v, !0, n);
n.flatten ? t.push.apply(t, d) : t.push({
scope: c,
candidates: d
});
} else {
var p = E.call(c, _);
p && n.filter(c) && (a || !e.includes(c)) && t.push(c);
var g = c.shadowRoot || // check for an undisclosed shadow
typeof n.getShadowRoot == "function" && n.getShadowRoot(c), T = !n.shadowRootFilter || n.shadowRootFilter(c);
if (g && T) {
var O = r(g === !0 ? c.children : g.children, !0, n);
n.flatten ? t.push.apply(t, O) : t.push({
scope: c,
candidates: O
});
} else
i.unshift.apply(i, c.children);
}
}
return t;
}, ue = function(e, a) {
return e.tabIndex < 0 && (a || /^(AUDIO|VIDEO|DETAILS)$/.test(e.tagName) || e.isContentEditable) && isNaN(parseInt(e.getAttribute("tabindex"), 10)) ? 0 : e.tabIndex;
}, ye = function(e, a) {
return e.tabIndex === a.tabIndex ? e.documentOrder - a.documentOrder : e.tabIndex - a.tabIndex;
}, ce = function(e) {
return e.tagName === "INPUT";
}, ge = function(e) {
return ce(e) && e.type === "hidden";
}, me = function(e) {
var a = e.tagName === "DETAILS" && Array.prototype.slice.apply(e.children).some(function(n) {
return n.tagName === "SUMMARY";
});
return a;
}, Te = function(e, a) {
for (var n = 0; n < e.length; n++)
if (e[n].checked && e[n].form === a)
return e[n];
}, Oe = function(e) {
if (!e.name)
return !0;
var a = e.form || M(e), n = function(l) {
return a.querySelectorAll('input[type="radio"][name="' + l + '"]');
}, t;
if (typeof window < "u" && typeof window.CSS < "u" && typeof window.CSS.escape == "function")
t = n(window.CSS.escape(e.name));
else
try {
t = n(e.name);
} catch (c) {
return console.error("Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s", c.message), !1;
}
var i = Te(t, e.form);
return !i || i === e;
}, Fe = function(e) {
return ce(e) && e.type === "radio";
}, we = function(e) {
return Fe(e) && !Oe(e);
}, Y = function(e) {
var a = e.getBoundingClientRect(), n = a.width, t = a.height;
return n === 0 && t === 0;
}, De = function(e, a) {
var n = a.displayCheck, t = a.getShadowRoot;
if (getComputedStyle(e).visibility === "hidden")
return !0;
var i = E.call(e, "details>summary:first-of-type"), c = i ? e.parentElement : e;
if (E.call(c, "details:not([open]) *"))
return !0;
var l = M(e).host, v = (l == null ? void 0 : l.ownerDocument.contains(l)) || e.ownerDocument.contains(e);
if (!n || n === "full") {
if (typeof t == "function") {
for (var d = e; e; ) {
var p = e.parentElement, g = M(e);
if (p && !p.shadowRoot && t(p) === !0)
return Y(e);
e.assignedSlot ? e = e.assignedSlot : !p && g !== e.ownerDocument ? e = g.host : e = p;
}
e = d;
}
if (v)
return !e.getClientRects().length;
} else if (n === "non-zero-area")
return Y(e);
return !1;
}, Ee = function(e) {
if (/^(INPUT|BUTTON|SELECT|TEXTAREA)$/.test(e.tagName))
for (var a = e.parentElement; a; ) {
if (a.tagName === "FIELDSET" && a.disabled) {
for (var n = 0; n < a.children.length; n++) {
var t = a.children.item(n);
if (t.tagName === "LEGEND")
return E.call(a, "fieldset[disabled] *") ? !0 : !t.contains(e);
}
return !0;
}
a = a.parentElement;
}
return !1;
}, x = function(e, a) {
return !(a.disabled || ge(a) || De(a, e) || // For a details element with a summary, the summary element gets the focus
me(a) || Ee(a));
}, q = function(e, a) {
return !(we(a) || ue(a) < 0 || !x(e, a));
}, Se = function(e) {
var a = parseInt(e.getAttribute("tabindex"), 10);
return !!(isNaN(a) || a >= 0);
}, ke = function r(e) {
var a = [], n = [];
return e.forEach(function(t, i) {
var c = !!t.scope, l = c ? t.scope : t, v = ue(l, c), d = c ? r(t.candidates) : l;
v === 0 ? c ? a.push.apply(a, d) : a.push(l) : n.push({
documentOrder: i,
tabIndex: v,
item: t,
isScope: c,
content: d
});
}), n.sort(ye).reduce(function(t, i) {
return i.isScope ? t.push.apply(t, i.content) : t.push(i.content), t;
}, []).concat(a);
}, se = function(e, a) {
a = a || {};
var n;
return a.getShadowRoot ? n = oe([e], a.includeContainer, {
filter: q.bind(null, a),
flatten: !1,
getShadowRoot: a.getShadowRoot,
shadowRootFilter: Se
}) : n = ie(e, a.includeContainer, q.bind(null, a)), ke(n);
}, le = function(e, a) {
a = a || {};
var n;
return a.getShadowRoot ? n = oe([e], a.includeContainer, {
filter: x.bind(null, a),
flatten: !0,
getShadowRoot: a.getShadowRoot
}) : n = ie(e, a.includeContainer, x.bind(null, a)), n;
}, C = function(e, a) {
if (a = a || {}, !e)
throw new Error("No node provided");
return E.call(e, _) === !1 ? !1 : q(a, e);
}, Re = /* @__PURE__ */ ne.concat("iframe").join(","), A = function(e, a) {
if (a = a || {}, !e)
throw new Error("No node provided");
return E.call(e, Re) === !1 ? !1 : x(a, e);
};
const Ce = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
focusable: le,
isFocusable: A,
isTabbable: C,
tabbable: se
}, Symbol.toStringTag, { value: "Module" }));
/*!
* focus-trap 6.9.4
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
*/
function Z(r, e) {
var a = Object.keys(r);
if (Object.getOwnPropertySymbols) {
var n = Object.getOwnPropertySymbols(r);
e && (n = n.filter(function(t) {
return Object.getOwnPropertyDescriptor(r, t).enumerable;
})), a.push.apply(a, n);
}
return a;
}
function Q(r) {
for (var e = 1; e < arguments.length; e++) {
var a = arguments[e] != null ? arguments[e] : {};
e % 2 ? Z(Object(a), !0).forEach(function(n) {
Pe(r, n, a[n]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(r, Object.getOwnPropertyDescriptors(a)) : Z(Object(a)).forEach(function(n) {
Object.defineProperty(r, n, Object.getOwnPropertyDescriptor(a, n));
});
}
return r;
}
function Pe(r, e, a) {
return e in r ? Object.defineProperty(r, e, {
value: a,
enumerable: !0,
configurable: !0,
writable: !0
}) : r[e] = a, r;
}
var X = function() {
var r = [];
return {
activateTrap: function(a) {
if (r.length > 0) {
var n = r[r.length - 1];
n !== a && n.pause();
}
var t = r.indexOf(a);
t === -1 || r.splice(t, 1), r.push(a);
},
deactivateTrap: function(a) {
var n = r.indexOf(a);
n !== -1 && r.splice(n, 1), r.length > 0 && r[r.length - 1].unpause();
}
};
}(), Ne = function(e) {
return e.tagName && e.tagName.toLowerCase() === "input" && typeof e.select == "function";
}, Ie = function(e) {
return e.key === "Escape" || e.key === "Esc" || e.keyCode === 27;
}, Ae = function(e) {
return e.key === "Tab" || e.keyCode === 9;
}, J = function(e) {
return setTimeout(e, 0);
}, ee = function(e, a) {
var n = -1;
return e.every(function(t, i) {
return a(t) ? (n = i, !1) : !0;
}), n;
}, k = function(e) {
for (var a = arguments.length, n = new Array(a > 1 ? a - 1 : 0), t = 1; t < a; t++)
n[t - 1] = arguments[t];
return typeof e == "function" ? e.apply(void 0, n) : e;
}, N = function(e) {
return e.target.shadowRoot && typeof e.composedPath == "function" ? e.composedPath()[0] : e.target;
}, _e = function(e, a) {
var n = (a == null ? void 0 : a.document) || document, t = Q({
returnFocusOnDeactivate: !0,
escapeDeactivates: !0,
delayInitialFocus: !0
}, a), i = {
// containers given to createFocusTrap()
// @type {Array<HTMLElement>}
containers: [],
// list of objects identifying tabbable nodes in `containers` in the trap
// NOTE: it's possible that a group has no tabbable nodes if nodes get removed while the trap
// is active, but the trap should never get to a state where there isn't at least one group
// with at least one tabbable node in it (that would lead to an error condition that would
// result in an error being thrown)
// @type {Array<{
// container: HTMLElement,
// tabbableNodes: Array<HTMLElement>, // empty if none
// focusableNodes: Array<HTMLElement>, // empty if none
// firstTabbableNode: HTMLElement|null,
// lastTabbableNode: HTMLElement|null,
// nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
// }>}
containerGroups: [],
// same order/length as `containers` list
// references to objects in `containerGroups`, but only those that actually have
// tabbable nodes in them
// NOTE: same order as `containers` and `containerGroups`, but __not necessarily__
// the same length
tabbableGroups: [],
nodeFocusedBeforeActivation: null,
mostRecentlyFocusedNode: null,
active: !1,
paused: !1,
// timer ID for when delayInitialFocus is true and initial focus in this trap
// has been delayed during activation
delayInitialFocusTimer: void 0
}, c, l = function(o, u, f) {
return o && o[u] !== void 0 ? o[u] : t[f || u];
}, v = function(o) {
return i.containerGroups.findIndex(function(u) {
var f = u.container, y = u.tabbableNodes;
return f.contains(o) || // fall back to explicit tabbable search which will take into consideration any
// web components if the `tabbableOptions.getShadowRoot` option was used for
// the trap, enabling shadow DOM support in tabbable (`Node.contains()` doesn't
// look inside web components even if open)
y.find(function(b) {
return b === o;
});
});
}, d = function(o) {
var u = t[o];
if (typeof u == "function") {
for (var f = arguments.length, y = new Array(f > 1 ? f - 1 : 0), b = 1; b < f; b++)
y[b - 1] = arguments[b];
u = u.apply(void 0, y);
}
if (u === !0 && (u = void 0), !u) {
if (u === void 0 || u === !1)
return u;
throw new Error("`".concat(o, "` was specified but was not a node, or did not return a node"));
}
var m = u;
if (typeof u == "string" && (m = n.querySelector(u), !m))
throw new Error("`".concat(o, "` as selector refers to no known node"));
return m;
}, p = function() {
var o = d("initialFocus");
if (o === !1)
return !1;
if (o === void 0)
if (v(n.activeElement) >= 0)
o = n.activeElement;
else {
var u = i.tabbableGroups[0], f = u && u.firstTabbableNode;
o = f || d("fallbackFocus");
}
if (!o)
throw new Error("Your focus-trap needs to have at least one focusable element");
return o;
}, g = function() {
if (i.containerGroups = i.containers.map(function(o) {
var u = se(o, t.tabbableOptions), f = le(o, t.tabbableOptions);
return {
container: o,
tabbableNodes: u,
focusableNodes: f,
firstTabbableNode: u.length > 0 ? u[0] : null,
lastTabbableNode: u.length > 0 ? u[u.length - 1] : null,
/**
* Finds the __tabbable__ node that follows the given node in the specified direction,
* in this container, if any.
* @param {HTMLElement} node
* @param {boolean} [forward] True if going in forward tab order; false if going
* in reverse.
* @returns {HTMLElement|undefined} The next tabbable node, if any.
*/
nextTabbableNode: function(b) {
var m = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !0, F = f.findIndex(function(D) {
return D === b;
});
if (!(F < 0))
return m ? f.slice(F + 1).find(function(D) {
return C(D, t.tabbableOptions);
}) : f.slice(0, F).reverse().find(function(D) {
return C(D, t.tabbableOptions);
});
}
};
}), i.tabbableGroups = i.containerGroups.filter(function(o) {
return o.tabbableNodes.length > 0;
}), i.tabbableGroups.length <= 0 && !d("fallbackFocus"))
throw new Error("Your focus-trap must have at least one container with at least one tabbable node in it at all times");
}, T = function h(o) {
if (o !== !1 && o !== n.activeElement) {
if (!o || !o.focus) {
h(p());
return;
}
o.focus({
preventScroll: !!t.preventScroll
}), i.mostRecentlyFocusedNode = o, Ne(o) && o.select();
}
}, O = function(o) {
var u = d("setReturnFocus", o);
return u || (u === !1 ? !1 : o);
}, w = function(o) {
var u = N(o);
if (!(v(u) >= 0)) {
if (k(t.clickOutsideDeactivates, o)) {
c.deactivate({
// if, on deactivation, we should return focus to the node originally-focused
// when the trap was activated (or the configured `setReturnFocus` node),
// then assume it's also OK to return focus to the outside node that was
// just clicked, causing deactivation, as long as that node is focusable;
// if it isn't focusable, then return focus to the original node focused
// on activation (or the configured `setReturnFocus` node)
// NOTE: by setting `returnFocus: false`, deactivate() will do nothing,
// which will result in the outside click setting focus to the node
// that was clicked, whether it's focusable or not; by setting
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
// on activation (or the configured `setReturnFocus` node)
returnFocus: t.returnFocusOnDeactivate && !A(u, t.tabbableOptions)
});
return;
}
k(t.allowOutsideClick, o) || o.preventDefault();
}
}, H = function(o) {
var u = N(o), f = v(u) >= 0;
f || u instanceof Document ? f && (i.mostRecentlyFocusedNode = u) : (o.stopImmediatePropagation(), T(i.mostRecentlyFocusedNode || p()));
}, fe = function(o) {
var u = N(o);
g();
var f = null;
if (i.tabbableGroups.length > 0) {
var y = v(u), b = y >= 0 ? i.containerGroups[y] : void 0;
if (y < 0)
o.shiftKey ? f = i.tabbableGroups[i.tabbableGroups.length - 1].lastTabbableNode : f = i.tabbableGroups[0].firstTabbableNode;
else if (o.shiftKey) {
var m = ee(i.tabbableGroups, function(L) {
var B = L.firstTabbableNode;
return u === B;
});
if (m < 0 && (b.container === u || A(u, t.tabbableOptions) && !C(u, t.tabbableOptions) && !b.nextTabbableNode(u, !1)) && (m = y), m >= 0) {
var F = m === 0 ? i.tabbableGroups.length - 1 : m - 1, D = i.tabbableGroups[F];
f = D.lastTabbableNode;
}
} else {
var S = ee(i.tabbableGroups, function(L) {
var B = L.lastTabbableNode;
return u === B;
});
if (S < 0 && (b.container === u || A(u, t.tabbableOptions) && !C(u, t.tabbableOptions) && !b.nextTabbableNode(u)) && (S = y), S >= 0) {
var de = S === i.tabbableGroups.length - 1 ? 0 : S + 1, ve = i.tabbableGroups[de];
f = ve.firstTabbableNode;
}
}
} else
f = d("fallbackFocus");
f && (o.preventDefault(), T(f));
}, z = function(o) {
if (Ie(o) && k(t.escapeDeactivates, o) !== !1) {
o.preventDefault(), c.deactivate();
return;
}
if (Ae(o)) {
fe(o);
return;
}
}, K = function(o) {
var u = N(o);
v(u) >= 0 || k(t.clickOutsideDeactivates, o) || k(t.allowOutsideClick, o) || (o.preventDefault(), o.stopImmediatePropagation());
}, V = function() {
if (i.active)
return X.activateTrap(c), i.delayInitialFocusTimer = t.delayInitialFocus ? J(function() {
T(p());
}) : T(p()), n.addEventListener("focusin", H, !0), n.addEventListener("mousedown", w, {
capture: !0,
passive: !1
}), n.addEventListener("touchstart", w, {
capture: !0,
passive: !1
}), n.addEventListener("click", K, {
capture: !0,
passive: !1
}), n.addEventListener("keydown", z, {
capture: !0,
passive: !1
}), c;
}, W = function() {
if (i.active)
return n.removeEventListener("focusin", H, !0), n.removeEventListener("mousedown", w, !0), n.removeEventListener("touchstart", w, !0), n.removeEventListener("click", K, !0), n.removeEventListener("keydown", z, !0), c;
};
return c = {
get active() {
return i.active;
},
get paused() {
return i.paused;
},
activate: function(o) {
if (i.active)
return this;
var u = l(o, "onActivate"), f = l(o, "onPostActivate"), y = l(o, "checkCanFocusTrap");
y || g(), i.active = !0, i.paused = !1, i.nodeFocusedBeforeActivation = n.activeElement, u && u();
var b = function() {
y && g(), V(), f && f();
};
return y ? (y(i.containers.concat()).then(b, b), this) : (b(), this);
},
deactivate: function(o) {
if (!i.active)
return this;
var u = Q({
onDeactivate: t.onDeactivate,
onPostDeactivate: t.onPostDeactivate,
checkCanReturnFocus: t.checkCanReturnFocus
}, o);
clearTimeout(i.delayInitialFocusTimer), i.delayInitialFocusTimer = void 0, W(), i.active = !1, i.paused = !1, X.deactivateTrap(c);
var f = l(u, "onDeactivate"), y = l(u, "onPostDeactivate"), b = l(u, "checkCanReturnFocus"), m = l(u, "returnFocus", "returnFocusOnDeactivate");
f && f();
var F = function() {
J(function() {
m && T(O(i.nodeFocusedBeforeActivation)), y && y();
});
};
return m && b ? (b(O(i.nodeFocusedBeforeActivation)).then(F, F), this) : (F(), this);
},
pause: function() {
return i.paused || !i.active ? this : (i.paused = !0, W(), this);
},
unpause: function() {
return !i.paused || !i.active ? this : (i.paused = !1, g(), V(), this);
},
updateContainerElements: function(o) {
var u = [].concat(o).filter(Boolean);
return i.containers = u.map(function(f) {
return typeof f == "string" ? n.querySelector(f) : f;
}), i.active && g(), this;
}
}, c.updateContainerElements(e), c;
};
const xe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
createFocusTrap: _e
}, Symbol.toStringTag, { value: "Module" })), je = /* @__PURE__ */ ae(xe), Le = /* @__PURE__ */ ae(Ce);
function U(r) {
"@babel/helpers - typeof";
return U = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
return typeof e;
} : function(e) {
return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
}, U(r);
}
function Be(r, e) {
if (!(r instanceof e))
throw new TypeError("Cannot call a class as a function");
}
function te(r, e) {
for (var a = 0; a < e.length; a++) {
var n = e[a];
n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(r, n.key, n);
}
}
function Me(r, e, a) {
return e && te(r.prototype, e), a && te(r, a), Object.defineProperty(r, "prototype", { writable: !1 }), r;
}
function qe(r, e) {
if (typeof e != "function" && e !== null)
throw new TypeError("Super expression must either be null or a function");
r.prototype = Object.create(e && e.prototype, { constructor: { value: r, writable: !0, configurable: !0 } }), Object.defineProperty(r, "prototype", { writable: !1 }), e && $(r, e);
}
function $(r, e) {
return $ = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(n, t) {
return n.__proto__ = t, n;
}, $(r, e);
}
function Ue(r) {
var e = Ge();
return function() {
var n = j(r), t;
if (e) {
var i = j(this).constructor;
t = Reflect.construct(n, arguments, i);
} else
t = n.apply(this, arguments);
return $e(this, t);
};
}
function $e(r, e) {
if (e && (U(e) === "object" || typeof e == "function"))
return e;
if (e !== void 0)
throw new TypeError("Derived constructors may only return object or undefined");
return P(r);
}
function P(r) {
if (r === void 0)
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
return r;
}
function Ge() {
if (typeof Reflect > "u" || !Reflect.construct || Reflect.construct.sham)
return !1;
if (typeof Proxy == "function")
return !0;
try {
return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {
})), !0;
} catch {
return !1;
}
}
function j(r) {
return j = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(a) {
return a.__proto__ || Object.getPrototypeOf(a);
}, j(r);
}
function He(r, e, a) {
return e in r ? Object.defineProperty(r, e, { value: a, enumerable: !0, configurable: !0, writable: !0 }) : r[e] = a, r;
}
var I = be, s = he, ze = je, Ke = ze.createFocusTrap, Ve = Le, We = Ve.isFocusable, G = /* @__PURE__ */ function(r) {
qe(a, r);
var e = Ue(a);
function a(n) {
var t;
Be(this, a), t = e.call(this, n), He(P(t), "getNodeForOption", function(l) {
var v, d = (v = this.internalOptions[l]) !== null && v !== void 0 ? v : this.originalOptions[l];
if (typeof d == "function") {
for (var p = arguments.length, g = new Array(p > 1 ? p - 1 : 0), T = 1; T < p; T++)
g[T - 1] = arguments[T];
d = d.apply(void 0, g);
}
if (d === !0 && (d = void 0), !d) {
if (d === void 0 || d === !1)
return d;
throw new Error("`".concat(l, "` was specified but was not a node, or did not return a node"));
}
var O = d;
if (typeof d == "string") {
var w;
if (O = (w = this.getDocument()) === null || w === void 0 ? void 0 : w.querySelector(d), !O)
throw new Error("`".concat(l, "` as selector refers to no known node"));
}
return O;
}), t.handleDeactivate = t.handleDeactivate.bind(P(t)), t.handlePostDeactivate = t.handlePostDeactivate.bind(P(t)), t.handleClickOutsideDeactivates = t.handleClickOutsideDeactivates.bind(P(t)), t.internalOptions = {
// We need to hijack the returnFocusOnDeactivate option,
// because React can move focus into the element before we arrived at
// this lifecycle hook (e.g. with autoFocus inputs). So the component
// captures the previouslyFocusedElement in componentWillMount,
// then (optionally) returns focus to it in componentWillUnmount.
returnFocusOnDeactivate: !1,
// the rest of these are also related to deactivation of the trap, and we
// need to use them and control them as well
checkCanReturnFocus: null,
onDeactivate: t.handleDeactivate,
onPostDeactivate: t.handlePostDeactivate,
// we need to special-case this setting as well so that we can know if we should
// NOT return focus if the trap gets auto-deactivated as the result of an
// outside click (otherwise, we'll always think we should return focus because
// of how we manage that flag internally here)
clickOutsideDeactivates: t.handleClickOutsideDeactivates
}, t.originalOptions = {
// because of the above `internalOptions`, we maintain our own flag for
// this option, and default it to `true` because that's focus-trap's default
returnFocusOnDeactivate: !0,
// because of the above `internalOptions`, we keep these separate since
// they're part of the deactivation process which we configure (internally) to
// be shared between focus-trap and focus-trap-react
onDeactivate: null,
onPostDeactivate: null,
checkCanReturnFocus: null,
// the user's setting, defaulted to false since focus-trap defaults this to false
clickOutsideDeactivates: !1
};
var i = n.focusTrapOptions;
for (var c in i)
if (Object.prototype.hasOwnProperty.call(i, c)) {
if (c === "returnFocusOnDeactivate" || c === "onDeactivate" || c === "onPostDeactivate" || c === "checkCanReturnFocus" || c === "clickOutsideDeactivates") {
t.originalOptions[c] = i[c];
continue;
}
t.internalOptions[c] = i[c];
}
return t.outsideClick = null, t.focusTrapElements = n.containerElements || [], t.updatePreviousElement(), t;
}
return Me(a, [{
key: "getDocument",
value: function() {
return this.props.focusTrapOptions.document || (typeof document < "u" ? document : void 0);
}
/**
* Gets the node for the given option, which is expected to be an option that
* can be either a DOM node, a string that is a selector to get a node, `false`
* (if a node is explicitly NOT given), or a function that returns any of these
* values.
* @param {string} optionName
* @returns {undefined | false | HTMLElement | SVGElement} Returns
* `undefined` if the option is not specified; `false` if the option
* resolved to `false` (node explicitly not given); otherwise, the resolved
* DOM node.
* @throws {Error} If the option is set, not `false`, and is not, or does not
* resolve to a node.
*/
}, {
key: "getReturnFocusNode",
value: function() {
var t = this.getNodeForOption("setReturnFocus", this.previouslyFocusedElement);
return t || (t === !1 ? !1 : this.previouslyFocusedElement);
}
/** Update the previously focused element with the currently focused element. */
}, {
key: "updatePreviousElement",
value: function() {
var t = this.getDocument();
t && (this.previouslyFocusedElement = t.activeElement);
}
}, {
key: "deactivateTrap",
value: function() {
!this.focusTrap || !this.focusTrap.active || this.focusTrap.deactivate({
// NOTE: we never let the trap return the focus since we do that ourselves
returnFocus: !1,
// we'll call this in our own post deactivate handler so make sure the trap doesn't
// do it prematurely
checkCanReturnFocus: null,
// let it call the user's original deactivate handler, if any, instead of
// our own which calls back into this function
onDeactivate: this.originalOptions.onDeactivate
// NOTE: for post deactivate, don't specify anything so that it calls the
// onPostDeactivate handler specified on `this.internalOptions`
// which will always be our own `handlePostDeactivate()` handler, which
// will finish things off by calling the user's provided onPostDeactivate
// handler, if any, at the right time
// onPostDeactivate: NOTHING
});
}
}, {
key: "handleClickOutsideDeactivates",
value: function(t) {
var i = typeof this.originalOptions.clickOutsideDeactivates == "function" ? this.originalOptions.clickOutsideDeactivates.call(null, t) : this.originalOptions.clickOutsideDeactivates;
return i && (this.outsideClick = {
target: t.target,
allowDeactivation: i
}), i;
}
}, {
key: "handleDeactivate",
value: function() {
this.originalOptions.onDeactivate && this.originalOptions.onDeactivate.call(null), this.deactivateTrap();
}
}, {
key: "handlePostDeactivate",
value: function() {
var t = this, i = function() {
var l = t.getReturnFocusNode(), v = !!// did the consumer allow it?
(t.originalOptions.returnFocusOnDeactivate && // can we actually focus the node?
l !== null && l !== void 0 && l.focus && // was there an outside click that allowed deactivation?
(!t.outsideClick || // did the consumer allow deactivation when the outside node was clicked?
t.outsideClick.allowDeactivation && // is the outside node NOT focusable (implying that it did NOT receive focus
// as a result of the click-through) -- in which case do NOT restore focus
// to `returnFocusNode` because focus should remain on the outside node
!We(t.outsideClick.target, t.internalOptions.tabbableOptions))), d = t.internalOptions.preventScroll, p = d === void 0 ? !1 : d;
v && l.focus({
preventScroll: p
}), t.originalOptions.onPostDeactivate && t.originalOptions.onPostDeactivate.call(null), t.outsideClick = null;
};
this.originalOptions.checkCanReturnFocus ? this.originalOptions.checkCanReturnFocus.call(null, this.getReturnFocusNode()).then(i, i) : i();
}
}, {
key: "setupFocusTrap",
value: function() {
if (this.focusTrap)
this.props.active && !this.focusTrap.active && (this.focusTrap.activate(), this.props.paused && this.focusTrap.pause());
else {
var t = this.focusTrapElements.some(Boolean);
t && (this.focusTrap = this.props._createFocusTrap(this.focusTrapElements, this.internalOptions), this.props.active && this.focusTrap.activate(), this.props.paused && this.focusTrap.pause());
}
}
}, {
key: "componentDidMount",
value: function() {
this.props.active && this.setupFocusTrap();
}
}, {
key: "componentDidUpdate",
value: function(t) {
if (this.focusTrap) {
t.containerElements !== this.props.containerElements && this.focusTrap.updateContainerElements(this.props.containerElements);
var i = !t.active && this.props.active, c = t.active && !this.props.active, l = !t.paused && this.props.paused, v = t.paused && !this.props.paused;
if (i && (this.updatePreviousElement(), this.focusTrap.activate()), c) {
this.deactivateTrap();
return;
}
l && this.focusTrap.pause(), v && this.focusTrap.unpause();
} else
t.containerElements !== this.props.containerElements && (this.focusTrapElements = this.props.containerElements), this.props.active && (this.updatePreviousElement(), this.setupFocusTrap());
}
}, {
key: "componentWillUnmount",
value: function() {
this.deactivateTrap();
}
}, {
key: "render",
value: function() {
var t = this, i = this.props.children ? I.Children.only(this.props.children) : void 0;
if (i) {
if (i.type && i.type === I.Fragment)
throw new Error("A focus-trap cannot use a Fragment as its child container. Try replacing it with a <div> element.");
var c = function(d) {
var p = t.props.containerElements;
i && (typeof i.ref == "function" ? i.ref(d) : i.ref && (i.ref.current = d)), t.focusTrapElements = p || [d];
}, l = I.cloneElement(i, {
ref: c
});
return l;
}
return null;
}
}]), a;
}(I.Component), R = typeof Element > "u" ? Function : Element;
G.propTypes = {
active: s.bool,
paused: s.bool,
focusTrapOptions: s.shape({
document: s.object,
onActivate: s.func,
onPostActivate: s.func,
checkCanFocusTrap: s.func,
onDeactivate: s.func,
onPostDeactivate: s.func,
checkCanReturnFocus: s.func,
initialFocus: s.oneOfType([s.instanceOf(R), s.string, s.bool, s.func]),
fallbackFocus: s.oneOfType([
s.instanceOf(R),
s.string,
// NOTE: does not support `false` as value (or return value from function)
s.func
]),
escapeDeactivates: s.oneOfType([s.bool, s.func]),
clickOutsideDeactivates: s.oneOfType([s.bool, s.func]),
returnFocusOnDeactivate: s.bool,
setReturnFocus: s.oneOfType([s.instanceOf(R), s.string, s.bool, s.func]),
allowOutsideClick: s.oneOfType([s.bool, s.func]),
preventScroll: s.bool,
tabbableOptions: s.shape({
displayCheck: s.oneOf(["full", "non-zero-area", "none"]),
getShadowRoot: s.oneOfType([s.bool, s.func])
})
}),
containerElements: s.arrayOf(s.instanceOf(R)),
// DOM element ONLY
children: s.oneOfType([
s.element,
// React element
s.instanceOf(R)
// DOM element
])
// NOTE: _createFocusTrap is internal, for testing purposes only, so we don't
// specify it here. It's expected to be set to the function returned from
// require('focus-trap'), or one with a compatible interface.
};
G.defaultProps = {
active: !0,
paused: !1,
focusTrapOptions: {},
_createFocusTrap: Ke
};
var Ye = G;
const Je = /* @__PURE__ */ pe(Ye);
export {
Je as f
};