MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useContextProps

Function useContextProps

packages/react-aria-components/src/utils.tsx:329–362  ·  view source on GitHub ↗
(
  props: T & SlotProps,
  ref: ForwardedRef<E> | undefined,
  context: Context<ContextValue<U, E>>
)

Source from the content-addressed store, hash-verified

327}
328
329export function useContextProps<T, U extends SlotProps, E extends Element>(
330 props: T & SlotProps,
331 ref: ForwardedRef<E> | undefined,
332 context: Context<ContextValue<U, E>>
333): [T, RefObject<E | null>] {
334 let ctx = useSlottedContext(context, props.slot) || {};
335 let {ref: contextRef, ...contextProps} = ctx as any;
336 let mergedRef = useObjectRef(useMemo(() => mergeRefs(ref, contextRef), [ref, contextRef]));
337 let mergedProps = mergeProps(contextProps, props) as unknown as T;
338
339 // mergeProps does not merge `style`. Adding this there might be a breaking change.
340 if ('style' in contextProps && contextProps.style && 'style' in props && props.style) {
341 if (typeof contextProps.style === 'function' || typeof props.style === 'function') {
342 // @ts-ignore
343 mergedProps.style = renderProps => {
344 let contextStyle =
345 typeof contextProps.style === 'function'
346 ? contextProps.style(renderProps)
347 : contextProps.style;
348 let defaultStyle = {...renderProps.defaultStyle, ...contextStyle};
349 let style =
350 typeof props.style === 'function'
351 ? props.style({...renderProps, defaultStyle})
352 : props.style;
353 return {...defaultStyle, ...style};
354 };
355 } else {
356 // @ts-ignore
357 mergedProps.style = {...contextProps.style, ...props.style};
358 }
359 }
360
361 return [mergedProps, mergedRef];
362}
363
364export function useSlot(
365 initialState: boolean | (() => boolean) = true

Callers 15

Toast.tsxFile · 0.90
Label.tsxFile · 0.90
Disclosure.tsxFile · 0.90
Popover.tsxFile · 0.90
Button.tsxFile · 0.90
Tooltip.tsxFile · 0.90
Keyboard.tsxFile · 0.90
Tabs.tsxFile · 0.90
ListBox.tsxFile · 0.90
ListBoxInnerFunction · 0.90

Calls 5

useObjectRefFunction · 0.90
mergeRefsFunction · 0.90
mergePropsFunction · 0.90
useSlottedContextFunction · 0.85
styleMethod · 0.80

Tested by

no test coverage detected