トースト通知 Toast Notification

画面の端に一時的に表示されるメッセージ。操作結果やお知らせを伝える。

使用場面: 保存完了、エラー通知、操作結果のフィードバック 採用例: Gmail, Slack

ライブデモ

ソースコード

<div class="demo-toast-controls">
  <button class="demo-toast-trigger" data-type="success">成功</button>
  <button class="demo-toast-trigger" data-type="error">エラー</button>
  <button class="demo-toast-trigger" data-type="info">お知らせ</button>
</div>
<div class="demo-toast-container"></div>
.demo-toast-controls { display: flex; gap: 8px; margin-bottom: 12px; }
.demo-toast-trigger {
  padding: 8px 16px; border: none; border-radius: 8px;
  font-size: 13px; cursor: pointer; color: #fff; font-weight: 600;
}
.demo-toast-trigger[data-type="success"] { background: var(--ui-primary); }
.demo-toast-trigger[data-type="error"] { background: #ef4444; }
.demo-toast-trigger[data-type="info"] { background: #6b7280; }
.demo-toast-container {
  position: relative; min-height: 60px;
  display: flex; flex-direction: column; gap: 8px; align-items: flex-end;
}
.demo-toast {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-radius: 10px; color: #fff;
  font-size: 13px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  animation: demo-toast-in 0.3s ease-out;
  min-width: 200px;
}
.demo-toast.success { background: var(--ui-primary); }
.demo-toast.error { background: #ef4444; }
.demo-toast.info { background: #6b7280; }
.demo-toast.removing { animation: demo-toast-out 0.3s ease-in forwards; }
@keyframes demo-toast-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
@keyframes demo-toast-out { from { opacity: 1; transform: translateX(0); } to { opacity: 0; transform: translateX(20px); } }
(function(){
  const messages = { success: '保存しました', error: 'エラーが発生しました', info: '新しいお知らせがあります' };
  const icons = { success: '✓', error: '✕', info: 'ℹ' };
  document.querySelectorAll('.demo-toast-trigger').forEach(btn => {
    btn.addEventListener('click', function() {
      const type = this.dataset.type;
      const toast = document.createElement('div');
      toast.className = 'demo-toast ' + type;
      toast.innerHTML = '<span>' + icons[type] + '</span><span>' + messages[type] + '</span>';
      document.querySelector('.demo-toast-container').appendChild(toast);
      setTimeout(() => { toast.classList.add('removing'); setTimeout(() => toast.remove(), 300); }, 2500);
    });
  });
})();

AIプロンプト

Basic
トースト通知をHTML/CSS/JSで作ってください。ボタンを押すと画面右下にメッセージが一時表示されます。
Custom
以下の仕様でトースト通知を実装してください。
- 成功(緑)、エラー(赤)、警告(黄)、お知らせ(青)の4種類
- プライマリカラー: #2563eb(お知らせタイプ)
- 右からスライドインアニメーション
- 自動消去(3秒後にフェードアウト)
- プログレスバー表示(残り時間)
- 閉じるボタン
- スタック表示(複数同時表示)
Advanced
本格的なトースト通知システムを実装してください。
- キューイングシステム(最大表示数制限、超過分はキューに)
- role="alert" + aria-live="polite"/"assertive" でスクリーンリーダー対応
- ホバーで自動消去を一時停止
- スワイプで閉じる(タッチデバイス)
- 重複メッセージの抑制
- プログラマティックAPI(toast.success(), toast.error())
- カスタムレンダリング対応(HTMLコンテンツ)
- prefers-reduced-motion でアニメーション無効化
- 位置の設定(上下左右の4コーナー)