ui: [bug]: The style overrides by shadcn for sonner are not working.

Describe the bug

image

From the screenshot, it is evident that the styles added by shadcn have a lower priority than the inherent styles of sonner , and therefore cannot override them correctly.

Affected component/components

Sonner

How to reproduce

  1. install shadcn/ui
style: default
baseColor: gray
cssVariable: false
  1. install sonner by npx shadcn-ui@latest add sonner
  2. use sonner in my project

Codesandbox/StackBlitz link

No response

Logs

No response

System Info

System: macOS 14.2.1
Browser: Arc Version 1.39.0 (48951) / Chromium Engine Version 124.0.6367.61

Before submitting

  • I’ve made research efforts and searched the documentation
  • I’ve searched for existing issues

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 4
  • Comments: 33 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Make sure when you import Toaster in your project you import it from your shadcn components and NOT the sonner lib, I acidently did it from the sonner lib and was wondering why it was not working.

Hey everyone,

Regarding needing to specify !important, the creator of sonner has confirmed that this is the expected behaviour.

That is expected and mentioned here. If you need to change a lot of styled you can either use the headless approach (recommended) or unstyled prop which will remove default styles.

Source:

As a workaround, you can use the “!” character before a tailwind class. Since the problem only affects the buttons (as far as I can see) you can use the following styles:

actionButton: 'group-[.toast]:!bg-primary group-[.toast]:!text-primary-foreground',
cancelButton: 'group-[.toast]:!bg-muted group-[.toast]:!text-muted-foreground',

Works in 1.5.0.

@anson0370 | cc: @skyfenton @sitenativedev @christian1koch @Phygon @code-xhyun

Might be some help here: I actually ended up modifying my sonner ui component file. Simply by adding a few lines to the toast Classname I was able to get some of the old feel back:

import { useTheme } from 'next-themes';
import { Toaster as Sonner } from 'sonner';

type ToasterProps = React.ComponentProps<typeof Sonner>;

const Toaster = ({ ...props }: ToasterProps) => {
  const { theme = 'system' } = useTheme();

  return (
    <Sonner
      theme={theme as ToasterProps['theme']}
      className="toaster group"
      toastOptions={{
        classNames: {
          toast:
            'h-auto w-full p-4 rounded-md border group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg',
          description: 'group-[.toast]:text-muted-foreground',
          actionButton:
            'group-[.toast]:bg-primary group-[.toast]:text-primary-foreground',
          cancelButton:
            'group-[.toast]:bg-muted group-[.toast]:text-muted-foreground',
        },
      }}
      {...props}
    />
  );
};

export { Toaster };

What changed:

  • classNames -> toast: add the following classes h-auto w-full p-4 rounded-md border

Its not perfect but it will get you some of the way there.

import “sonner/dist/styles.css”;

apply this on root file

After hundreds of tinkering with my config, this is basically the problem. For some reason, the sonner styles are not added by default. I tried importing it as you said, but its the “same”. My issue was a bit odd. I am in v2. The height was super collapsed, css was nowhere. What I did, is to import sonners css in my css file (copy and paste from repo, nothing worked for some reason).

import “sonner/dist/styles.css”;

apply this on root file

Sonner in version 1.7.0 provides a dist/styles.css.

When I include that in my app like this in one of JS files, the styling is correct:

import '../node_modules/sonner/dist/styles.css';

<Toaster richColors />

Need to add prop richColors

Modifying this component at all is such a fucking mess it goes against the original idea of shadcn. I have no idea who on their end thought this was alright.