# Toast

> Confirms an action with a short message that goes away.

A toast confirms that something happened, such as a save or a send, with a short message that goes away by itself.

## When to use

- Use it to confirm an action the person just took: "Rilis tersimpan."
- Use it for errors that are not tied to one field: "Belum bisa mengirim. Coba lagi sebentar."
- Show validation errors under their field, not in a toast; the message comes from the backend.
- Use a [Dialog](https://design.sosialoka.tech/components/dialog/) when the person must decide something.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/toast/_

### Do and don't

- **Do:** Confirm what just happened in a few words.
- **Don't:** Don't put errors, long text or decisions in a toast; it closes on its own.

## Anatomy

1. Status icon
2. Message
3. Close

A message on `popover` with a `border`, `radius-lg` and `shadow-lg`, with an icon in the status color: `success`, `info`, `warning` or `destructive`. Toasts stack in a corner and follow the page's mode.

## Variants

`toast()` for a plain message, and `toast.success`, `toast.info`, `toast.warning`, `toast.error` for a status icon.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `toast.success(message)` from `sonner` | `toast.success(message)` from `vue-sonner` | `ShadToaster.of(context).show(ShadToast(...))` | function | none | Shows a toast. |
| `<Toaster />` once in the app | `<Toaster />` once in the app | `ShadToaster` (in `ShadApp`) | element | none | Where toasts appear. |
| `position` on `Toaster` | `position` on `Toaster` | `alignment` | string | `bottom-right` | Corner of the screen. |

The Sosialoka `Toaster` reads light or dark from `data-theme` or the `.dark` class on `<html>`, so it needs no theme provider.

## Code

**React**
```tsx
import { toast } from "sonner"
import { Button } from "@/components/ui/button"
import { Toaster } from "@/components/ui/sonner"

export default function SonnerDemo() {
  return (
    <div className="flex flex-wrap gap-2">
      <Button variant="outline" onClick={() => toast.success("Rilis tersimpan.")}>
        Simpan
      </Button>
      <Button variant="outline" onClick={() => toast.error("Belum bisa mengirim. Coba lagi sebentar.")}>
        Kirim
      </Button>
      <Toaster />
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { toast } from "vue-sonner"
import { Button } from "@/components/ui/button"
import { Toaster } from "@/components/ui/sonner"
</script>

<template>
  <div class="flex flex-wrap gap-2">
    <Button variant="outline" @click="toast.success('Rilis tersimpan.')">Simpan</Button>
    <Button variant="outline" @click="toast.error('Belum bisa mengirim. Coba lagi sebentar.')">Kirim</Button>
    <Toaster />
  </div>
</template>
```

**Flutter**
```dart
// ShadApp already wraps the app in a ShadToaster.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class SonnerDemo extends StatelessWidget {
  const SonnerDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Wrap(
      spacing: 8,
      children: [
        ShadButton.outline(
          child: const Text('Simpan'),
          onPressed: () => ShadToaster.of(context).show(const ShadToast(description: Text('Rilis tersimpan.'))),
        ),
        ShadButton.outline(
          child: const Text('Kirim'),
          onPressed: () => ShadToaster.of(context).show(
            const ShadToast.destructive(description: Text('Belum bisa mengirim. Coba lagi sebentar.')),
          ),
        ),
      ],
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/sonner.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/sonner.json
```

## Accessibility

- Toasts are announced politely by screen readers and don't take focus.
- Keep them short; anything people must act on belongs in the page or a dialog.
