UNPKG

@wordpress/components

Version:
186 lines (184 loc) 7.29 kB
"use strict"; 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/components/src/autocomplete/autocompleter-ui.tsx var autocompleter_ui_exports = {}; __export(autocompleter_ui_exports, { getAutoCompleterUI: () => getAutoCompleterUI }); module.exports = __toCommonJS(autocompleter_ui_exports); var import_clsx = __toESM(require("clsx")); var import_element = require("@wordpress/element"); var import_rich_text = require("@wordpress/rich-text"); var import_compose = require("@wordpress/compose"); var import_a11y = require("@wordpress/a11y"); var import_i18n = require("@wordpress/i18n"); var import_get_default_use_items = __toESM(require("./get-default-use-items")); var import_button = __toESM(require("../button")); var import_popover = __toESM(require("../popover")); var import_visually_hidden = require("../visually-hidden"); var import_react_dom = require("react-dom"); var import_jsx_runtime = require("react/jsx-runtime"); function ListBox({ items, onSelect, selectedIndex, instanceId, listBoxId, className, Component = "div" }) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, { id: listBoxId, role: "listbox", className: "components-autocomplete__results", children: items.map((option, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, { id: `components-autocomplete-item-${instanceId}-${option.key}`, role: "option", __next40pxDefaultSize: true, "aria-selected": index === selectedIndex, accessibleWhenDisabled: true, disabled: option.isDisabled, className: (0, import_clsx.default)("components-autocomplete__result", className, { // Unused, for backwards compatibility. "is-selected": index === selectedIndex }), variant: index === selectedIndex ? "primary" : void 0, onClick: () => onSelect(option), children: option.label }, option.key)) }); } function getAutoCompleterUI(autocompleter) { var _autocompleter$useIte; const useItems = (_autocompleter$useIte = autocompleter.useItems) !== null && _autocompleter$useIte !== void 0 ? _autocompleter$useIte : (0, import_get_default_use_items.default)(autocompleter); function AutocompleterUI({ filterValue, instanceId, listBoxId, className, selectedIndex, onChangeOptions, onSelect, onReset, reset, contentRef }) { const [items] = useItems(filterValue); const popoverAnchor = (0, import_rich_text.useAnchor)({ editableContentElement: contentRef.current }); const [needsA11yCompat, setNeedsA11yCompat] = (0, import_element.useState)(false); const popoverRef = (0, import_element.useRef)(null); const popoverRefs = (0, import_compose.useMergeRefs)([popoverRef, (0, import_compose.useRefEffect)((node) => { if (!contentRef.current) { return; } setNeedsA11yCompat(node.ownerDocument !== contentRef.current.ownerDocument); }, [contentRef])]); useOnClickOutside(popoverRef, reset); const debouncedSpeak = (0, import_compose.useDebounce)(import_a11y.speak, 500); function announce(options) { if (!debouncedSpeak) { return; } if (!!options.length) { if (filterValue) { debouncedSpeak((0, import_i18n.sprintf)( /* translators: %d: number of results. */ (0, import_i18n._n)("%d result found, use up and down arrow keys to navigate.", "%d results found, use up and down arrow keys to navigate.", options.length), options.length ), "assertive"); } else { debouncedSpeak((0, import_i18n.sprintf)( /* translators: %d: number of results. */ (0, import_i18n._n)("Initial %d result loaded. Type to filter all available results. Use up and down arrow keys to navigate.", "Initial %d results loaded. Type to filter all available results. Use up and down arrow keys to navigate.", options.length), options.length ), "assertive"); } } else { debouncedSpeak((0, import_i18n.__)("No results."), "assertive"); } } (0, import_element.useLayoutEffect)(() => { onChangeOptions(items); announce(items); }, [items]); if (items.length === 0) { return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_popover.default, { focusOnMount: false, onClose: onReset, placement: "top-start", className: "components-autocomplete__popover", anchor: popoverAnchor, ref: popoverRefs, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ListBox, { items, onSelect, selectedIndex, instanceId, listBoxId, className }) }), contentRef.current && needsA11yCompat && (0, import_react_dom.createPortal)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ListBox, { items, onSelect, selectedIndex, instanceId, listBoxId, className, Component: import_visually_hidden.VisuallyHidden }), contentRef.current.ownerDocument.body)] }); } return AutocompleterUI; } function useOnClickOutside(ref, handler) { (0, import_element.useEffect)(() => { const listener = (event) => { if (!ref.current || ref.current.contains(event.target)) { return; } handler(event); }; document.addEventListener("mousedown", listener); document.addEventListener("touchstart", listener); return () => { document.removeEventListener("mousedown", listener); document.removeEventListener("touchstart", listener); }; }, [handler, ref]); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getAutoCompleterUI }); //# sourceMappingURL=autocompleter-ui.js.map