UNPKG

@1771technologies/lytenyte-pro

Version:

150 lines (149 loc) 4.52 kB
import * as React from "react"; import { d as useEnhancedEffect, P as PropTypes } from "./proptypes-BjYr2nFr.js"; import { jsx } from "react/jsx-runtime"; const CompositeListContext = /* @__PURE__ */ React.createContext({ register: () => { }, unregister: () => { }, map: /* @__PURE__ */ new Map(), elementsRef: { current: [] } }); if (process.env.NODE_ENV !== "production") { CompositeListContext.displayName = "CompositeListContext"; } function useCompositeListContext() { return React.useContext(CompositeListContext); } function useCompositeListItem(params = {}) { const { label, metadata } = params; const { register, unregister, map, elementsRef, labelsRef } = useCompositeListContext(); const [index, setIndex] = React.useState(null); const componentRef = React.useRef(null); const ref = React.useCallback((node) => { componentRef.current = node; if (index !== null) { elementsRef.current[index] = node; if (labelsRef) { const isLabelDefined = label !== void 0; labelsRef.current[index] = isLabelDefined ? label : node?.textContent ?? null; } } }, [index, elementsRef, labelsRef, label]); useEnhancedEffect(() => { const node = componentRef.current; if (node) { register(node, metadata); return () => { unregister(node); }; } return void 0; }, [register, unregister, metadata]); useEnhancedEffect(() => { const i = componentRef.current ? map.get(componentRef.current)?.index : null; if (i != null) { setIndex(i); } }, [map]); return React.useMemo(() => ({ ref, index: index == null ? -1 : index }), [index, ref]); } function sortByDocumentPosition(a, b) { const position = a.compareDocumentPosition(b); if (position & Node.DOCUMENT_POSITION_FOLLOWING || position & Node.DOCUMENT_POSITION_CONTAINED_BY) { return -1; } if (position & Node.DOCUMENT_POSITION_PRECEDING || position & Node.DOCUMENT_POSITION_CONTAINS) { return 1; } return 0; } function CompositeList(props) { const { children, elementsRef, labelsRef, onMapChange } = props; const [map, setMap] = React.useState(() => /* @__PURE__ */ new Map()); const register = React.useCallback((node, metadata) => { setMap((prevMap) => new Map(prevMap).set(node, metadata ?? null)); }, []); const unregister = React.useCallback((node) => { setMap((prevMap) => { const nextMap = new Map(prevMap); nextMap.delete(node); return nextMap; }); }, []); const sortedMap = React.useMemo(() => { const newMap = /* @__PURE__ */ new Map(); const sortedNodes = Array.from(map.keys()).sort(sortByDocumentPosition); sortedNodes.forEach((node, index) => { const metadata = map.get(node) ?? {}; newMap.set(node, { ...metadata, index }); }); return newMap; }, [map]); useEnhancedEffect(() => { onMapChange?.(sortedMap); }, [sortedMap, onMapChange]); const contextValue = React.useMemo(() => ({ register, unregister, map: sortedMap, elementsRef, labelsRef }), [register, unregister, sortedMap, elementsRef, labelsRef]); return /* @__PURE__ */ jsx(CompositeListContext.Provider, { value: contextValue, children }); } process.env.NODE_ENV !== "production" ? CompositeList.propTypes = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @ignore */ children: PropTypes.node, /** * A ref to the list of HTML elements, ordered by their index. * `useListNavigation`'s `listRef` prop. */ elementsRef: PropTypes.any, /** * A ref to the list of element labels, ordered by their index. * `useTypeahead`'s `listRef` prop. */ labelsRef: PropTypes.shape({ current: PropTypes.arrayOf(PropTypes.string).isRequired }), /** * @ignore */ onMapChange: PropTypes.func } : void 0; export { CompositeList as C, useCompositeListItem as u };