Top Related Projects
Chakra UI is a component system for building SaaS products with speed ⚡️
A set of beautifully-designed, accessible components and a code distribution platform. Works with your favorite frameworks. Open Source. Open Code.
Radix Primitives is an open-source UI component library for building high-quality, accessible design systems and web apps. Maintained by @workos.
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
React notification made easy 🚀 !
Quick Overview
Sonner is a lightweight, customizable toast component for React applications. It provides an easy way to display notifications or messages to users with a clean and modern design. The library is designed to be simple to use while offering flexibility for customization.
Pros
- Lightweight and performant, with minimal dependencies
- Highly customizable, allowing for easy theming and styling
- Supports rich content, including custom React components
- Offers various positioning options and animation effects
Cons
- Limited to React applications, not usable in other frameworks
- Lacks some advanced features found in more comprehensive notification libraries
- Documentation could be more extensive, especially for advanced use cases
- May require additional setup for TypeScript users
Code Examples
Creating a basic toast:
import { toast } from 'sonner'
toast('My first toast')
Displaying a toast with custom options:
toast('Custom toast', {
description: 'This is a more detailed message',
icon: '👋',
duration: 5000,
position: 'top-center'
})
Using different toast types:
toast.success('Operation successful')
toast.error('An error occurred')
toast.loading('Processing...')
Getting Started
- Install the package:
npm install sonner
- Import and use the
Toaster
component in your app:
import { Toaster } from 'sonner'
function App() {
return (
<div>
{/* Your app content */}
<Toaster />
</div>
)
}
- Use the
toast
function to display toasts:
import { toast } from 'sonner'
function MyComponent() {
const handleClick = () => {
toast('Hello, World!')
}
return <button onClick={handleClick}>Show Toast</button>
}
Competitor Comparisons
Chakra UI is a component system for building SaaS products with speed ⚡️
Pros of Chakra UI
- Comprehensive component library with a wide range of pre-built UI elements
- Highly customizable with a robust theming system
- Excellent accessibility features built-in
Cons of Chakra UI
- Larger bundle size due to its extensive feature set
- Steeper learning curve for beginners
- May require more setup and configuration
Code Comparison
Chakra UI example:
import { Button, Box } from "@chakra-ui/react"
function Example() {
return (
<Box>
<Button colorScheme="blue">Click me</Button>
</Box>
)
}
Sonner example:
import { Toaster, toast } from 'sonner'
function Example() {
return (
<div>
<Toaster />
<button onClick={() => toast('My first toast')}>
Create a toast
</button>
</div>
)
}
Summary
Chakra UI is a comprehensive UI library offering a wide range of components and customization options, while Sonner is focused specifically on toast notifications. Chakra UI provides more flexibility but comes with a larger footprint and learning curve. Sonner offers a simpler, more targeted solution for toast notifications with a smaller bundle size and easier integration.
A set of beautifully-designed, accessible components and a code distribution platform. Works with your favorite frameworks. Open Source. Open Code.
Pros of ui
- More comprehensive UI component library with a wider range of components
- Highly customizable and themeable, allowing for greater flexibility in design
- Follows a modular approach, enabling developers to use only the components they need
Cons of ui
- Steeper learning curve due to its more complex architecture and customization options
- Potentially larger bundle size if not properly optimized or tree-shaken
Code Comparison
ui:
import * as React from "react"
import { Button } from "@/components/ui/button"
export function ButtonDemo() {
return <Button>Click me</Button>
}
sonner:
import { Toaster, toast } from 'sonner'
export default function App() {
return (
<div>
<Toaster />
<button onClick={() => toast('My first toast')}>
Give me a toast
</button>
</div>
)
}
Summary
ui offers a more extensive set of UI components with greater customization options, making it suitable for larger projects with diverse UI needs. However, it may require more time to learn and implement effectively. sonner, on the other hand, focuses specifically on toast notifications, providing a simpler and more straightforward implementation for this particular use case.
Radix Primitives is an open-source UI component library for building high-quality, accessible design systems and web apps. Maintained by @workos.
Pros of Primitives
- Comprehensive set of unstyled, accessible UI components
- Highly customizable and flexible for various design systems
- Strong focus on accessibility and keyboard navigation
Cons of Primitives
- Steeper learning curve due to its extensive API
- Requires more setup and configuration for basic use cases
Code Comparison
Primitives (Dialog component):
import * as Dialog from '@radix-ui/react-dialog';
<Dialog.Root>
<Dialog.Trigger>Open</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay />
<Dialog.Content>
<Dialog.Title>Dialog Title</Dialog.Title>
<Dialog.Description>Dialog content here</Dialog.Description>
<Dialog.Close>Close</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
Sonner (Toast component):
import { Toaster, toast } from 'sonner';
<Toaster />
<button onClick={() => toast('My first toast')}>
Create a toast
</button>
Summary
Primitives offers a comprehensive set of UI components with a strong focus on accessibility and customization. It's ideal for complex projects requiring fine-grained control over components. Sonner, on the other hand, provides a simpler, more straightforward approach to creating toast notifications, making it easier to implement basic use cases quickly. The choice between the two depends on the project's complexity and specific requirements.
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
Pros of Headless UI
- More comprehensive UI component library with a wider range of components
- Fully accessible and follows WAI-ARIA guidelines
- Highly customizable and framework-agnostic
Cons of Headless UI
- Steeper learning curve due to its headless nature
- Requires more setup and styling work compared to Sonner's ready-to-use components
- Larger bundle size due to its extensive feature set
Code Comparison
Sonner (Toast component):
import { Toaster, toast } from 'sonner'
function App() {
return (
<div>
<Toaster />
<button onClick={() => toast('My first toast')}>Give me a toast</button>
</div>
)
}
Headless UI (Menu component):
import { Menu } from '@headlessui/react'
function MyDropdown() {
return (
<Menu>
<Menu.Button>More</Menu.Button>
<Menu.Items>
<Menu.Item>
{({ active }) => (
<a className={`${active && 'bg-blue-500'}`} href="/account-settings">
Account settings
</a>
)}
</Menu.Item>
</Menu.Items>
</Menu>
)
}
React notification made easy 🚀 !
Pros of react-toastify
- More feature-rich with extensive customization options
- Supports multiple toast types (success, error, info, etc.)
- Larger community and more frequent updates
Cons of react-toastify
- Larger bundle size due to more features
- Steeper learning curve for advanced customizations
- More complex API compared to Sonner's simplicity
Code Comparison
react-toastify:
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
toast.success('Success message');
<ToastContainer position="top-right" autoClose={3000} />
Sonner:
import { Toaster, toast } from 'sonner';
toast('My first toast');
<Toaster />
Summary
react-toastify offers more features and customization options, making it suitable for complex projects with diverse notification requirements. It has a larger community and more frequent updates, ensuring better long-term support.
However, this comes at the cost of a larger bundle size and a steeper learning curve. The API is more complex compared to Sonner's straightforward approach.
Sonner, on the other hand, provides a simpler and more lightweight solution. It's easier to set up and use, making it ideal for projects that require basic toast functionality without the need for extensive customization.
The code comparison demonstrates the simplicity of Sonner's implementation compared to react-toastify's more verbose setup. While react-toastify requires importing styles and offers more configuration options, Sonner provides a more streamlined approach out of the box.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
https://github.com/vallezw/sonner/assets/50796600/59b95cb7-9068-4f3e-8469-0b35d9de5cf0
Sonner is an opinionated toast component for React. You can read more about why and how it was built here.
Usage
To start using the library, install it in your project:
npm install sonner
Add <Toaster />
to your app, it will be the place where all your toasts will be rendered.
After that you can use toast()
from anywhere in your app.
import { Toaster, toast } from 'sonner';
// ...
function App() {
return (
<div>
<Toaster />
<button onClick={() => toast('My first toast')}>Give me a toast</button>
</div>
);
}
Documentation
Find the full API reference in the documentation.
Top Related Projects
Chakra UI is a component system for building SaaS products with speed ⚡️
A set of beautifully-designed, accessible components and a code distribution platform. Works with your favorite frameworks. Open Source. Open Code.
Radix Primitives is an open-source UI component library for building high-quality, accessible design systems and web apps. Maintained by @workos.
Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
React notification made easy 🚀 !
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot