# Toast

## Usage

Place `ToastViewport` once near the root of your app, then call `useToast` from any component.

On a touch screen, a swipe toward the toast's edge dismisses it: down when it sits at the bottom, up when it sits at the top. A horizontal drag is left to the page. A swipe that starts on a button or link does not dismiss. A mouse drag does not dismiss either.

```vue
<script setup lang="ts">
import { ToastViewport, useToast } from '@astryx-vue/core/Toast'

const toast = useToast()

toast({ body: 'Saved successfully', type: 'success' })
</script>

<template>
  <div>
    <button @click="toast({ body: 'Saved' })">
      Save
    </button>
    <ToastViewport />
  </div>
</template>
```

### Anatomy

- **Body**: The primary message text describing what happened or what the user should know.
- **End content**: A trailing action like an Undo button or a link, placed after the body text.
- **Dismiss button**: A close button that lets the user manually dismiss the toast before auto-hide.

## Best practices

| Guidance | Practice |
| --- | --- |
| Recommended | Put `ToastViewport` at the top level so toasts overlay every page. |
| Recommended | Use `type="error"` for failures and rely on `role="alert"`. |
| Recommended | Keep toast copy short — one line describing the outcome is enough. |
| Don't | Flood the screen with many toasts at once. |
| Don't | Use toasts for actions that need user confirmation. |

## Examples

### Action

A persistent toast can end with a button or a link, for undoing a delete or opening a report.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { Link } from '@astryx-vue/core/Link'
import { VStack } from '@astryx-vue/core/Stack'
import { Toast, ToastViewport, useToast } from '@astryx-vue/core/Toast'

const toast = useToast()
</script>

<template>
  <VStack :gap="3">
    <Toast :is-auto-hide="false" body="Item deleted" type="info">
      <template #end>
        <Button
          label="Undo"
          size="sm"
          variant="secondary"
          @click="toast({ body: 'Undo successful', type: 'info' })"
        />
      </template>
    </Toast>
    <Toast :is-auto-hide="false" body="Your report is ready." type="info">
      <template #end>
        <Link href="#" has-underline>
          View report
        </Link>
      </template>
    </Toast>
    <ToastViewport />
  </VStack>
</template>
```

### Deduplication

uniqueID collapses repeats. ignore keeps the first toast; overwrite replaces it, for a progress percentage.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Toast, ToastViewport, useToast } from '@astryx-vue/core/Toast'

const toast = useToast()
</script>

<template>
  <VStack :gap="3">
    <Toast :is-auto-hide="false" body="You are offline" type="info" />
    <HStack align="center" :gap="3">
      <Button
        label="Offline (ignore)"
        size="sm"
        variant="secondary"
        @click="toast({ body: 'You are offline', uniqueID: 'offline', collisionBehavior: 'ignore', isAutoHide: false })"
      />
      <Button
        label="Progress (overwrite)"
        size="sm"
        variant="secondary"
        @click="toast({ body: `Uploading… ${Math.floor(Math.random() * 100)}%`, uniqueID: 'upload-progress', collisionBehavior: 'overwrite', isAutoHide: false })"
      />
    </HStack>
    <ToastViewport />
  </VStack>
</template>
```

### Dismiss

useToast returns a function that dismisses that toast. Use it when a long-running operation should close the notice itself.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Toast, ToastViewport, useToast } from '@astryx-vue/core/Toast'
import { ref } from 'vue'

const toast = useToast()
const dismiss = ref<(() => void) | null>(null)

function show(): void {
  dismiss.value = toast({ body: 'Uploading file…', isAutoHide: false })
}

function dismissFromCode(): void {
  dismiss.value?.()
  dismiss.value = null
}
</script>

<template>
  <VStack :gap="3">
    <Toast :is-auto-hide="false" body="Uploading file…" type="info" />
    <HStack align="center" :gap="3">
      <Button label="Show toast" size="sm" variant="secondary" @click="show" />
      <Button label="Dismiss via code" size="sm" variant="ghost" @click="dismissFromCode" />
    </HStack>
    <ToastViewport />
  </VStack>
