MCPcopy
hub / github.com/shadcn-ui/ui / useRender

Function useRender

packages/react/src/use-render/index.ts:41–82  ·  view source on GitHub ↗
({
  defaultTagName,
  props,
  render,
  state = {} as TState,
  stateAttributesMapping,
}: UseRenderOptions<TElement, TState>)

Source from the content-addressed store, hash-verified

39}
40
41function useRender<
42 TElement extends React.ElementType,
43 TState extends RenderState = RenderState,
44>({
45 defaultTagName,
46 props,
47 render,
48 state = {} as TState,
49 stateAttributesMapping,
50}: UseRenderOptions<TElement, TState>) {
51 const elementProps = mergeProps<TElement>(
52 getStateAttributes(state, stateAttributesMapping),
53 props
54 )
55
56 if (!render) {
57 return React.createElement(defaultTagName, elementProps)
58 }
59
60 if (typeof render === "function") {
61 return render(elementProps, state)
62 }
63
64 if (!React.isValidElement(render)) {
65 return null
66 }
67
68 const renderProps = render.props as Record<string, unknown>
69 const propsWithRenderProps = mergeProps<TElement>(elementProps, renderProps)
70 const propsWithRef = {
71 ...propsWithRenderProps,
72 ref: composeRefs(
73 elementProps.ref as React.Ref<unknown> | undefined,
74 renderProps.ref as React.Ref<unknown> | undefined
75 ),
76 }
77
78 return React.cloneElement(
79 render,
80 propsWithRef as React.Attributes & Record<string, unknown>
81 )
82}
83
84function mergeProps<TElement extends React.ElementType>(
85 ...sources: Array<

Callers 15

MessageScrollerButtonFunction · 0.90
BreadcrumbLinkFunction · 0.90
ButtonGroupTextFunction · 0.90
MarkerFunction · 0.90
BubbleContentFunction · 0.90
AttachmentTriggerFunction · 0.90
ItemFunction · 0.90
SidebarGroupLabelFunction · 0.90
SidebarGroupActionFunction · 0.90
SidebarMenuButtonFunction · 0.90
SidebarMenuActionFunction · 0.90
SidebarMenuSubButtonFunction · 0.90

Calls 3

mergePropsFunction · 0.85
getStateAttributesFunction · 0.85
composeRefsFunction · 0.85

Tested by

no test coverage detected