Toast
一時的な通知を表示するトースト。プログラマティックなAPI(toast.success等)で呼び出し可能。
インストール
import { Toast } from "@ginga-ui/core";基本的な使い方
ToastRegionをアプリのルートに1回だけ設置し、toast.showで通知を表示します。toast関数はクライアントのイベントハンドラから呼び出してください。
toast.tsx
import { Button, toast, ToastRegion } from "@ginga-ui/core";
import { X } from "lucide-react";
export function BasicExample() {
return (
<>
<ToastRegion closeIcon={<X />} />
<Button variant="filled" onPress={() => toast.show("通知が届きました")}>
トーストを表示
</Button>
</>
);
}バリアント
toast.info、toast.success、toast.warning、toast.dangerで状態に応じた通知を表示できます。
toast.tsx
toast.module.css
import { Button, toast } from "@ginga-ui/core";
import styles from "./toast.module.css";
import { CircleAlert, CircleCheck, Info, TriangleAlert } from "lucide-react";
export function VariantsExample() {
return (
<div className={styles.variantList}>
<Button
variant="outline"
onPress={() =>
toast.info("新しいバージョンが利用可能です", { icon: <Info /> })
}
>
Info
</Button>
<Button
variant="outline"
onPress={() =>
toast.success("変更が保存されました", { icon: <CircleCheck /> })
}
>
Success
</Button>
<Button
variant="outline"
onPress={() =>
toast.warning("ストレージの空き容量が少なくなっています", {
icon: <TriangleAlert />,
})
}
>
Warning
</Button>
<Button
variant="outline"
onPress={() =>
toast.danger("データの読み込みに失敗しました", {
icon: <CircleAlert />,
})
}
>
Danger
</Button>
</div>
);
}自動クローズ
timeoutオプションを指定すると自動的に閉じます。ホバーやフォーカス中はタイマーが一時停止します。
toast.tsx
import { Button, toast } from "@ginga-ui/core";
import { CircleCheck } from "lucide-react";
export function TimeoutExample() {
return (
<Button
variant="filled"
onPress={() =>
toast.success("アップロード完了", {
icon: <CircleCheck />,
description: "5秒後に自動的に閉じます。",
timeout: 5000,
})
}
>
自動で閉じるトースト
</Button>
);
}Props
ToastRegion
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
placement | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "bottom-right" | トーストを表示する画面の隅。 |
queue | ToastQueue<ToastContentValue> | toast's queue | 表示するトーストのキュー。既定では toast で追加したトーストを表示します。 |
closeIcon | ReactNode | - | 閉じるボタンのアイコン。 |
closeLabel | string | - | 閉じるボタンのアクセシブルな名前。 |
上記に加えて、React Aria の ToastRegion(新しいタブで開く) の props を受け取ります。
toast
トーストを追加する関数。いずれもトーストのキーを返します。
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
show | (title: string, options?: ToastOptions & { variant?: ToastVariant }) => string | - | 任意のバリアントのトーストを追加します。 |
info / success / warning / danger | (title: string, options?: ToastOptions) => string | - | それぞれのバリアントのトーストを追加します。 |
close | (key: string) => void | - | 指定したキーのトーストを閉じます。 |
ToastOptions
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
description | string | - | タイトルの下に表示する説明文。 |
icon | ReactNode | - | タイトルの前に表示するアイコン。 |
timeout | number | - | 自動で閉じるまでのミリ秒。省略すると閉じるまで表示し続けます。 |
onClose | () => void | - | トーストが閉じたときに呼ばれます。 |