@base-ui/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.
84 lines (83 loc) • 2.34 kB
JavaScript
'use client';
import * as React from 'react';
import { EMPTY_ARRAY, EMPTY_OBJECT } from '@base-ui/utils/empty';
import { CompositeList } from "../list/CompositeList.mjs";
import { useCompositeRoot } from "./useCompositeRoot.mjs";
import { CompositeRootContext } from "./CompositeRootContext.mjs";
import { useRenderElement } from "../../useRenderElement.mjs";
import { useDirection } from "../../direction-context/DirectionContext.mjs";
import { jsx as _jsx } from "react/jsx-runtime";
/**
* @internal
*/
export function CompositeRoot(componentProps) {
const {
render,
className,
style,
refs = EMPTY_ARRAY,
props = EMPTY_ARRAY,
state = EMPTY_OBJECT,
stateAttributesMapping,
highlightedIndex: highlightedIndexProp,
onHighlightedIndexChange: onHighlightedIndexChangeProp,
orientation,
grid,
loopFocus,
onLoop,
enableHomeAndEndKeys,
onMapChange: onMapChangeProp,
stopEventPropagation = true,
rootRef,
disabledIndices,
modifierKeys,
highlightItemOnHover = false,
tag = 'div',
...elementProps
} = componentProps;
const direction = useDirection();
const {
props: defaultProps,
highlightedIndex,
onHighlightedIndexChange,
elementsRef,
onMapChange: onMapChangeUnwrapped,
relayKeyboardEvent
} = useCompositeRoot({
grid,
loopFocus,
onLoop,
orientation,
highlightedIndex: highlightedIndexProp,
onHighlightedIndexChange: onHighlightedIndexChangeProp,
rootRef,
stopEventPropagation,
enableHomeAndEndKeys,
direction,
disabledIndices,
modifierKeys
});
const element = useRenderElement(tag, componentProps, {
state,
ref: refs,
props: [defaultProps, ...props, elementProps],
stateAttributesMapping
});
const contextValue = React.useMemo(() => ({
highlightedIndex,
onHighlightedIndexChange,
highlightItemOnHover,
relayKeyboardEvent
}), [highlightedIndex, onHighlightedIndexChange, highlightItemOnHover, relayKeyboardEvent]);
return /*#__PURE__*/_jsx(CompositeRootContext.Provider, {
value: contextValue,
children: /*#__PURE__*/_jsx(CompositeList, {
elementsRef: elementsRef,
onMapChange: newMap => {
onMapChangeProp?.(newMap);
onMapChangeUnwrapped(newMap);
},
children: element
})
});
}