ui icon indicating copy to clipboard operation
ui copied to clipboard

Add position prop to toaster

Open minjie0501 opened this issue 2 years ago • 5 comments

Any plans on adding a 'position' prop that could be passed when using the useToast hook? Something similar to react-hot-toast ⬇️ toast('Hello World', { position: 'top-center', });

minjie0501 avatar Mar 18 '23 21:03 minjie0501

You mean on a per toast basis. You generally want all your toasts to always be from one position.

If you want to switch to top center, you can do this by changing the classes here: https://github.com/shadcn/ui/blob/faf05aa086c27d1d7a021cb716768ac2e1261f65/apps/www/components/ui/toast.tsx#L17

- fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:top-auto sm:bottom-0 sm:right-0 sm:flex-col md:max-w-[420px]
+ fixed top-0 left-[50%] z-[100] flex max-h-screen w-full translate-x-[-50%] flex-col-reverse p-4 sm:right-0 sm:flex-col md:max-w-[420px]

This will position the toast to the top center. (Might need some animations adjustments)

shadcn avatar Mar 20 '23 07:03 shadcn

Yes I do want to do it on a per toast basis. I agree that it should appear in the same position in most cases but I do have a use case where I would like it to appear in a different place for one of the toasts.

minjie0501 avatar Mar 20 '23 09:03 minjie0501

One time one toast not enough, showing multiple toasts is the common senario

hiql avatar Mar 21 '23 13:03 hiql

@hiql You can do this using TOAST_LIMIT.

YxK1EKJu

shadcn avatar Mar 21 '23 13:03 shadcn

react-hot-toast API has id prop. It allows to create multiple toasts without duplication.

Are you planning to update Toast component to have that feature?

See: https://react-hot-toast.com/docs/toast @: Prevent duplicate toasts

AdisonCavani avatar Mar 21 '23 15:03 AdisonCavani

Hi, any update on this?

Ryuk-hash avatar Sep 07 '23 05:09 Ryuk-hash

We can customize the position using CSS classes because in the component the ClassName overwrites the original properties This is designed for customization, that being said, we can follow the code below

<Button
  variant={'default'}
  onClick={() => {
    toast({
      className: cn(
        'top-0 right-0 flex fixed md:max-w-[420px] md:top-4 md:right-4'
      ),
      variant: 'default',
      title: 'Uh oh! Something went wrong.',
      description: 'There was a problem with your request.',
      action: <ToastAction altText="Try again">Try again</ToastAction>
    })
  }}
>
  Save
</Button>

image

charleston10 avatar Sep 13 '23 12:09 charleston10

For Top-Right, just remove sm:top-auto sm:bottom-0 from the Shadcn toast.tsx. So the result would be:

"fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4  sm:right-0  sm:flex-col md:max-w-[420px]",

The above cn function is defined here:

import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

AuroraLantean avatar Sep 25 '23 00:09 AuroraLantean

For Top-Right, just remove sm:top-auto sm:bottom-0 from the Shadcn toast.tsx. So the result would be:

"fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4  sm:right-0  sm:flex-col md:max-w-[420px]",

I found that it is also a good idea to change data-[state=open]:sm:slide-in-from-bottom-full to data-[state=open]:sm:slide-in-from-top-full for a nicer animation.

goktugerce avatar Feb 22 '24 19:02 goktugerce