@wordpress/components
Version:
UI components for WordPress.
101 lines (100 loc) • 4.19 kB
JavaScript
import clsx from "clsx";
import { createPortal } from "react-dom";
import { useEffect, useLayoutEffect, useRef, useState } from "@wordpress/element";
import { useAnchor } from "@wordpress/rich-text";
import { useDebounce, useMergeRefs, useRefEffect } from "@wordpress/compose";
import { speak } from "@wordpress/a11y";
import { __, _n, sprintf } from "@wordpress/i18n";
import getDefaultUseItems from "./get-default-use-items.mjs";
import Button from "../button/index.mjs";
import Popover from "../popover/index.mjs";
import { VisuallyHidden } from "../visually-hidden/index.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/autocomplete/autocompleter-ui.tsx
function ListBox({ items, onSelect, onHover, selectedIndex, instanceId, listBoxId, className, Component = "div" }) {
return /* @__PURE__ */ jsx(Component, {
id: listBoxId,
role: "listbox",
className: "components-autocomplete__results",
children: items.map((option, index) => /* @__PURE__ */ jsx(Button, {
id: `components-autocomplete-item-${instanceId}-${option.key}`,
role: "option",
__next40pxDefaultSize: true,
"aria-selected": index === selectedIndex,
accessibleWhenDisabled: true,
disabled: option.isDisabled,
className: clsx("components-autocomplete__result", className, { "is-selected": index === selectedIndex }),
onClick: () => onSelect(option),
onMouseMove: () => {
if (index !== selectedIndex) onHover(index);
},
children: option.label
}, option.key))
});
}
function AutocompleterUI({ autocompleter, filterValue, instanceId, listBoxId, className, selectedIndex, onChangeOptions, onSelect, onHover, reset, contentRef }) {
const [items] = (autocompleter.useItems ?? getDefaultUseItems(autocompleter))(filterValue);
const popoverAnchor = useAnchor({ editableContentElement: contentRef.current });
const [needsA11yCompat, setNeedsA11yCompat] = useState(false);
const popoverRef = useRef(null);
const popoverRefs = useMergeRefs([popoverRef, useRefEffect((node) => {
if (!contentRef.current) return;
setNeedsA11yCompat(node.ownerDocument !== contentRef.current.ownerDocument);
}, [contentRef])]);
useOnClickOutside(popoverRef, reset);
const debouncedSpeak = useDebounce(speak, 500);
function announce(options) {
if (!debouncedSpeak) return;
if (!!options.length) {
if (filterValue) debouncedSpeak(sprintf(_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(sprintf(_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(__("No results."), "assertive");
}
useLayoutEffect(() => {
onChangeOptions(items);
announce(items);
}, [items]);
if (items.length === 0) return null;
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Popover, {
offset: 8,
focusOnMount: false,
placement: "top-start",
className: "components-autocomplete__popover",
anchor: popoverAnchor,
ref: popoverRefs,
children: /* @__PURE__ */ jsx(ListBox, {
items,
onSelect,
onHover,
selectedIndex,
instanceId,
listBoxId,
className
})
}), contentRef.current && needsA11yCompat && createPortal(/* @__PURE__ */ jsx(ListBox, {
items,
onSelect,
onHover,
selectedIndex,
instanceId,
listBoxId,
className,
Component: VisuallyHidden
}), contentRef.current.ownerDocument.body)] });
}
function useOnClickOutside(ref, handler) {
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]);
}
export { AutocompleterUI };
//# sourceMappingURL=autocompleter-ui.mjs.map