# Toast

Brief messages about what just happened, with one optional action; call toast() anywhere under the provider.

- Family: overlay
- Import: `import { Toaster } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/toast
- JSON: https://design.senvenli.com/docs/components/toast.json

## Example: Say what just happened

Call toast() from any handler; the provider already renders the region.

```tsx
import { Button } from "@senvenli/react/action";
import { toast } from "@senvenli/react/overlay";

export default function Example() {
  return <Button onPress={() => toast.success("Order confirmed", { description: "ORD-1040 · email sent to the customer" })}>Confirm order</Button>;
}
```

## Examples

### Tones

Success, information and danger. A danger toast stays until it is dismissed.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";
import { toast } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      <Button onPress={() => toast.success("Changes saved")}>Success</Button>
      <Button onPress={() => toast.information("Export started", { description: "You get an email when the file is ready." })}>Information</Button>
      <Button onPress={() => toast.danger("Upload failed", { description: "The file is larger than 10 MB." })}>Danger</Button>
    </Stack>
  );
}
```

### With an action

One link at most: undo a reversible step, or view the result.

```tsx
import { Button } from "@senvenli/react/action";
import { toast } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <Button
      iconBefore="archive"
      onPress={() =>
        toast({
          title: "Project archived",
          description: "Atlas moved to the archive.",
          action: { label: "Undo", onPress: () => toast.success("Project restored") },
        })
      }
    >
      Archive project
    </Button>
  );
}
```

### Kept until dismissed

A timeout of 0 keeps a toast until it is dismissed; its key dismisses it from code.

```tsx
import { Button } from "@senvenli/react/action";
import { Stack } from "@senvenli/react/layout";
import { toast } from "@senvenli/react/overlay";
import { useRef } from "react";

export default function Example() {
  const key = useRef<string | null>(null);
  return (
    <Stack direction="horizontal" space="space-100">
      <Button
        onPress={() => {
          if (key.current) toast.dismiss(key.current);
          key.current = toast({ title: "Import started", description: "1,204 customers · this can take a minute.", timeout: 0 });
        }}
      >
        Start import
      </Button>
      <Button variant="subtle" onPress={() => key.current && toast.dismiss(key.current)}>
        Dismiss it
      </Button>
    </Stack>
  );
}
```

### Following a promise

`toast.promise`: a spinner while it runs, then the success or the error in place.

```tsx
import { Button } from "@senvenli/react/action";
import { toast } from "@senvenli/react/overlay";

// One toast for a task that takes a moment: a spinner while it runs, then the result in place.
export default function Example() {
  const save = () => new Promise<string>((resolve) => setTimeout(() => resolve("ORD-0914"), 1500));
  return (
    <Button
      variant="primary"
      onPress={() =>
        void toast.promise(save(), {
          loading: "Saving the order…",
          success: (id) => `Order ${id} saved`,
          error: "The order was not saved",
        })
      }
    >
      Save order
    </Button>
  );
}
```

## Guidelines

### When to use it

- Confirming an action that finished: saved, sent, deleted (with Undo).

### Use something else when

- [banner](https://design.senvenli.com/docs/components/banner.md): The message must stay until resolved.

### Do and avoid

- Write in the past tense: "Order confirmed".

## Accessibility

Built in:

- Announced politely; danger toasts stay until dismissed.
- The provider mounts the toast region once.

## API reference

### Tokens it reads

`--ds-border-focused`, `--ds-easing-enter`, `--ds-icon-danger`, `--ds-icon-information`, `--ds-icon-subtle`, `--ds-icon-success`, `--ds-radius-full`, `--ds-radius-large`, `--ds-shadow-overlay`, `--ds-space-025`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-subtle`

## Related

- [banner](https://design.senvenli.com/docs/components/banner.md)
