Skip to content

Commit

Permalink
feat: Toast component with Sonner
Browse files Browse the repository at this point in the history
  • Loading branch information
BrickheadJohnny committed Nov 25, 2024
1 parent 4e4d409 commit aaefa32
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react": "19.0.0-rc-66855b96-20241106",
"react-dom": "19.0.0-rc-66855b96-20241106",
"react-hook-form": "^7.53.2",
"sonner": "^1.7.0",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.1",
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "@/styles/globals.css";
import { Header } from "@/components/Header";
import { Providers } from "@/components/Providers";
import { SignInDialog } from "@/components/SignInDialog";
import { Toaster } from "@/components/ui/Toaster";
import { dystopian, inter } from "@/lib/fonts";
import { cn } from "lib/cssUtils";

Expand Down Expand Up @@ -30,6 +31,7 @@ const RootLayout = ({

{/* TODO: maybe load this dynamically? */}
<SignInDialog />
<Toaster />
</Providers>
</body>
</html>
Expand Down
31 changes: 31 additions & 0 deletions src/components/ui/Toaster.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

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:
"group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg data-[button]:bg-button-primary-background gap-2",
description: "group-[.toast]:text-foreground-secondary",
actionButton:
"group-[.toast]:!bg-button-primary-background group-[.toast]:!text-button-primary-foreground",
closeButton:
"group-[.toast]:bg-background group-[.toast]:text-foreground group-[.toast]:border-border",
},
}}
{...props}
/>
);
};

export { Toaster };
56 changes: 56 additions & 0 deletions src/stories/Toast.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Button } from "@/components/ui/Button";
import { Toaster } from "@/components/ui/Toaster";
import type { Meta, StoryObj } from "@storybook/react";
import { toast } from "sonner";

type ToastExampleProps = {
title: string;
description?: string;
action?: boolean;
closeButton?: boolean;
};
const ToastExample = ({
title = "Toast example",
description,
action,
closeButton,
}: ToastExampleProps) => {
return (
<>
<Button
onClick={() =>
toast(title, {
description,
action: action ? (
<Button size="xs" className="ml-auto">
Action
</Button>
) : undefined,
closeButton,
})
}
>
Show toast!
</Button>
<Toaster />
</>
);
};

const meta: Meta<typeof ToastExample> = {
title: "Design system/Toast",
component: ToastExample,
};

export default meta;

type Story = StoryObj<typeof ToastExample>;

export const Default: Story = {
args: {
title: "Toast example",
description: "",
action: false,
closeButton: false,
},
};

0 comments on commit aaefa32

Please sign in to comment.