# Toast

A message that provides feedback to users about an action or event, often temporary and dismissible.

[`FluxProvider`](https://fluxui.dev/react/docs/installation) shows toasts on every page. To place them yourself, render a `Toast` component, such as in a layout; while it's on the page, it replaces the provider's.

```tsx
import { Toast } from '@fluxui-dev/react';

<body>
    {/* ... */}
    <Toast />
</body>
```

Once a toaster is on the page, import `toast()` from `@fluxui-dev/react` and call it from anywhere, such as an event handler or an Inertia callback:

```tsx
import { Button, Toast, toast } from '@fluxui-dev/react';

<Button onClick={() => toast('Your changes have been saved.')}>Save changes</Button>

<Toast />
```

The `toast()` function supports the following parameter signatures:

```tsx
toast('Your changes have been saved.');

toast('Changes saved', 'You can always update this in your settings.');

toast('Invoice created.', { variant: 'success' });

toast({
    heading: 'Changes saved',
    text: 'Your changes have been saved.',
    variant: 'success',
});
```

In an Inertia app, you can also flash a toast from a Laravel controller. See the [Inertia guide](https://fluxui.dev/react/docs/inertia).

## With heading

Use a heading to provide additional context for the toast.

```tsx
<Button onClick={() => toast({ heading: 'Changes saved', text: 'You can always update this in your settings.' })}>Save changes</Button>
```

## Variants

Use the `variant` option to change the visual style of the toast.

```tsx
<Button onClick={() => toast({ variant: 'success', ... })}>Success</Button>
<Button onClick={() => toast({ variant: 'warning', ... })}>Warning</Button>
<Button onClick={() => toast({ variant: 'danger', ... })}>Danger</Button>
```

## Inverted

Add `invert` to the `Toast` component in your layout to use a dark appearance in light mode and a light appearance in dark mode. This configures every toast while leaving `variant` available for per-message semantics.

```tsx
<Toast invert />
```

## Actions

Add an action to a toast when users need to respond to it, running an `onClick` function when clicked.

```tsx
<Button
    onClick={() =>
        toast({
            heading: 'Changes saved',
            text: 'Your updates are now live.',
            duration: 0,
            variant: 'success',
            action: {
                label: 'Undo',
                onClick: async () => {
                    await new Promise((resolve) => setTimeout(resolve, 1200));
                    toast('Your changes were undone.', { variant: 'success' });
                },
            },
        })
    }
>
    Save changes
</Button>
```

When `onClick` returns a promise, the action shows a loading indicator until it settles and then dismisses the toast. Pass `dismiss: false` to keep it open afterward.

An action can also be a real link. This keeps native link behavior such as opening in a new tab.

```tsx
toast('Invoice created.', {
    action: {
        label: 'View',
        href: `/invoices/${invoice.id}`,
    },
});
```

## Links

Add a link to a toast to give users a clear next step.

```tsx
<Button onClick={() => toast('Invoice created.', { variant: 'success', link: { label: 'View invoice', href: '#' } })}>Create invoice</Button>
```

## Positioning

By default, the toast will appear in the bottom right corner of the page. You can customize this position using the `position` prop.

```tsx
<Toast position="top end" />

{/* Customize top padding for things like navbars... */}
<Toast position="top end" className="pt-24" />
```

Override the position for an individual toast by passing `position` to `toast()`.

```tsx
<Button onClick={() => toast('Your changes have been saved.', { position: 'top end' })}>Save changes</Button>
```

## Duration

By default, the toast will automatically dismiss after 5 seconds. You can customize this duration by passing a number of milliseconds to the `duration` option.

```tsx
<Button onClick={() => toast('Your changes have been saved.', { duration: 1000 })}>Save changes</Button>
```

## Permanent

Use a value of `0` as the `duration` option to make the toast stay open indefinitely.

```tsx
<Button onClick={() => toast('Your changes have been saved.', { duration: 0 })}>Save changes</Button>
```

## Stack

To show a stack of toasts, you can wrap the `Toast` component in a `Toast.Group` component. By default, toasts in a stack overlap and expand on hover to show each toast vertically.

```tsx
<Toast.Group>
    <Toast />
</Toast.Group>
```

Use the `expanded` prop to always show the toast stack in an expanded state, making all toasts visible at once.

```tsx
<Toast.Group expanded>
    <Toast />
</Toast.Group>
```

The group component also accepts the `position` prop to control where the toast stack appears on the screen.

```tsx
<Toast.Group position="top end">
    <Toast />
</Toast.Group>
```

With `FluxProvider`, pass the same options to its `toast` prop, with `group` for a stack.

```tsx
<FluxProvider toast={{ group: true, expanded: true, position: 'top end' }}>
    {app}
</FluxProvider>
```

## Reference

### Toast

**Prop:**

- `position` - Position of the toast on the screen. Options: `bottom end` (default), `bottom center`, `bottom start`, `top end`, `top center`, `top start`.
- `invert` - If `true`, uses a dark appearance in light mode and a light appearance in dark mode for every toast. Default: `false`.

**CSS:**

- `className` - CSS classes added to each toast's outer element, such as `pt-24` to clear a sticky navbar.

**Attribute:**

- `data-flux-toast` - Applied to the toast viewport for styling and identification (`data-flux-toast-group` inside `Toast.Group`).
- `data-flux-toast-dialog` - Applied to each toast.

### Toast.Group

**Prop:**

- `position` - Position of the toast group on the screen. Options: `bottom end` (default), `bottom center`, `bottom start`, `top end`, `top center`, `top start`.
- `expanded` - If `true`, always shows the toast stack in an expanded state, making all toasts visible at once. Default: `false`.

### toast()

The JavaScript function used to trigger toasts from React components.

**Parameter:**

- `heading` - Optional heading text for the toast.
- `text` - Main content text of the toast.
- `variant` - Visual style. Options: `default`, `success`, `warning`, `danger`, `info`.
- `duration` - Duration in milliseconds. Use `0` for permanent toasts. Default: `5000`.
- `position` - Position of this toast, overriding the `Toast` position. Ignored inside `Toast.Group`.
- `link` - Optional link configuration. Supports `label`, `href`, `target`, `rel`, and `download`.
- `action` - Optional action configuration. Requires a `label` and either an `onClick` or `href`. Both support `dismiss`; link actions support `target`, `rel`, and `download`.

### Inertia::flash('flux:toast')

The flash key that the `FluxProvider` from `@fluxui-dev/react/inertia` shows as a toast, taking the same parameters as `toast()`, except that an action needs an `href`, since `onClick` cannot be flashed from PHP.
