-
-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add asChild prop #138
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
cmdk/src/index.tsx
Outdated
{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> | ||
)} |
There was a problem hiding this comment.
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>
cmdk/src/index.tsx
Outdated
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) |
There was a problem hiding this comment.
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.
@pacocoursey anything blocking this PR? I'm interested in having #156 fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
This PR adds
asChild
prop to all componentsCloses #98