Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 41 additions & 65 deletions apps/webapp/app/components/primitives/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExclamationCircleIcon, XMarkIcon } from "@heroicons/react/20/solid";
import { CheckCircleIcon } from "@heroicons/react/24/solid";
import { AnimatePresence, motion } from "framer-motion";
import toast, { Toaster, resolveValue, useToasterStore } from "react-hot-toast";
import { Toaster, toast } from "sonner";

import { useTypedLoaderData } from "remix-typedjson";
import { loader } from "~/root";
import { useEffect } from "react";
Expand All @@ -11,79 +11,55 @@ const permanentToastDuration = 60 * 60 * 24 * 1000;

export function Toast() {
const { toastMessage } = useTypedLoaderData<typeof loader>();

useEffect(() => {
if (!toastMessage) {
return;
}
const { message, type, options } = toastMessage;

switch (type) {
case "success":
toast.success(message, {
duration: options.ephemeral ? defaultToastDuration : permanentToastDuration,
});
break;
case "error":
toast.error(message, {
duration: options.ephemeral ? defaultToastDuration : permanentToastDuration,
});
break;
default:
throw new Error(`${type} is not handled`);
}
toast.custom((t) => <ToastUI variant={type} message={message} t={t as string} />, {
duration: options.ephemeral ? defaultToastDuration : permanentToastDuration,
});
}, [toastMessage]);

return <Toaster />;
}

export function ToastUI({
variant,
message,
t,
toastWidth = 356, // Default width, matches what sonner provides by default
}: {
variant: "error" | "success";
message: string;
t: string;
toastWidth?: string | number;
}) {
return (
<Toaster
position="bottom-right"
toastOptions={{
success: {
icon: <CheckCircleIcon className="h-6 w-6 text-green-600" />,
},
error: {
icon: <ExclamationCircleIcon className="h-6 w-6 text-rose-600" />,
},
<div
className={`self-end rounded-lg border border-slate-750 bg-midnight-900 shadow-md`}
style={{
width: toastWidth,
}}
>
{(t) => (
<AnimatePresence>
<motion.div
className="flex gap-2 rounded-lg border border-slate-750 bg-no-repeat p-4 text-bright shadow-md"
style={{
opacity: t.visible ? 1 : 0,
background:
"radial-gradient(at top, hsla(271, 91%, 65%, 0.18), hsla(221, 83%, 53%, 0.18)) hsla(221, 83%, 53%, 0.18)",
}}
initial={{ opacity: 0, y: 100 }}
animate={t.visible ? "visible" : "hidden"}
variants={{
hidden: {
opacity: 0,
y: 0,
transition: {
duration: 0.15,
ease: "easeInOut",
},
},
visible: {
opacity: 1,
y: 0,
transition: {
duration: 0.3,
ease: "easeInOut",
},
},
}}
>
{t.icon}
{resolveValue(t.message, t)}
<button className="p-1" onClick={() => toast.dismiss(t.id)}>
<XMarkIcon className="h-4 w-4 text-bright" />
</button>
</motion.div>
</AnimatePresence>
)}
</Toaster>
<div
className="flex w-full gap-2 rounded-lg bg-no-repeat p-4 text-bright"
style={{
background:
"radial-gradient(at top, hsla(271, 91%, 65%, 0.18), hsla(221, 83%, 53%, 0.18)) hsla(221, 83%, 53%, 0.18)",
}}
>
{variant === "success" ? (
<CheckCircleIcon className="h-6 w-6 text-green-600" />
) : (
<ExclamationCircleIcon className="h-6 w-6 text-rose-600" />
)}
{message}
<button className="ms-auto p-1" onClick={() => toast.dismiss(t)}>
<XMarkIcon className="h-4 w-4 text-bright" />
</button>
</div>
</div>
);
}
47 changes: 47 additions & 0 deletions apps/webapp/app/components/stories/ToastUI.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { Meta, StoryObj } from "@storybook/react";
import { Toaster, toast } from "sonner";
import { ToastUI } from "../primitives/Toast";
import { Button } from "../primitives/Buttons";

const meta: Meta = {
title: "Primitives/Toast",
};

export default meta;

type Story = StoryObj<typeof Collection>;

export const Toasts: Story = {
render: () => <Collection />,
};

function Collection() {
return (
<div className="flex flex-col items-start gap-y-4 p-4">
<ToastUI variant="success" message="Success UI" t="-" />
<ToastUI variant="error" message="Error UI" t="-" />
<br />
<Button
variant="primary/large"
onClick={() =>
toast.custom((t) => <ToastUI variant="success" message="Success" t={t as string} />, {
duration: Infinity, // Prevents auto-dismissal for demo purposes
})
}
>
Success
</Button>
<Button
variant="primary/large"
onClick={() =>
toast.custom((t) => <ToastUI variant="error" message="Error" t={t as string} />, {
duration: Infinity,
})
}
>
Error
</Button>
<Toaster />
</div>
);
}
1 change: 1 addition & 0 deletions apps/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"simple-oauth2": "^5.0.0",
"simplur": "^3.0.1",
"slug": "^6.0.0",
"sonner": "^1.0.3",
"tailwind-merge": "^1.12.0",
"tailwind-scrollbar-hide": "^1.1.7",
"tailwindcss-animate": "^1.0.5",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.