@wordpress/compose
Version:
WordPress higher-order components (HOCs).
85 lines (83 loc) • 3.65 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/compose/src/hooks/use-dialog/index.ts
var use_dialog_exports = {};
__export(use_dialog_exports, {
default: () => use_dialog_default
});
module.exports = __toCommonJS(use_dialog_exports);
var import_element = require("@wordpress/element");
var import_keycodes = require("@wordpress/keycodes");
var import_use_constrained_tabbing = __toESM(require("../use-constrained-tabbing"));
var import_use_focus_on_mount = __toESM(require("../use-focus-on-mount"));
var import_use_focus_return = __toESM(require("../use-focus-return"));
var import_use_focus_outside = __toESM(require("../use-focus-outside"));
var import_use_merge_refs = __toESM(require("../use-merge-refs"));
function useDialog(options) {
const currentOptions = (0, import_element.useRef)();
const { constrainTabbing = options.focusOnMount !== false } = options;
(0, import_element.useEffect)(() => {
currentOptions.current = options;
}, Object.values(options));
const constrainedTabbingRef = (0, import_use_constrained_tabbing.default)();
const focusOnMountRef = (0, import_use_focus_on_mount.default)(options.focusOnMount);
const focusReturnRef = (0, import_use_focus_return.default)();
const focusOutsideProps = (0, import_use_focus_outside.default)((event) => {
if (currentOptions.current?.__unstableOnClose) {
currentOptions.current.__unstableOnClose("focus-outside", event);
} else if (currentOptions.current?.onClose) {
currentOptions.current.onClose();
}
});
const closeOnEscapeRef = (0, import_element.useCallback)((node) => {
if (!node) {
return;
}
node.addEventListener("keydown", (event) => {
if (event.keyCode === import_keycodes.ESCAPE && !event.defaultPrevented && currentOptions.current?.onClose) {
event.preventDefault();
currentOptions.current.onClose();
}
});
}, []);
return [
(0, import_use_merge_refs.default)([
constrainTabbing ? constrainedTabbingRef : null,
options.focusOnMount !== false ? focusReturnRef : null,
options.focusOnMount !== false ? focusOnMountRef : null,
closeOnEscapeRef
]),
{
...focusOutsideProps,
tabIndex: -1
}
];
}
var use_dialog_default = useDialog;
//# sourceMappingURL=index.js.map