@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
79 lines (77 loc) • 2.4 kB
JavaScript
'use client';
import * as React from 'react';
import { useIsoLayoutEffect } from '@base-ui-components/utils/useIsoLayoutEffect';
import { useCompositeListContext } from "./CompositeListContext.js";
export let IndexGuessBehavior = /*#__PURE__*/function (IndexGuessBehavior) {
IndexGuessBehavior[IndexGuessBehavior["None"] = 0] = "None";
IndexGuessBehavior[IndexGuessBehavior["GuessFromOrder"] = 1] = "GuessFromOrder";
return IndexGuessBehavior;
}({});
/**
* Used to register a list item and its index (DOM position) in the `CompositeList`.
*/
export function useCompositeListItem(params = {}) {
const {
label,
metadata,
textRef,
indexGuessBehavior,
index: externalIndex
} = params;
const {
register,
unregister,
subscribeMapChange,
elementsRef,
labelsRef,
nextIndexRef
} = useCompositeListContext();
const indexRef = React.useRef(-1);
const [index, setIndex] = React.useState(externalIndex ?? (indexGuessBehavior === IndexGuessBehavior.GuessFromOrder ? () => {
if (indexRef.current === -1) {
const newIndex = nextIndexRef.current;
nextIndexRef.current += 1;
indexRef.current = newIndex;
}
return indexRef.current;
} : -1));
const componentRef = React.useRef(null);
const ref = React.useCallback(node => {
componentRef.current = node;
if (index !== -1 && node !== null) {
elementsRef.current[index] = node;
if (labelsRef) {
const isLabelDefined = label !== undefined;
labelsRef.current[index] = isLabelDefined ? label : textRef?.current?.textContent ?? node.textContent;
}
}
}, [index, elementsRef, labelsRef, label, textRef]);
useIsoLayoutEffect(() => {
if (externalIndex != null) {
return undefined;
}
const node = componentRef.current;
if (node) {
register(node, metadata);
return () => {
unregister(node);
};
}
return undefined;
}, [externalIndex, register, unregister, metadata]);
useIsoLayoutEffect(() => {
if (externalIndex != null) {
return undefined;
}
return subscribeMapChange(map => {
const i = componentRef.current ? map.get(componentRef.current)?.index : null;
if (i != null) {
setIndex(i);
}
});
}, [externalIndex, subscribeMapChange, setIndex]);
return React.useMemo(() => ({
ref,
index
}), [index, ref]);
}