Docs
Sonner
Sonner
An opinionated toast component for Svelte.
Loading...
<script lang="ts">
import { toast } from "svelte-sonner";
import { Button } from "$lib/components/ui/button";
</script>
<Button
variant="outline"
on:click={() =>
toast("Event has been created", {
description: "Sunday, December 03, 2023 at 9:00 AM",
action: {
label: "Undo",
onClick: () => console.log("Undo")
}
})}
>
Show Toast
</Button>
<script lang="ts">
import { toast } from "svelte-sonner";
import { Button } from "$lib/components/ui/button";
</script>
<Button
variant="outline"
on:click={() =>
toast.success("Event has been created", {
description: "Sunday, December 03, 2023 at 9:00 AM",
action: {
label: "Undo",
onClick: () => console.log("Undo")
}
})}
>
Show Toast
</Button>
About
The Sonner component is provided by svelte-sonner, which is a Svelte port of Sonner, originally created by Emil Kowalski for React.
Installation
Setup theme support
By default, Sonner will use the user's system preferences to determine whether to show the light or dark theme. To get around this, you can either pass in a custom theme
prop to the component, or simply use mode-watcher which you can hardcode to dark
or light
mode should you wish.
You can learn more about setting up Dark Mode support here.
If you wish to opt out of Dark Mode support, you can uninstall mode-watcher
and remove the theme
prop from the component after installing via CLI, or manually install the component and don't include mode-watcher
Run the following command:
npx shadcn-svelte@latest add sonner
Add the Toaster component
+layout.svelte
<script lang="ts">
import { Toaster } from "$lib/components/ui/sonner";
</script>
<Toaster />
<slot />
Usage
<script lang="ts">
import { toast } from "svelte-sonner";
import { Button } from "$lib/components/ui/button";
</script>
<Button on:click={() => toast("Hello world")}>Show toast</Button>
On This Page