Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add asChild prop #138

Merged
merged 8 commits into from
Jan 30, 2024
Merged

feat: add asChild prop #138

merged 8 commits into from
Jan 30, 2024

Conversation

joaom00
Copy link
Contributor

@joaom00 joaom00 commented May 15, 2023

This PR adds asChild prop to all components

Closes #98

@vercel
Copy link

vercel bot commented May 15, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
cmdk-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 30, 2024 4:19am

Comment on lines 593 to 608
{props.asChild ? (
<Slottable>
{React.isValidElement(children) &&
React.cloneElement(
children,
undefined,
<StoreContext.Provider value={store}>
<CommandContext.Provider value={context}>{children.props.children}</CommandContext.Provider>
</StoreContext.Provider>,
)}
</Slottable>
) : (
<StoreContext.Provider value={store}>
<CommandContext.Provider value={context}>{children}</CommandContext.Provider>
</StoreContext.Provider>
)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do this in some components because Slot does not support when children are nested. For example:

If we have this component:

const Button = () => (
  <Slot>
    <div>
      {children}
    </div>
  </Slot>
)

and using like

<Button asChild>
  <a>
   Button
  </a>
</Button>

will result in

<div>
  <a>
   Button
  </a>
</div>

instead of

<a>
  <div>
   Button
  </div>
</a>

Comment on lines 1038 to 1049
const Primitive = NODES.reduce((primitive, node) => {
const Node = React.forwardRef((props: PrimitivePropsWithRef<typeof node>, forwardedRef: any) => {
const { asChild, ...primitiveProps } = props
const Comp: any = asChild ? Slot : node

return <Comp {...primitiveProps} ref={forwardedRef} />
})

Node.displayName = `Primitive.${node}`

return { ...primitive, [node]: Node }
}, {} as Primitives)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's bring in the @radix-ui/react-primitive package for this behavior instead, for better package de-duplication.

@xixixao
Copy link

xixixao commented Jan 14, 2024

@pacocoursey anything blocking this PR? I'm interested in having #156 fixed.

Copy link
Owner

@pacocoursey pacocoursey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@pacocoursey pacocoursey merged commit 071ee2d into pacocoursey:main Jan 30, 2024
1 of 3 checks passed
nklhtv added a commit to nklhtv/cmdk that referenced this pull request Mar 2, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add asChild for Item
3 participants