hsuanyi-chou / shadcn-ui-expansions

More components built on top of shadcn-ui.
https://shadcnui-expansions.typeart.cc/
MIT License
1.1k stars 51 forks source link

LoadingButton fails when using asChild #25

Closed lok225 closed 9 months ago

lok225 commented 9 months ago

When implementing the LoadingButton with the asChild prop, you get the following error. I guess the current implementation negates the Slot implementation when using asChild:

image

const LoadingButton = React.forwardRef<HTMLButtonElement, ButtonProps>( ({ className, variant, size, asChild = false, loading, children, ...props }, ref) => { const Comp = asChild ? Slot : 'button'; return ( <Comp className={cn(buttonVariants({ variant, size, className }))} disabled={loading} ref={ref} {...props}

{loading && <Loader2 className={cn('h-4 w-4 animate-spin', children && 'mr-2')} />} {children} ); }, ); LoadingButton.displayName = 'LoadingButton';

hsuanyi-chou commented 9 months ago

It's fixed. Missing fragment.

Thanks!

Following is a snippet so that you don't have to copy it again.

      <Comp
        className={cn(buttonVariants({ variant, size, className }))}
        disabled={loading}
        ref={ref}
        {...props}
      >
+        <>
          {loading && <Loader2 className={cn('h-4 w-4 animate-spin', children && 'mr-2')} />}
          {children}
+        </>
      </Comp>
Yhprum commented 6 months ago

Adding this fragment breaks styles when actually using asChild with NextJS Link for me.

<Button variant="outline" asChild className="ml-auto gap-1">
  <Link href="/">
    <Plus />
    New
  </Link>
</Button>

the button does not have outline styles nor classname styles

hsuanyi-chou commented 6 months ago

@Yhprum fix it. please copy the loading-button.tsx again.

demo

Yhprum commented 6 months ago

thanks you! works great