Sonner
An opinionated toast component for Solid.
import { toast } from "solid-sonner"
import { Button } from "~/components/ui/button"
export default function SonnerDemo() {
return (
<Button
variant="outline"
onClick={() =>
toast("Event has been created", {
description: "Sunday, December 03, 2023 at 9:00 AM",
action: {
label: "Undo",
onClick: () => toast.info("Event removed")
}
})
}
>
Show Toast
</Button>
)
}About
Sonner is built on solid-sonner by wobsoriano, a Solid port of Sonner by Emil Kowalski.
Installation
Run the following command:
Add the Toaster to the root of your app
import { Toaster } from "~/components/ui/sonner"
export default function App() {
return (
<>
<main>{/* Your app */}</main>
<Toaster />
</>
)
}Install the following dependencies:
pnpm add solid-sonnerCopy and paste the following code into your project.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { createSignal, onSettled } from "solid-js"
import { Toaster as Sonner, type ToasterProps } from "solid-sonner"
// Follows the `.dark` class on the root element, like the theme tokens.
function createColorMode() {
const [mode, setMode] = createSignal<"light" | "dark">("light")
onSettled(() => {
const root = document.documentElement
const update = () =>
setMode(root.classList.contains("dark") ? "dark" : "light")
update()
const observer = new MutationObserver(update)
observer.observe(root, { attributes: true, attributeFilter: ["class"] })
return () => observer.disconnect()
})
return mode
}
export function Toaster(props: ToasterProps) {
const mode = createColorMode()
return (
<Sonner
theme={mode()}
class="toaster group"
// Getters render a new icon for each toast.
icons={{
get success() {
return (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<circle cx="12" cy="12" r="10" />
<path d="m9 12 2 2 4-4" />
</svg>
)
},
get info() {
return (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<circle cx="12" cy="12" r="10" />
<path d="M12 16v-4" />
<path d="M12 8h.01" />
</svg>
)
},
get warning() {
return (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" />
<path d="M12 9v4" />
<path d="M12 17h.01" />
</svg>
)
},
get error() {
return (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="m15 9-6 6" />
<path d="M2.586 16.726A2 2 0 0 1 2 15.312V8.688a2 2 0 0 1 .586-1.414l4.688-4.688A2 2 0 0 1 8.688 2h6.624a2 2 0 0 1 1.414.586l4.688 4.688A2 2 0 0 1 22 8.688v6.624a2 2 0 0 1-.586 1.414l-4.688 4.688a2 2 0 0 1-1.414.586H8.688a2 2 0 0 1-1.414-.586z" />
<path d="m9 9 6 6" />
</svg>
)
},
get loading() {
return (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4 motion-safe:animate-spin"
>
<path d="M21 12a9 9 0 1 1-6.219-8.56" />
</svg>
)
}
}}
style={{
// Sonner sets a system font stack; use the app's font instead.
"font-family": "inherit",
"--normal-bg": "var(--popover)",
"--normal-text": "var(--popover-foreground)",
"--normal-border": "var(--border)",
"--border-radius": "var(--radius)"
}}
toastOptions={{
classNames: {
toast: "rounded-2xl!"
}
}}
{...props}
/>
)
}Add the Toaster to the root of your app
import { Toaster } from "~/components/ui/sonner"
export default function App() {
return (
<>
<main>{/* Your app */}</main>
<Toaster />
</>
)
}The Toaster follows the .dark class on the root element, like the theme. Pass theme to set it yourself.
Usage
import { toast } from "solid-sonner"toast("Event has been created.")Types
Use toast.success, toast.info, toast.warning, or toast.error to indicate the outcome. Use toast.promise to show a loading toast and update it when the promise settles.
import { toast } from "solid-sonner"
import { Button } from "~/components/ui/button"
export default function SonnerTypes() {
return (
<div class="flex flex-wrap gap-2">
<Button variant="outline" onClick={() => toast("Event has been created")}>
Default
</Button>
<Button
variant="outline"
onClick={() => toast.success("Event has been created")}
>
Success
</Button>
<Button
variant="outline"
onClick={() =>
toast.info("Be at the area 10 minutes before the event time")
}
>
Info
</Button>
<Button
variant="outline"
onClick={() =>
toast.warning("Event start time cannot be earlier than 8am")
}
>
Warning
</Button>
<Button
variant="outline"
onClick={() => toast.error("Event has not been created")}
>
Error
</Button>
<Button
variant="outline"
onClick={() => {
toast.promise<{ name: string }>(
() =>
new Promise((resolve) =>
setTimeout(() => resolve({ name: "Event" }), 2000)
),
{
loading: "Loading...",
success: (data) => `${data.name} has been created`,
error: "Error"
}
)
}}
>
Promise
</Button>
</div>
)
}Description
import { toast } from "solid-sonner"
import { Button } from "~/components/ui/button"
export default function SonnerDescription() {
return (
<Button
onClick={() =>
toast("Event has been created", {
description: "Monday, January 3rd at 6:00pm"
})
}
variant="outline"
class="w-fit"
>
Show Toast
</Button>
)
}Position
Toasts appear at the bottom right by default. Set position on Toaster to change it for every toast, or pass position to toast for one toast.
import { toast } from "solid-sonner"
import { Button } from "~/components/ui/button"
export default function SonnerPosition() {
return (
<div class="flex flex-wrap justify-center gap-2">
<Button
variant="outline"
onClick={() =>
toast("Event has been created", { position: "top-left" })
}
>
Top Left
</Button>
<Button
variant="outline"
onClick={() =>
toast("Event has been created", { position: "top-center" })
}
>
Top Center
</Button>
<Button
variant="outline"
onClick={() =>
toast("Event has been created", { position: "top-right" })
}
>
Top Right
</Button>
<Button
variant="outline"
onClick={() =>
toast("Event has been created", { position: "bottom-left" })
}
>
Bottom Left
</Button>
<Button
variant="outline"
onClick={() =>
toast("Event has been created", { position: "bottom-center" })
}
>
Bottom Center
</Button>
<Button
variant="outline"
onClick={() =>
toast("Event has been created", { position: "bottom-right" })
}
>
Bottom Right
</Button>
</div>
)
}API Reference
See the solid-sonner README for the toast options and the Toaster props.