Skip to content
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

A detail when pasting the button component #3

Closed
ivanosquis10 opened this issue Feb 5, 2024 · 3 comments · Fixed by #4
Closed

A detail when pasting the button component #3

ivanosquis10 opened this issue Feb 5, 2024 · 3 comments · Fixed by #4

Comments

@ivanosquis10
Copy link

ivanosquis10 commented Feb 5, 2024

First of all your idea is very cool and I love it. The situation is that when I paste the button component that you provide, I save and I get the following error, I have a project with a long and good development, I liked the idea and I wanted to implement some details in the buttons but I get this error...I will try to fix the error but I leave it here in case someone else has the same problem.
image

Updated: I think the problem is related to the prop "aschild".

@jakobhoeg
Copy link
Owner

Thank you for providing this information! I'll definitely look into it.

@thedevdavid
Copy link
Contributor

thedevdavid commented Feb 7, 2024

It's because the Slot component from Radix can only accept 1 child component and now it may have more than that.

const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
>
{Icon && iconPlacement === "left" && (
<div className="w-0 translate-x-[0%] pr-0 opacity-0 transition-all duration-200 group-hover:w-5 group-hover:translate-x-100 group-hover:pr-2 group-hover:opacity-100">
<Icon />
</div>
)}
{props.children}
{Icon && iconPlacement === "right" && (
<div className="w-0 translate-x-[100%] pl-0 opacity-0 transition-all duration-200 group-hover:w-5 group-hover:translate-x-0 group-hover:pl-2 group-hover:opacity-100">
<Icon />
</div>
)}
</Comp>
);

If you wrap everything inside <Comp> into a <React.Fragment>, it'll work. I've opened a PR #4

@jakobhoeg jakobhoeg linked a pull request Feb 7, 2024 that will close this issue
@thedevdavid
Copy link
Contributor

@jakobhoeg one thing to mention I just realized after making the PR: asChild prop does not work with Fragment.

So if the child is a <Link> from next.js for example, this won't work.

And it looks like an issue with Radix. radix-ui/primitives#1825

So probably better to reorganize the Button component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants