UNPKG

@wordpress/commands

Version:
307 lines (306 loc) 10.8 kB
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); var command_menu_exports = {}; __export(command_menu_exports, { CommandMenu: () => CommandMenu, CommandMenuGroup: () => CommandMenuGroup, CommandMenuLoaderWrapper: () => CommandMenuLoaderWrapper }); module.exports = __toCommonJS(command_menu_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_cmdk = require("cmdk"); var import_clsx = __toESM(require("clsx")); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_i18n = require("@wordpress/i18n"); var import_components = require("@wordpress/components"); var import_keyboard_shortcuts = require("@wordpress/keyboard-shortcuts"); var import_icons = require("@wordpress/icons"); var import_store = require("../store"); const inputLabel = (0, import_i18n.__)("Search commands and settings"); function CommandMenuLoader({ name, search, hook, setLoader, close }) { const { isLoading, commands = [] } = hook({ search }) ?? {}; (0, import_element.useEffect)(() => { setLoader(name, isLoading); }, [setLoader, name, isLoading]); if (!commands.length) { return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: commands.map((command) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_cmdk.Command.Item, { value: command.searchLabel ?? command.label, keywords: command.keywords, onSelect: () => command.callback({ close }), id: command.name, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.__experimentalHStack, { alignment: "left", className: (0, import_clsx.default)("commands-command-menu__item", { "has-icon": command.icon }), children: [ command.icon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.Icon, { icon: command.icon }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.TextHighlight, { text: command.label, highlight: search } ) }) ] } ) }, command.name )) }); } function CommandMenuLoaderWrapper({ hook, search, setLoader, close }) { const currentLoaderRef = (0, import_element.useRef)(hook); const [key, setKey] = (0, import_element.useState)(0); (0, import_element.useEffect)(() => { if (currentLoaderRef.current !== hook) { currentLoaderRef.current = hook; setKey((prevKey) => prevKey + 1); } }, [hook]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( CommandMenuLoader, { hook: currentLoaderRef.current, search, setLoader, close }, key ); } function CommandMenuGroup({ isContextual, search, setLoader, close }) { const { commands, loaders } = (0, import_data.useSelect)( (select) => { const { getCommands, getCommandLoaders } = select(import_store.store); return { commands: getCommands(isContextual), loaders: getCommandLoaders(isContextual) }; }, [isContextual] ); if (!commands.length && !loaders.length) { return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_cmdk.Command.Group, { children: [ commands.map((command) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_cmdk.Command.Item, { value: command.searchLabel ?? command.label, keywords: command.keywords, onSelect: () => command.callback({ close }), id: command.name, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)( import_components.__experimentalHStack, { alignment: "left", className: (0, import_clsx.default)("commands-command-menu__item", { "has-icon": command.icon }), children: [ command.icon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.Icon, { icon: command.icon }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.TextHighlight, { text: command.label, highlight: search } ) }) ] } ) }, command.name )), loaders.map((loader) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( CommandMenuLoaderWrapper, { hook: loader.hook, search, setLoader, close }, loader.name )) ] }); } function CommandInput({ isOpen, search, setSearch }) { const commandMenuInput = (0, import_element.useRef)(); const _value = (0, import_cmdk.useCommandState)((state) => state.value); const selectedItemId = (0, import_element.useMemo)(() => { const item = document.querySelector( `[cmdk-item=""][data-value="${_value}"]` ); return item?.getAttribute("id"); }, [_value]); (0, import_element.useEffect)(() => { if (isOpen) { commandMenuInput.current.focus(); } }, [isOpen]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_cmdk.Command.Input, { ref: commandMenuInput, value: search, onValueChange: setSearch, placeholder: inputLabel, "aria-activedescendant": selectedItemId, icon: search } ); } function CommandMenu() { const { registerShortcut } = (0, import_data.useDispatch)(import_keyboard_shortcuts.store); const [search, setSearch] = (0, import_element.useState)(""); const isOpen = (0, import_data.useSelect)( (select) => select(import_store.store).isOpen(), [] ); const { open, close } = (0, import_data.useDispatch)(import_store.store); const [loaders, setLoaders] = (0, import_element.useState)({}); (0, import_element.useEffect)(() => { registerShortcut({ name: "core/commands", category: "global", description: (0, import_i18n.__)("Open the command palette."), keyCombination: { modifier: "primary", character: "k" } }); }, [registerShortcut]); (0, import_keyboard_shortcuts.useShortcut)( "core/commands", /** @type {import('react').KeyboardEventHandler} */ (event) => { if (event.defaultPrevented) { return; } event.preventDefault(); if (isOpen) { close(); } else { open(); } }, { bindGlobal: true } ); const setLoader = (0, import_element.useCallback)( (name, value) => setLoaders((current) => ({ ...current, [name]: value })), [] ); const closeAndReset = () => { setSearch(""); close(); }; if (!isOpen) { return false; } const onKeyDown = (event) => { if ( // Ignore keydowns from IMEs event.nativeEvent.isComposing || // Workaround for Mac Safari where the final Enter/Backspace of an IME composition // is `isComposing=false`, even though it's technically still part of the composition. // These can only be detected by keyCode. event.keyCode === 229 ) { event.preventDefault(); } }; const isLoading = Object.values(loaders).some(Boolean); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Modal, { className: "commands-command-menu", overlayClassName: "commands-command-menu__overlay", onRequestClose: closeAndReset, __experimentalHideHeader: true, contentLabel: (0, import_i18n.__)("Command palette"), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "commands-command-menu__container", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_cmdk.Command, { label: inputLabel, onKeyDown, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "commands-command-menu__header", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_icons.Icon, { className: "commands-command-menu__header-search-icon", icon: import_icons.search } ), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( CommandInput, { search, setSearch, isOpen } ) ] }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_cmdk.Command.List, { label: (0, import_i18n.__)("Command suggestions"), children: [ search && !isLoading && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_cmdk.Command.Empty, { children: (0, import_i18n.__)("No results found.") }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( CommandMenuGroup, { search, setLoader, close: closeAndReset, isContextual: true } ), search && /* @__PURE__ */ (0, import_jsx_runtime.jsx)( CommandMenuGroup, { search, setLoader, close: closeAndReset } ) ] }) ] }) }) } ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CommandMenu, CommandMenuGroup, CommandMenuLoaderWrapper }); //# sourceMappingURL=command-menu.js.map