Docs
トースト

トースト

一時的に表示される、要約されたメッセージ。

インストール

以下のコマンドを実行してください:

npx shadcn-ui@latest add toast

Toaster コンポーネントを追加する

app/layout.tsx
import { Toaster } from "@/components/ui/toaster"
 
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head />
      <body>
        <main>{children}</main>
        <Toaster />
      </body>
    </html>
  )
}

使用方法

useToast フックは、トーストを表示するために使用できる toast 関数を返します。

import { useToast } from "@/components/ui/use-toast"
export const ToastDemo = () => {
  const { toast } = useToast()
 
  return (
    <Button
      onClick={() => {
        toast({
          title: "スケジュール: 巻き戻し",
          description: "2023 年 2 月 10 日金曜日午後 5 時 57 分",
        })
      }}
    >
      トーストを表示
    </Button>
  )
}

シンプル

タイトル付き

アクション付き

デストラクティブ

toast({ variant: "destructive" }) を使用して、デストラクティブ トーストを表示します。