💡 This guide shows how to use the library with Astro (opens in a new tab) + @astro/react (opens in a new tab).
Create a new Astro project
- Run the following command:
npm create astro@latest- Add @astrojs/react (opens in a new tab) integration:
npx astro add reactAdd the library
-
Run the following command:
-
Add the
Toasterto thelayout.astrofile:
💡 Add
client:load(opens in a new tab) directive to theToastercomponent.
- Create a example component with a button to trigger a toast message:
// 📄 components/showToast.tsx- Import the
ShowToastcomponent in theindex.astrofile:
💡 Add
client:load(opens in a new tab) directive to theShowToastcomponent.
// 📄 pages/index.astro
---
import Layout from '../layouts/Layout.astro';
import ShowToast from '../components/showToast';
---
<Layout title="Welcome to Astro.">
<main>
<h1>Welcome to <span class="text-gradient">Astro</span></h1>
<ShowToast client:load />
</main>
</Layout>Add the styles (optional)
The library exports a CSS file that you can include in your project. Only use in cases where the toast styles do not render correctly:
💡 Add the following code to the global
layout.astrofile.
// 📄 layout.astro
---
import "@pheralb/toast/dist/styles.css";
---
<h2>hello</h2>🛠️ Example with View Transitions:
---
import { ClientRouter } from "astro:transitions";
import { Toaster } from "@pheralb/toast";
import "@pheralb/toast/dist/styles.css";
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>Astro Basics</title>
<ClientRouter />
</head>
<body>
<slot />
<Toaster client:load />
</body>
</html>✨ Ready.