Toast
一時的に表示される簡潔なメッセージ。アクション後のフィードバックによく使用されます。
いつ使うか
- 成功、警告、エラーなどのフィードバックを提供するため。
- 複雑な内容を含む通知。
- 中央上部に表示され、自動的に消去されるメッセージで、邪魔にならない軽量なプロンプトとして機能します。
使用例
ルートレイアウトに一度だけ <Toast /> コンポーネントを含めます。次に、クライアントサイド JavaScript のどこでも window.Toast.show() を使用してトーストをレンダリングします。
Example.astro
---
import Toast from '@/components/feedback/Toast.astro';
---
<!-- Add this once in your layout -->
<Toast />
<!-- Use the JS API to show toasts -->
<script>
window.Toast.show({
title: 'Toast',
description: 'Friday, February 10, 2023 at 5:57 PM'
});
</script> バリアント
トーストには、適切なアイコンと色を自動的にレンダリングするセマンティックバリアント(`success`、`error`、`info`、`warning`)が含まれています。
script.js
window.Toast.show({
title: 'Toast',
description: 'Your changes have been saved.',
variant: 'success' // 'default' | 'success' | 'error' | 'info' | 'warning'
}); 位置
トーストは画面のさまざまな位置にレンダリングできます。デフォルトは bottom-right です。
アクションとオプション
トーストにアクションボタンを追加したり、自動的に閉じる時間を調整したり、閉じるボタンを完全に削除したりできます。
script.js
window.Toast.show({
title: 'Toast',
action: {
label: 'Try again',
onClick: () => console.log('Retrying...')
},
duration: 10000, // 10 seconds. Set to 0 to disable auto-close.
closable: false // Removes the 'x' close button
}); API リファレンス
| プロパティ | 説明 | タイプ | デフォルト |
|---|---|---|---|
| title | トーストのメインタイトルテキスト。 | string | - |
| description | タイトルの下のセカンダリ説明テキスト。 | string | - |
| variant | アイコンと色を決定するセマンティックバリアント(success、error、info、warning)。 | 'default' | 'success' | 'error' | 'info' | 'warning' | 'default' |
| position | トーストが表示される画面位置(例:top-right、bottom-center)。 | 'top-left' | 'top-right' | 'top-center' | 'bottom-left' | 'bottom-right' | 'bottom-center' | 'bottom-right' |
| duration | 自動的に閉じるまでの時間(ms)(0で無効)。 | number | 5000 |
| closable | 閉じるボタンを表示するかどうか。 | boolean | true |
| action | インラインアクションボタンの設定。 | ToastAction | - |