</template>
```

### Stacking

Toasts stack vertically and animate in and out. Clicking again queues another one.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { VStack } from '@astryx-vue/core/Stack'
import { Toast, ToastViewport, useToast } from '@astryx-vue/core/Toast'
import { ref } from 'vue'

const messages = [
  { body: 'Changes saved.', type: 'info' as const },
  { body: 'Failed to upload file.', type: 'error' as const },
  { body: 'Message sent to Sarah Chen.', type: 'info' as const },
]

const toast = useToast()
const count = ref(0)

function show(): void {
  const message = messages[count.value % messages.length]
  count.value += 1
  toast(message)
}
</script>

<template>
  <VStack :gap="3">
    <Toast
      v-for="message in messages"
      :key="message.body"
      :is-auto-hide="false"
      :body="message.body"
      :type="message.type"
    />
    <Button label="Show toast" size="sm" variant="secondary" @click="show" />
    <ToastViewport />
  </VStack>
</template>
```

### Types

Info and error side by side. Info goes away on its own; error stays until it is dismissed.

```vue
<script setup lang="ts">
import { Button } from '@astryx-vue/core/Button'
import { VStack } from '@astryx-vue/core/Stack'
import { Toast, ToastViewport, useToast } from '@astryx-vue/core/Toast'

const toast = useToast()
</script>

<template>
  <VStack :gap="3">
    <Toast :is-auto-hide="false" body="Changes saved successfully." type="info">
      <template #end>
        <Button
          label="Show toast"
          size="sm"
          variant="ghost"
          @click="toast({ body: 'Changes saved successfully.', type: 'info' })"
        />
      </template>
    </Toast>
    <Toast :is-auto-hide="false" body="Failed to save changes." type="error">
      <template #end>
        <Button
          label="Show toast"
          size="sm"
          variant="ghost"
          @click="toast({ body: 'Failed to save changes.', type: 'error' })"
        />
      </template>
    </Toast>
    <ToastViewport />
  </VStack>
</template>
```

## Toast Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `type` | `ToastType` | `'info'` | Visual type. Optional: info, success, warning, error. |
| `body` | `string` | - | Toast message text. |
| `isAutoHide` | `boolean` | `true` for every type except `error` | Whether the toast dismisses itself. Error toasts stay until they are dismissed unless this is set. |
| `autoHideDuration` | `number` | `5000` | Auto-dismiss duration in milliseconds. A pause on hover, focus, or window blur keeps at least one second on the clock. |
| `autoClose` | `boolean` | - | Older name of `isAutoHide`. Both work; `isAutoHide` wins. |
| `duration` | `number` | - | Older name of `autoHideDuration`. Both work; `autoHideDuration` wins. |
| `uniqueID` | `string` | - | Identity shared by toasts that collapse into one. |
| `collisionBehavior` | `'ignore' \| 'overwrite'` | `'overwrite'` | What happens when a toast reuses a visible `uniqueID`. |
| `endContent` | `Component` | - | Trailing action, such as an undo button. Use the `end` slot for one-off content. |
| `onDismiss` | `(reason: 'auto' \| 'manual') => void` | - | Runs when the toast is dismissed from inside the card: the timer, the close button, or custom content. Options for `useToast()` only. |
| `onHide` | `(reason: 'auto' \| 'manual') => void` | - | Runs when the toast is removed, however it was dismissed, including the dismiss function returned by `useToast()`. Options for `useToast()` only. |

## Toast Events

| Name | Description |
| --- | --- |
| `dismiss` | Emitted with the reason (`'auto'` or `'manual'`) when the card dismisses itself. |
| `close` | Older name of `dismiss`, emitted at the same time without the reason. |

## Toast Slots

| Name | Description |
| --- | --- |
| `default` | Custom toast content. Defaults to `body`. |
| `end` | Trailing content, replacing `endContent`. |
| `content` | Replaces the whole card content and receives `{ body, endContent, type, isAutoHide, autoHideDuration, dismiss }`. The empty-state close button is not rendered, so the custom layout owns every control. |

## ToastViewport Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `placement` | `ToastPlacement` | `'bottom-end'` | Position of the toast stack in the viewport. Optional: top-start, top-center, top-end, bottom-start, bottom-center, bottom-end. |

## useToast

```ts
const toast = useToast()

const dismiss = toast({
  body: 'Hello',
  type: 'info',
  autoHideDuration: 3000,
  onDismiss: reason => console.log('dismissed', reason),
  onHide: reason => console.log('removed', reason),
})
// dismiss manually
dismiss()
// dismiss all
toast.clear()
```
