MCPcopy
hub / github.com/adobe/react-spectrum / useContextProps

Function useContextProps

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

Source from the content-addressed store, hash-verified

312}
313
314export function useContextProps<T, U extends SlotProps, E extends Element>(
315 props: T & SlotProps,
316 ref: ForwardedRef<E> | undefined,
317 context: Context<ContextValue<U, E>>
318): [T, RefObject<E | null>] {
319 let ctx = useSlottedContext(context, props.slot) || {};
320 let {ref: contextRef, ...contextProps} = ctx as any;
321 let mergedRef = useObjectRef(useMemo(() => mergeRefs(ref, contextRef), [ref, contextRef]));
322 let mergedProps = mergeProps(contextProps, props) as unknown as T;
323
324 // mergeProps does not merge `style`. Adding this there might be a breaking change.
325 if ('style' in contextProps && contextProps.style && 'style' in props && props.style) {
326 if (typeof contextProps.style === 'function' || typeof props.style === 'function') {
327 // @ts-ignore
328 mergedProps.style = renderProps => {
329 let contextStyle =
330 typeof contextProps.style === 'function'
331 ? contextProps.style(renderProps)
332 : contextProps.style;
333 let defaultStyle = {...renderProps.defaultStyle, ...contextStyle};
334 let style =
335 typeof props.style === 'function'
336 ? props.style({...renderProps, defaultStyle})
337 : props.style;
338 return {...defaultStyle, ...style};
339 };
340 } else {
341 // @ts-ignore
342 mergedProps.style = {...contextProps.style, ...props.style};
343 }
344 }
345
346 return [mergedProps, mergedRef];
347}
348
349export function useSlot(
350 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