sonner.tsx
Overview
The sonner.tsx file defines a React component named Toaster that serves as a styled wrapper around the Toaster component from the sonner notification library. This component integrates theme-awareness using the next-themes package to dynamically adjust its appearance based on the current UI theme (light, dark, or system). It applies custom styling that aligns the toast notifications with the overall design system of the application. The component facilitates displaying toast notifications with consistent theming and customized visual styles.
Detailed Breakdown
Imports
useThemefromnext-themes
Hook to detect and react to the current theme (light, dark, or system).Toaster as Sonner from
sonner
The base toast notification component from the Sonner library, renamed to avoid naming conflicts.
Type Aliases
type ToasterProps = React.ComponentProps<typeof Sonner>;
Purpose:
Extracts the prop types of the importedSonnercomponent to ensureToasteraccepts the exact same props and can pass them through seamlessly.
Component: Toaster
const Toaster = ({ ...props }: ToasterProps) => {
const { theme = 'system' } = useTheme();
return (
<Sonner
theme={theme as ToasterProps['theme']}
className="toaster group"
toastOptions={{
classNames: {
toast:
'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}
/>
);
};
Purpose
Wraps the
Sonnertoast component to:Automatically apply the theme based on the current app theme.
Provide consistent styling (class names) for different parts of the toast notification.
Allow additional props to be passed through to customize behavior.
Parameters
props: ToasterProps
All props available to the originalSonnercomponent can be passed here, such asposition,richColors,duration, and event handlers.
Return Value
Returns a React element that renders the themed and styled
Sonnertoast container.
Usage Example
import { Toaster } from './sonner';
// To display toast notifications globally in your app
function App() {
return (
<>
{/* Other app components */}
<Toaster position="bottom-right" />
</>
);
}
Important Implementation Details
Theming Integration:
The component uses theuseThemehook fromnext-themesto detect the current theme. This value is passed to theSonnercomponent'sthemeprop, ensuring the toast notifications adapt automatically to light, dark, or system preferences.Class Names with Group Selectors:
The toast options apply Tailwind CSS utility classes with group selectors (group-[.toaster],group-[.toast]). This approach scopes styles dynamically based on the toast's container and state, enabling flexible and contextual styling without inline styles.Props Forwarding:
TheToastercomponent spreads all other props into the innerSonnercomponent, making it fully customizable while maintaining default theming and styles.
Interaction with Other Parts of the System
Theme Provider:
The component depends on thenext-themesprovider being set up in the React app. Without the theme context, it falls back to'system'theme.Sonner Library:
This file acts as a bridge, enhancing thesonnerToaster by adding app-specific theming and styling conventions.Application UI:
Typically, thisToastercomponent is included once at a high level in the app (e.g., in the root layout or main app component) to display toast notifications triggered anywhere.
Mermaid Diagram
This component file contains a single functional component wrapping a third-party component, so a component diagram illustrating the structure and prop flow is appropriate.
componentDiagram
component Toaster {
+props: ToasterProps
+theme: 'light' | 'dark' | 'system'
}
component Sonner {
+theme: string
+className: string
+toastOptions: object
+otherProps: any
}
Toaster --> Sonner : wraps and forwards props
Toaster ..> useTheme : uses current theme
Summary
The sonner.tsx file provides a reusable, theme-aware toast notification component for a React/Next.js application. By encapsulating the third-party Sonner Toaster and integrating it with the app's theme context, it ensures consistent and customizable user feedback notifications that adapt visually based on the app's theme settings. The use of scoped Tailwind CSS classes further enhances styling flexibility while keeping the API simple and extensible. This component is intended to be included once per app to manage toast notifications globally.