import { mergeProps, mergeRefs, useObjectRef } from "@react-aria/utils"; import { SlotProps, SlottedContextValue, useSlottedContext, } from "./useSlottedContext"; type WithRef = T & { ref?: React.Ref }; export type ContextValue = SlottedContextValue>; // from: https://github.com/adobe/react-spectrum/blob/c6bd2cb0808838a9f1f850b6c1ffe88465254222/packages/react-aria-components/src/utils.tsx#L180 /** Consume a value from a context & merge it with the provided props */ export function useContextProps( context: React.Context>, props: T, ref: React.Ref ): [T, React.RefObject] { const ctx = useSlottedContext(context, (props as SlotProps).slot) || {}; const { ref: contextRef, ...contextProps } = ctx as WithRef; const mergedRef = useObjectRef(mergeRefs(ref, contextRef!)); const mergedProps = mergeProps(contextProps, props) as T; return [mergedProps, mergedRef]; }