ui icon indicating copy to clipboard operation
ui copied to clipboard

`toast` title does not accept `Element` type

Open MarkLyck opened this issue 1 year ago • 12 comments

It is quite common to add icons to toast messages such as "success" or "danger" variants.

However when trying to add an icon to the toast in shadcn UI it throws a typescript error saying it will only accept a string:

Type 'Element' is not assignable to type 'string | (string & ReactElement<any, string | JSXElementConstructor<any>>) | (string & Iterable<ReactNode>) | (string & ReactPortal) | undefined'.
  Type 'ReactElement<any, any>' is not assignable to type 'string & ReactPortal'.
    Type 'ReactElement<any, any>' is not assignable to type 'string’.

Despite this error, the code works fine and the icon is rendered in the title without issues. This is supported fine in Radix toast. But the accepted types seems to be incorrect? Or am I using it wrong?

Here's my code example:

toast({
        title: (
          <div className="flex items-center">
            <CheckIcon className="mr-2" />
            <span className="first-letter:capitalize">
              successfully updated ssid
            </span>
          </div>
        ),
      })

and this is what gets rendered:

Screenshot 2023-07-11 at 11 27 37

MarkLyck avatar Jul 11 '23 09:07 MarkLyck

Use action in the toast as it accepts elements. You may want to add w-full though.

    toast({
      action: (
        <div className="w-full flex items-center">
          <CheckIcon className="mr-2" />
          <span className="first-letter:capitalize">
            successfully updated
          </span>
        </div>
      ),
    });

limitless-dev avatar Jul 12 '23 20:07 limitless-dev

I've created PR in #1363 to resolve this issue. I hope the maintainer will see it at once.

fahmij8 avatar Aug 27 '23 15:08 fahmij8

Having the same problem and also wondering if I'm doing something wrong or not.

oncet avatar Oct 15 '23 02:10 oncet

Having the same problem and also wondering if I'm doing something wrong or not.

Hey since the PR is already there... and the fix is not hard just do it yourself. Thats the beauty of shadcn you have the source code in your poject.

Navigate to use-toast.ts and then replace the type with this: type ToasterToast = Omit<ToastProps, 'id' | 'title' | 'description' | 'action'> & { id: string title?: React.ReactNode description?: React.ReactNode action?: ToastActionElement }

Xentox-Phil avatar Nov 29 '23 23:11 Xentox-Phil

Having the same problem and also wondering if I'm doing something wrong or not.

Hey since the PR is already there... and the fix is not hard just do it yourself. Thats the beauty of shadcn you have the source code in your poject.

Navigate to use-toast.ts and then replace the type with this: type ToasterToast = Omit<ToastProps, 'id' | 'title' | 'description' | 'action'> & { id: string title?: React.ReactNode description?: React.ReactNode action?: ToastActionElement }

Yes, that's what I ended up doing! Thank you.

oncet avatar Nov 30 '23 10:11 oncet

@shadcn , any updates regarding this?

alamenai avatar Mar 06 '24 07:03 alamenai

yeah same, any updates for it?

AndonMitev avatar Mar 13 '24 09:03 AndonMitev

@Xentox-Phil's answer worked for me.

alamenai avatar Mar 13 '24 11:03 alamenai

Yup, to those who have this issue. You can refer to this answer right here https://github.com/shadcn-ui/ui/issues/896#issuecomment-1832842508

I'm closing my PR. Since it's not getting merged for a while. Maybe you can close the issue too @MarkLyck

fahmij8 avatar Mar 13 '24 11:03 fahmij8

I'm using the following implementation,

toast({
	description: 'Failed to create project',
	variant: 'destructive',
});

but what I want to make is

toast({
	description: (<someIcon /><p>Failed to create project<p>),
	variant: 'destructive',
});

whenever I try to use

I keep getting Cannot find name p, and suggestions

lxtadhamnawito avatar Mar 19 '24 12:03 lxtadhamnawito

I'm using the following implementation,

toast({
	description: 'Failed to create project',
	variant: 'destructive',
});

but what I want to make is

toast({
	description: (<someIcon /><p>Failed to create project<p>),
	variant: 'destructive',
});

whenever I try to use

I keep getting Cannot find name p, and suggestions

You have a typo in your closing tag, it should be </p>.

And you prob need to wrap it in a single component, you can use fragment:

<><icon /><p>Foo</p></>

oncet avatar Mar 19 '24 16:03 oncet

@oncet Thanks it was missing <></> the main thing. I appreciate your help

lxtadhamnawito avatar Mar 20 '24 12:03 lxtadhamnawito