@wordpress/compose
Version:
WordPress higher-order components (HOCs).
62 lines (60 loc) • 2.42 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/compose/src/hooks/use-focus-return/index.js
var use_focus_return_exports = {};
__export(use_focus_return_exports, {
default: () => use_focus_return_default
});
module.exports = __toCommonJS(use_focus_return_exports);
var import_element = require("@wordpress/element");
var origin = null;
function useFocusReturn(onFocusReturn) {
const ref = (0, import_element.useRef)(null);
const focusedBeforeMount = (0, import_element.useRef)(null);
const onFocusReturnRef = (0, import_element.useRef)(onFocusReturn);
(0, import_element.useEffect)(() => {
onFocusReturnRef.current = onFocusReturn;
}, [onFocusReturn]);
return (0, import_element.useCallback)((node) => {
if (node) {
ref.current = node;
if (focusedBeforeMount.current) {
return;
}
const activeDocument = node.ownerDocument.activeElement instanceof window.HTMLIFrameElement ? node.ownerDocument.activeElement.contentDocument : node.ownerDocument;
focusedBeforeMount.current = activeDocument?.activeElement ?? null;
} else if (focusedBeforeMount.current) {
const isFocused = ref.current?.contains(
ref.current?.ownerDocument.activeElement
);
if (ref.current?.isConnected && !isFocused) {
origin ??= focusedBeforeMount.current;
return;
}
if (onFocusReturnRef.current) {
onFocusReturnRef.current();
} else {
(!focusedBeforeMount.current.isConnected ? origin : focusedBeforeMount.current)?.focus();
}
origin = null;
}
}, []);
}
var use_focus_return_default = useFocusReturn;
//# sourceMappingURL=index.js.map