コンテンツにスキップ
Page

Toast

コードから toast() で出す短い通知です。

import { Toaster, toast, dismissToast } from "@pelagornis/page/components";

上のプレビューはこのファイルを描画したものです。src/components/ にコピーして使い始めてください。

src/components/ToastExample.tsx
import { Button, Stack, Toaster, toast } from "@pelagornis/page/components";
export default function ToastDemo() {
return (
<Stack direction="row" gap="sizeSmall" wrap>
<Button variant="primary" onClick={() => toast("Settings saved", { variant: "success", duration: 3000 })}>
Show success
</Button>
<Button
variant="outline"
onClick={() =>
toast("Upload failed", {
variant: "error",
action: { label: "Retry", onClick: () => toast("Retrying…") },
})
}
>
Show error
</Button>
<Toaster />
</Stack>
);
}

Toast は React の状態を共有する複数のパーツで構成されます。MDX に書いた子要素はそれぞれ別のアイランドとして描画されるため、パーツ同士がつながりません。上の例のように .tsx ファイルで組み立て、そのファイルをインポートしてください。

src/content/docs/example.mdx
---
title: Example
---
import ToastExample from "../../components/ToastExample";
<ToastExample client:load />

client:load はページの読み込み時にアイランドをハイドレートします。ページの下の方にあるコンテンツには client:visible を使ってください。

Markdoc のタグは Astro コンポーネントを描画しますが、このコンポーネントにはクライアントの状態が必要です。例をハイドレートする .astro ファイルで包み、そのファイルをタグとして登録してください。

src/components/markdoc/ToastExample.astro
---
import ToastExample from "../ToastExample";
---
<ToastExample client:load />
markdoc.config.mjs
import { component, defineMarkdocConfig } from "@astrojs/markdoc/config";
import starlightMarkdoc from "@astrojs/starlight-markdoc";
import pageMarkdoc from "@pelagornis/page/markdoc";
export default defineMarkdocConfig({
extends: [starlightMarkdoc(), pageMarkdoc()],
tags: {
"toast-example": {
render: component("./src/components/markdoc/ToastExample.astro"),
selfClosing: true,
},
},
});
src/content/docs/example.mdoc
{% toast-example /%}

各 variant prop が受け付ける値です。

Prop値
variantdefault success error warning

コンポーネントを構成するパーツと、それぞれが描画する data-refineui のロールです。

パーツロール
roottoast
icontoast-icon
itemtoast-item
scale_layertoast-scale-layer
toastertoaster