Skip to content

Toast ​

Generates toast notifications.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import ToastButton from './ToastButton.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <ToastButton />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const toastManager = Toast.useToastManager();
let count = 0;

function createToast() {
  count += 1;
  toastManager.add({
    title: `Toast ${count} created`,
    description: 'This is a toast notification.',
  });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="createToast">Create toast</button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root v-for="toast in toasts" :key="toast.id" :toast="toast" :class="styles.Toast">
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <Toast.Title :class="styles.Title" />
          <Toast.Description :class="styles.Description" />
        </div>
        <Toast.Close :class="styles.Close">Dismiss</Toast.Close>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  margin: 0 auto;
  bottom: 1rem;
  right: 1rem;
  left: auto;
  top: auto;

  @media (min-width: 500px) {
    bottom: 2rem;
    right: 2rem;
    width: 22.5rem;
  }
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) +
      var(--toast-swipe-movement-y)
  );
  position: absolute;
  right: 0;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: bottom center;
  bottom: 0;
  left: auto;
  margin-right: 0;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) - (var(--toast-index) * var(--peek)) -
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }
    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    top: 100%;
    width: 100%;
    left: 0;
    height: calc(var(--gap) + 1px);
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Close {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import ToastButton from './ToastButton.vue';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <ToastButton />
    <Toast.Portal>
      <Toast.Viewport
        aria-label="Notifications"
        class="fixed top-auto right-[1rem] bottom-[1rem] z-1 mx-auto w-[calc(100vw-2rem)] sm:right-[2rem] sm:bottom-[2rem] sm:w-[22.5rem]"
      >
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const toastManager = Toast.useToastManager();
let count = 0;

function createToast() {
  count += 1;
  toastManager.add({
    title: `Toast ${count} created`,
    description: 'This is a toast notification.',
  });
}
</script>

<template>
  <button
    type="button"
    class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 py-0 font-[inherit] text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
    @click="createToast"
  >
    Create toast
  </button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const { toasts } = Toast.useToastManager();

const startingClass = 'toast-starting';
const endingClass = 'toast-ending';
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root
      v-for="toast in toasts"
      :key="toast.id"
      :toast="toast"
      class="[--gap:0.75rem] [--peek:0.75rem] [--scale:calc(max(0,1-(var(--toast-index)*0.1)))] [--shrink:calc(1-var(--scale))] [--height:var(--toast-frontmost-height,var(--toast-height))] [--offset-y:calc(var(--toast-offset-y)*-1+calc(var(--toast-index)*var(--gap)*-1)+var(--toast-swipe-movement-y))] absolute right-0 bottom-0 left-auto z-[calc(1000-var(--toast-index))] mr-0 w-full origin-bottom [transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--toast-swipe-movement-y)-(var(--toast-index)*var(--peek))-(var(--shrink)*var(--height))))_scale(var(--scale))] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 select-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none after:absolute after:top-full after:left-0 after:h-[calc(var(--gap)+1px)] after:w-full after:content-[''] [&.toast-ending]:opacity-0 data-expanded:[transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--offset-y)))] inert:opacity-0 [&.toast-starting]:[transform:translateY(150%)] [&.toast-ending:not([inert]):not([data-swipe-direction])]:[transform:translateY(150%)] [&.toast-ending]:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))] data-expanded:[&.toast-ending]:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))] [&.toast-ending]:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))] data-expanded:[&.toast-ending]:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))] [&.toast-ending]:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))] data-expanded:[&.toast-ending]:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))] [&.toast-ending]:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))] data-expanded:[&.toast-ending]:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))] h-[var(--height)] data-expanded:h-[var(--toast-height)] [transition:transform_0.5s_cubic-bezier(0.22,1,0.36,1),opacity_0.5s,height_0.15s] focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
    >
      <Toast.Content
        class="flex items-center gap-4 p-3 overflow-hidden transition-opacity duration-[250ms] ease-[cubic-bezier(0.22,1,0.36,1)] data-behind:opacity-0 data-expanded:opacity-100"
      >
        <div class="flex min-w-0 flex-1 flex-col gap-1">
          <Toast.Title class="text-sm font-bold" />
          <Toast.Description class="text-sm" />
        </div>
        <Toast.Close
          class="flex h-8 shrink-0 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 py-0 font-[inherit] text-sm leading-none whitespace-nowrap font-normal text-neutral-950 hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
          >Dismiss</Toast.Close
        >
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>

Anatomy ​

Import the component and assemble its parts:

vue
<script setup lang="ts">
import { Toast, type ToastObject } from '@vase-ui/vue/toast';

// A component inside each provider reads its toasts with `Toast.useToastManager()`.
defineProps<{ toasts: ToastObject[]; anchoredToasts: ToastObject[] }>();
</script>

<template>
  <Toast.Provider>
    <Toast.Portal>
      <Toast.Viewport aria-label="Notifications">
        <TransitionGroup name="toast">
          <!-- Stacked toasts -->
          <Toast.Root v-for="toast in toasts" :key="toast.id" :toast="toast">
            <Toast.Content>
              <Toast.Title />
              <Toast.Description />
              <Toast.Action />
              <Toast.Close />
            </Toast.Content>
          </Toast.Root>
        </TransitionGroup>
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>

  <!-- Anchored toasts, in a provider of their own -->
  <Toast.Provider>
    <Toast.Portal>
      <Toast.Viewport aria-label="Anchored notifications">
        <TransitionGroup name="toast" :duration="100">
          <Toast.Positioner v-for="toast in anchoredToasts" :key="toast.id" :toast="toast">
            <Toast.Root :toast="toast">
              <Toast.Arrow />
              <Toast.Content>
                <Toast.Title />
                <Toast.Description />
                <Toast.Action />
                <Toast.Close />
              </Toast.Content>
            </Toast.Root>
          </Toast.Positioner>
        </TransitionGroup>
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>

General usage ​

  • <Toast.Provider> can be wrapped around your entire app, so that all toasts render in the same viewport.
  • Toast.useToastManager() reads the toasts of the nearest <Toast.Provider>. Call it in a component inside the provider. Render a Toast.Root for each toast in a <TransitionGroup>, with :key="toast.id".
  • To change the element of a root or a positioner in the <TransitionGroup>, use as. With the element slot, Vue warns in development.
  • The viewport is a landmark region. Give it a name with aria-label, in the language of your page.
  • F6 moves the focus into the landmark region of the toast viewport, so that users can move between the toasts with the keyboard. Press F6 again to move the focus back to where it was.
  • Esc closes the toast that has the focus. After F6, Esc elsewhere in the viewport moves the focus back to where it was. A popup under the toasts, such as a dialog, stays open for that press.
  • Add the data-vase-ui-swipe-ignore attribute to an element in a toast to stop the swipe to dismiss on that element. An interactive element does not start a swipe.
  • The toasts stay in use under a modal popup, such as a dialog, and a screen reader announces them.

Global manager ​

To make a global toast manager, call Toast.createToastManager() and give the result to the toastManager prop of <Toast.Provider>. You can then add a toast from anywhere in the app, such as a function outside a component, and the same provider renders it.

The manager has the same add, close, update and promise methods as Toast.useToastManager(). It does not have the toasts list, because it is not in a component.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const toastManager = Toast.createToastManager();
</script>

<template>
  <Toast.Provider :toast-manager="toastManager">
    <!-- The content of the app -->
  </Toast.Provider>
</template>

All server requests share a manager of a module. A server render does not subscribe the provider to its manager, so a toast of one request does not go to another request.

You can give one manager to more than one provider. Each provider then gets each toast of the manager. The promise method returns one promise for all the providers, so one handler handles its rejection.

Stacking and animations ​

Use the --toast-index CSS variable for the stacking order of the toasts. The toast with the index 0 is at the front.

css
.Toast {
  z-index: calc(1000 - var(--toast-index));
  transform: scale(calc(max(0, 1 - (var(--toast-index) * 0.1))));
}

The --toast-offset-y CSS variable gives the vertical offset of a toast when the toasts are positioned absolutely with a translation offset. Use it with the data-expanded attribute. The attribute is present while the pointer is on the viewport or the keyboard focus is in it.

css
.Toast[data-expanded] {
  transform: translateY(var(--toast-offset-y));
}

While the stack is collapsed, clamp the height of each toast to the height of the frontmost toast with the --toast-frontmost-height CSS variable. Use <Toast.Content> to hide the content of the toasts behind it. The data-behind attribute marks the content behind the frontmost toast. Use it with the data-expanded attribute, so that the content fades in again when the viewport expands:

css
.Toast {
  height: var(--toast-frontmost-height, var(--toast-height));
}

.ToastContent {
  transition: opacity 0.25s;
}

.ToastContent[data-behind] {
  opacity: 0;
}

.ToastContent[data-expanded] {
  opacity: 1;
}

The --toast-swipe-movement-x and --toast-swipe-movement-y CSS variables give the swipe movement of a toast. Use them in a translation offset.

css
.Toast {
  transform: scale(calc(max(0, 1 - (var(--toast-index) * 0.1))))
    translateX(var(--toast-swipe-movement-x))
    translateY(calc(var(--toast-swipe-movement-y) + (var(--toast-index) * -20%)));
}

<TransitionGroup> gives the classes of the enter and the leave. The data-swipe-direction attribute gives the direction of the swipe that closed the toast. Use it in a translation offset in the leave.

css
&.toast-enter-from {
  transform: translateY(150%);
}

&.toast-leave-to {
  opacity: 0;

  &[data-swipe-direction='up'] {
    transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
  }
  &[data-swipe-direction='down'] {
    transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
  }
  /* The examples calculate --offset-y from --toast-offset-y, --toast-index and the swipe movement. */
  &[data-swipe-direction='left'] {
    transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
  }
  &[data-swipe-direction='right'] {
    transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
  }
}

A toast over the limit of the provider stays mounted with the HTML inert attribute. Use [inert] to hide such a toast or to animate it in a different way. Each toast moves to its place with its own transition, so the group needs no move class.

The updateKey property of a toast increases when the toast is updated or added again with its id. Use it to play an animation again: change the animation name with it. When the toast can mount again, you can also put it in the key of the root.

Examples ​

Anchored toasts ​

To anchor a toast to an element, render it in <Toast.Positioner> and give the element as anchor in the positionerProps option of add. Use this for feedback in context, such as a short "Copied" toast next to the button that copied the text.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import AnchoredToasts from './AnchoredToasts.vue';
import { anchoredToastManager } from './anchoredToastManager';
import CopyButton from './CopyButton.vue';
</script>

<template>
  <Toast.Provider :toast-manager="anchoredToastManager">
    <AnchoredToasts />
    <CopyButton />
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <Toast.Portal>
    <Toast.Viewport :class="styles.Viewport" aria-label="Anchored notifications">
      <TransitionGroup
        :enter-from-class="startingClass"
        :leave-to-class="endingClass"
        :duration="100"
      >
        <Toast.Positioner
          v-for="toast in toasts"
          :key="toast.id"
          :toast="toast"
          :class="styles.Positioner"
        >
          <Toast.Root :toast="toast" :class="styles.Toast">
            <Toast.Content>
              <Toast.Description :class="styles.Description" />
            </Toast.Content>
          </Toast.Root>
        </Toast.Positioner>
      </TransitionGroup>
    </Toast.Viewport>
  </Toast.Portal>
</template>
vue
<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue';
import { Button } from '@vase-ui/vue/button';
import { anchoredToastManager } from './anchoredToastManager';
import styles from './index.module.css';

const copied = shallowRef(false);
const button = useTemplateRef('button');

function handleCopy() {
  copied.value = true;

  anchoredToastManager.add({
    description: 'Copied',
    positionerProps: {
      anchor: button.value?.element ?? null,
      sideOffset: 10,
    },
    timeout: 1500,
    onClose() {
      copied.value = false;
    },
  });
}
</script>

<template>
  <Button
    ref="button"
    :class="styles.CopyButton"
    aria-label="Copy to clipboard"
    :disabled="copied"
    focusable-when-disabled
    @click="handleCopy"
  >
    <svg
      v-if="copied"
      width="16"
      height="16"
      viewBox="0 0 16 16"
      fill="none"
      stroke="currentColor"
      style="display: block"
    >
      <path d="m2.5 8.5 4 4 7-9" />
    </svg>
    <svg
      v-else
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="1.5"
      style="display: block"
    >
      <rect width="8" height="4" x="8" y="2" rx="1" ry="1" />
      <path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2" />
    </svg>
  </Button>
</template>
ts
import { Toast } from '@vase-ui/vue/toast';

export const anchoredToastManager = Toast.createToastManager();
css
.Viewport {
  outline: 0;
}

.Positioner {
  z-index: calc(1000 - var(--toast-index));
}

.Toast {
  box-sizing: border-box;
  font-size: 0.875rem;
  line-height: 1.25rem;
  width: max-content;
  padding: 0.25rem 0.5rem;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: var(--transform-origin);
  transition:
    scale 100ms ease-out,
    opacity 100ms ease-out;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  .Starting &,
  .Ending & {
    opacity: 0;
    scale: 0.98;
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Description {
  margin: 0;
}

.CopyButton {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not(:disabled, [aria-disabled='true']) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled, [aria-disabled='true']) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import AnchoredToasts from './AnchoredToasts.vue';
import { anchoredToastManager } from './anchoredToastManager';
import CopyButton from './CopyButton.vue';
</script>

<template>
  <Toast.Provider :toast-manager="anchoredToastManager">
    <AnchoredToasts />
    <CopyButton />
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const { toasts } = Toast.useToastManager();

const startingClass = 'toast-starting';
const endingClass = 'toast-ending';
</script>

<template>
  <Toast.Portal>
    <Toast.Viewport class="outline-0" aria-label="Anchored notifications">
      <TransitionGroup
        :enter-from-class="startingClass"
        :leave-to-class="endingClass"
        :duration="100"
      >
        <Toast.Positioner
          v-for="toast in toasts"
          :key="toast.id"
          :toast="toast"
          class="z-[calc(1000-var(--toast-index))]"
        >
          <Toast.Root
            :toast="toast"
            class="w-max border border-neutral-950 bg-white px-2 py-1 text-sm text-neutral-950 origin-[var(--transform-origin)] shadow-[0.25rem_0.25rem_0] shadow-black/12 transition-[scale,opacity] duration-100 ease-out [.toast-ending_&]:opacity-0 [.toast-ending_&]:scale-[0.98] [.toast-starting_&]:opacity-0 [.toast-starting_&]:scale-[0.98] dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
          >
            <Toast.Content>
              <Toast.Description />
            </Toast.Content>
          </Toast.Root>
        </Toast.Positioner>
      </TransitionGroup>
    </Toast.Viewport>
  </Toast.Portal>
</template>
vue
<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue';
import { Button } from '@vase-ui/vue/button';
import { anchoredToastManager } from './anchoredToastManager';

const copied = shallowRef(false);
const button = useTemplateRef('button');

function handleCopy() {
  copied.value = true;

  anchoredToastManager.add({
    description: 'Copied',
    positionerProps: {
      anchor: button.value?.element ?? null,
      sideOffset: 10,
    },
    timeout: 1500,
    onClose() {
      copied.value = false;
    },
  });
}
</script>

<template>
  <Button
    ref="button"
    class="flex h-8 w-8 items-center justify-center rounded-none border border-neutral-950 bg-white text-neutral-950 select-none hover:not-disabled:not-aria-disabled:bg-neutral-100 active:not-disabled:not-aria-disabled:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-disabled:not-aria-disabled:bg-neutral-800 dark:active:not-disabled:not-aria-disabled:bg-neutral-700 aria-disabled:border-neutral-500 aria-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:aria-disabled:border-neutral-400 dark:disabled:border-neutral-400 dark:aria-disabled:text-neutral-400 dark:disabled:text-neutral-400 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
    aria-label="Copy to clipboard"
    :disabled="copied"
    focusable-when-disabled
    @click="handleCopy"
  >
    <svg
      v-if="copied"
      width="16"
      height="16"
      viewBox="0 0 16 16"
      fill="none"
      stroke="currentColor"
      style="display: block"
    >
      <path d="m2.5 8.5 4 4 7-9" />
    </svg>
    <svg
      v-else
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="1.5"
      style="display: block"
    >
      <rect width="8" height="4" x="8" y="2" rx="1" ry="1" />
      <path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2" />
    </svg>
  </Button>
</template>
ts
import { Toast } from '@vase-ui/vue/toast';

export const anchoredToastManager = Toast.createToastManager();

A prop of <Toast.Positioner> wins over the same key of positionerProps. When neither of them sets a key, the positioner uses the default of that key: 'top' for side, 'center' for align, 0 for sideOffset and alignOffset, 5 for arrowPadding and collisionPadding, 'clipping-ancestors' for collisionBoundary, 'absolute' for positionMethod, and false for sticky and disableAnchorTracking.

Render the anchored toasts in a <Toast.Provider> of their own, apart from the stacked toasts. Make a global toast manager for each provider, so that the app can manage each kind of toast:

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const anchoredToastManager = Toast.createToastManager();
const stackedToastManager = Toast.createToastManager();
</script>

<template>
  <Toast.Provider :toast-manager="anchoredToastManager">
    <!-- The anchored toasts -->
  </Toast.Provider>
  <Toast.Provider :toast-manager="stackedToastManager">
    <!-- The stacked toasts -->
  </Toast.Provider>

  <!-- The content of the app -->
</template>

Each viewport is a landmark region. Give the viewport of the anchored toasts an aria-label of its own, such as "Anchored notifications". The positioner is the child of <TransitionGroup> and gets its classes. For a transition on the root in it, give :duration to the group.

Custom position ​

Your own CSS sets the position of the toasts. To change the position, change the styles of .Viewport and .Root. A more general component can take a data-position attribute, and the CSS gives each value its position. This demo shows the toasts at the top center:

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import ToastButton from './ToastButton.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <ToastButton />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Top notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const toastManager = Toast.useToastManager();
let count = 0;

function createToast() {
  count += 1;
  toastManager.add({
    title: `Toast ${count} created`,
    description: 'This is a toast notification.',
  });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="createToast">Create toast</button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root
      v-for="toast in toasts"
      :key="toast.id"
      :toast="toast"
      swipe-direction="up"
      :class="styles.Toast"
    >
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <Toast.Title :class="styles.Title" />
          <Toast.Description :class="styles.Description" />
        </div>
        <Toast.Close :class="styles.Close">Dismiss</Toast.Close>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  max-width: 22.5rem;
  margin: 0 auto;
  top: 1rem;
  right: 0;
  left: 0;
  bottom: auto;
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) + (var(--toast-index) * var(--gap)) + var(--toast-swipe-movement-y)
  );
  position: absolute;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: top center;
  top: 0;
  left: 0;
  right: 0;
  margin-right: auto;
  margin-left: auto;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) + (var(--toast-index) * var(--peek)) +
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(-150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }
    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    width: 100%;
    bottom: 100%;
    left: 0;
    height: calc(var(--gap) + 1px);
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Close {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import ToastButton from './ToastButton.vue';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <ToastButton />
    <Toast.Portal>
      <Toast.Viewport
        class="fixed top-[1rem] right-0 bottom-auto left-0 z-1 mx-auto w-[calc(100vw-2rem)] max-w-[22.5rem]"
        aria-label="Top notifications"
      >
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const toastManager = Toast.useToastManager();
let count = 0;

function createToast() {
  count += 1;
  toastManager.add({
    title: `Toast ${count} created`,
    description: 'This is a toast notification.',
  });
}
</script>

<template>
  <button
    type="button"
    class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 py-0 font-[inherit] text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
    @click="createToast"
  >
    Create toast
  </button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const { toasts } = Toast.useToastManager();

const startingClass = 'toast-starting';
const endingClass = 'toast-ending';
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root
      v-for="toast in toasts"
      :key="toast.id"
      :toast="toast"
      swipe-direction="up"
      class="[--gap:0.75rem] [--peek:0.75rem] [--scale:calc(max(0,1-(var(--toast-index)*0.1)))] [--shrink:calc(1-var(--scale))] [--height:var(--toast-frontmost-height,var(--toast-height))] [--offset-y:calc(var(--toast-offset-y)+(var(--toast-index)*var(--gap))+var(--toast-swipe-movement-y))] absolute top-0 right-0 left-0 z-[calc(1000-var(--toast-index))] mx-auto w-full origin-top [transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--toast-swipe-movement-y)+(var(--toast-index)*var(--peek))+(var(--shrink)*var(--height))))_scale(var(--scale))] border border-neutral-950 bg-white text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 select-none dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none after:absolute after:bottom-full after:left-0 after:h-[calc(var(--gap)+1px)] after:w-full after:content-[''] [&.toast-ending]:opacity-0 data-expanded:[transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--offset-y)))] inert:opacity-0 [&.toast-starting]:[transform:translateY(-150%)] [&.toast-ending:not([inert]):not([data-swipe-direction])]:[transform:translateY(-150%)] [&.toast-ending]:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))] data-expanded:[&.toast-ending]:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))] [&.toast-ending]:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))] data-expanded:[&.toast-ending]:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))] [&.toast-ending]:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))] data-expanded:[&.toast-ending]:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))] [&.toast-ending]:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))] data-expanded:[&.toast-ending]:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))] h-[var(--height)] data-expanded:h-[var(--toast-height)] [transition:transform_0.5s_cubic-bezier(0.22,1,0.36,1),opacity_0.5s,height_0.15s] focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
    >
      <Toast.Content
        class="flex items-center gap-4 p-3 overflow-hidden transition-opacity duration-[250ms] ease-[cubic-bezier(0.22,1,0.36,1)] data-behind:opacity-0 data-expanded:opacity-100"
      >
        <div class="flex min-w-0 flex-1 flex-col gap-1">
          <Toast.Title class="text-sm font-bold" />
          <Toast.Description class="text-sm" />
        </div>
        <Toast.Close
          class="flex h-8 shrink-0 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 py-0 font-[inherit] text-sm leading-none whitespace-nowrap font-normal text-neutral-950 hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
          >Dismiss</Toast.Close
        >
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>

Undo action ​

The actionProps option of add gives the props of an action button in the toast. Use it to undo the action that the toast tells about.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import UndoForm from './UndoForm.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <UndoForm />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Undo notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const toastManager = Toast.useToastManager();

function action() {
  const id = toastManager.add({
    title: 'Action performed',
    description: 'You can undo this action.',
    type: 'success',
    timeout: 10000,
    actionProps: {
      children: 'Undo',
      onClick() {
        toastManager.close(id);
        toastManager.add({
          title: 'Action undone',
        });
      },
    },
  });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="action">Perform action</button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root v-for="toast in toasts" :key="toast.id" :toast="toast" :class="styles.Toast">
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <div :class="styles.Message">
            <Toast.Title :class="styles.Title" />
            <Toast.Description :class="styles.Description" />
          </div>
          <Toast.Action :class="styles.UndoButton" />
        </div>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  margin: 0 auto;
  bottom: 1rem;
  right: 1rem;
  left: auto;
  top: auto;

  @media (min-width: 500px) {
    bottom: 2rem;
    right: 2rem;
    width: 22.5rem;
  }
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) +
      var(--toast-swipe-movement-y)
  );
  position: absolute;
  right: 0;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: bottom center;
  bottom: 0;
  left: auto;
  margin-right: 0;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) - (var(--toast-index) * var(--peek)) -
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }
    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    width: 100%;
    top: 100%;
    left: 0;
    height: calc(var(--gap) + 1px);
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  min-width: 0;
  flex: 1;
  flex-direction: column;
  align-items: start;
  gap: 0.75rem;
}

.Message {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.UndoButton {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

Promise ​

An asynchronous toast has three states: loading, success and error. The type string of the toast has the name of the state, so a style can change with it. Each state also accepts an object of options of the toast. See promise method.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import PromiseDemo from './PromiseDemo.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <PromiseDemo />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Promise notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const toastManager = Toast.useToastManager();

function runPromise() {
  toastManager
    .promise(
      // Simulate an API request with a promise that resolves after 2 seconds
      new Promise<string>((resolve, reject) => {
        const shouldSucceed = Math.random() > 0.3; // 70% success rate
        setTimeout(() => {
          if (shouldSucceed) {
            resolve('operation completed');
          } else {
            reject(new Error('operation failed'));
          }
        }, 2000);
      }),
      {
        loading: 'Loading data…',
        success: (data: string) => `Success: ${data}`,
        error: (err: Error) => `Error: ${err.message}`,
      },
    )
    .catch(() => {
      // The error toast already shows the reason.
    });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="runPromise">Run promise</button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root v-for="toast in toasts" :key="toast.id" :toast="toast" :class="styles.Toast">
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <Toast.Title :class="styles.Title" />
          <Toast.Description :class="styles.Description" />
        </div>
        <Toast.Close :class="styles.Close">Dismiss</Toast.Close>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  margin: 0 auto;
  bottom: 1rem;
  right: 1rem;
  left: auto;
  top: auto;

  @media (min-width: 500px) {
    bottom: 2rem;
    right: 2rem;
    width: 22.5rem;
  }
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) +
      var(--toast-swipe-movement-y)
  );
  position: absolute;
  right: 0;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: bottom center;
  bottom: 0;
  left: auto;
  margin-right: 0;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) - (var(--toast-index) * var(--peek)) -
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }
    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    width: 100%;
    top: 100%;
    left: 0;
    height: calc(var(--gap) + 1px);
  }

  &[data-type='success'] .Text {
    color: oklch(52.7% 0.154 150.069deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(79.2% 0.209 151.711deg);
    }
  }

  &[data-type='error'] .Text {
    color: oklch(50.5% 0.213 27.518deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.4% 0.191 22.216deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Close {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

Custom ​

To give a toast custom data, pass an object of your own type in the data option. The object can hold any data, functions also, and the template of the toast can read it.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import CustomToast from './CustomToast.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <CustomToast />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Custom notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import type { CustomToastData } from './CustomToastData';
import styles from './index.module.css';

const toastManager = Toast.useToastManager();

function action() {
  const data: CustomToastData = {
    userId: '123',
  };

  toastManager.add({
    title: 'Toast with custom data',
    data,
  });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="action">Create custom toast</button>
</template>
ts
import type { ToastObject } from '@vase-ui/vue/toast';

export interface CustomToastData {
  userId: string;
}

export function isCustomToast(toast: ToastObject): toast is ToastObject<CustomToastData> {
  return toast.data?.userId !== undefined;
}
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import { isCustomToast } from './CustomToastData';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root v-for="toast in toasts" :key="toast.id" :toast="toast" :class="styles.Toast">
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <Toast.Title :class="styles.Title" />
          <Toast.Description v-if="isCustomToast(toast) && toast.data" :class="styles.Description">
            data.userId is {{ toast.data.userId }}
          </Toast.Description>
          <Toast.Description v-else :class="styles.Description" />
        </div>
        <Toast.Close :class="styles.Close">Dismiss</Toast.Close>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  margin: 0 auto;
  bottom: 1rem;
  right: 1rem;
  left: auto;
  top: auto;

  @media (min-width: 500px) {
    bottom: 2rem;
    right: 2rem;
    width: 22.5rem;
  }
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) +
      var(--toast-swipe-movement-y)
  );
  position: absolute;
  right: 0;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: bottom center;
  bottom: 0;
  left: auto;
  margin-right: 0;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) - (var(--toast-index) * var(--peek)) -
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }
    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    top: 100%;
    width: 100%;
    left: 0;
    height: calc(var(--gap) + 1px);
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Close {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

Deduplicated toast ​

When you add a toast again with the same id, its updateKey property increases, so that a custom template can play an animation again. This demo changes between two CSS animation names with updateKey. The same toast stays mounted, and the pulse plays again.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import PulseToastButton from './PulseToastButton.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <PulseToastButton />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Deduplicated notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const toastManager = Toast.useToastManager();

function createToast() {
  toastManager.add({
    id: 'save-status',
    title: 'Draft saved',
    description: 'Click again while it is visible to replay the pulse.',
  });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="createToast">Save draft</button>
</template>
vue
<script setup lang="ts">
import { Toast, type ToastObject } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

function pulseClass(toast: ToastObject) {
  // New toasts start with `updateKey: 0`, so the first add skips the replay pulse.
  if (!toast.updateKey) {
    return null;
  }
  return toast.updateKey % 2 === 0 ? styles.PulseEven : styles.PulseOdd;
}

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root
      v-for="toast in toasts"
      :key="toast.id"
      :toast="toast"
      :class="[styles.Toast, pulseClass(toast)]"
    >
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <Toast.Title :class="styles.Title" />
          <Toast.Description :class="styles.Description" />
        </div>
        <Toast.Close :class="styles.Close">Dismiss</Toast.Close>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  margin: 0 auto;
  right: 1rem;
  bottom: 1rem;
  left: auto;
  top: auto;

  @media (min-width: 500px) {
    right: 2rem;
    bottom: 2rem;
    width: 22.5rem;
  }
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) +
      var(--toast-swipe-movement-y)
  );
  position: absolute;
  right: 0;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: bottom center;
  bottom: 0;
  left: auto;
  margin-right: 0;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) - (var(--toast-index) * var(--peek)) -
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }

    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }

    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }

    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    height: calc(var(--gap) + 1px);
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

/* Alternate animation names so repeated updates restart the same pulse. */
.PulseEven {
  animation: pulse-even 0.28s ease;
}

.PulseOdd {
  animation: pulse-odd 0.28s ease;
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Close {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

@keyframes pulse-even {
  0%,
  100% {
    scale: 1;
  }

  50% {
    scale: 1.04;
  }
}

@keyframes pulse-odd {
  0%,
  100% {
    scale: 1;
  }

  50% {
    scale: 1.04;
  }
}

Varying heights ​

To stack toasts of different heights, clamp the height of each toast to the height of the frontmost toast with the --toast-frontmost-height CSS variable. The data-behind attribute hides the content of the toasts behind it. Do not give <Toast.Content> the height of the root, such as height: 100%. A change of its height with the root stops the height transition of the root.

vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import ToastButton from './ToastButton.vue';
import styles from './index.module.css';
import ToastList from './ToastList.vue';
</script>

<template>
  <Toast.Provider>
    <ToastButton />
    <Toast.Portal>
      <Toast.Viewport :class="styles.Viewport" aria-label="Varying height notifications">
        <ToastList />
      </Toast.Viewport>
    </Toast.Portal>
  </Toast.Provider>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const TEXTS = [
  'Short message.',
  'A bit longer message that spans two lines.',
  'This is a longer description that intentionally takes more vertical space to demonstrate stacking with varying heights.',
  'An even longer description that should span multiple lines so we can verify the clamped collapsed height and smooth expansion animation when hovering or focusing the viewport.',
];

const toastManager = Toast.useToastManager();
let count = 0;

function createToast() {
  count += 1;
  const description = TEXTS[Math.floor(Math.random() * TEXTS.length)];
  toastManager.add({
    title: `Toast ${count} created`,
    description,
  });
}
</script>

<template>
  <button type="button" :class="styles.Button" @click="createToast">
    Create varying height toast
  </button>
</template>
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';
import styles from './index.module.css';

const { toasts } = Toast.useToastManager();

const startingClass = styles.Starting;
const endingClass = styles.Ending;
</script>

<template>
  <TransitionGroup :enter-from-class="startingClass" :leave-to-class="endingClass">
    <Toast.Root v-for="toast in toasts" :key="toast.id" :toast="toast" :class="styles.Toast">
      <Toast.Content :class="styles.Content">
        <div :class="styles.Text">
          <Toast.Title :class="styles.Title" />
          <Toast.Description :class="styles.Description" />
        </div>
        <Toast.Close :class="styles.Close">Dismiss</Toast.Close>
      </Toast.Content>
    </Toast.Root>
  </TransitionGroup>
</template>
css
.Viewport {
  position: fixed;
  z-index: 1;
  width: calc(100vw - 2rem);
  margin: 0 auto;
  bottom: 1rem;
  right: 1rem;
  left: auto;
  top: auto;

  @media (min-width: 500px) {
    bottom: 2rem;
    right: 2rem;
    width: 22.5rem;
  }
}

.Toast {
  --gap: 0.75rem;
  --peek: 0.75rem;
  --scale: calc(max(0, 1 - (var(--toast-index) * 0.1)));
  --shrink: calc(1 - var(--scale));
  --height: var(--toast-frontmost-height, var(--toast-height));
  --offset-y: calc(
    var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) +
      var(--toast-swipe-movement-y)
  );
  position: absolute;
  right: 0;
  margin: 0 auto;
  box-sizing: border-box;
  width: 100%;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  transform-origin: bottom center;
  bottom: 0;
  left: auto;
  margin-right: 0;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.5s,
    height 0.15s;
  cursor: default;
  z-index: calc(1000 - var(--toast-index));
  height: var(--height);
  transform: translateX(var(--toast-swipe-movement-x))
    translateY(
      calc(
        var(--toast-swipe-movement-y) - (var(--toast-index) * var(--peek)) -
          (var(--shrink) * var(--height))
      )
    )
    scale(var(--scale));

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-expanded] {
    transform: translateX(var(--toast-swipe-movement-x)) translateY(var(--offset-y));
    height: var(--toast-height);
  }

  &.Starting,
  &.Ending {
    transform: translateY(150%);
  }

  &[inert] {
    opacity: 0;
  }

  &.Ending {
    opacity: 0;

    &[data-swipe-direction='up'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) - 150%));
    }
    &[data-swipe-direction='left'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) - 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='right'] {
      transform: translateX(calc(var(--toast-swipe-movement-x) + 150%)) translateY(var(--offset-y));
    }
    &[data-swipe-direction='down'] {
      transform: translateY(calc(var(--toast-swipe-movement-y) + 150%));
    }
  }

  &::after {
    content: '';
    position: absolute;
    width: 100%;
    top: 100%;
    left: 0;
    height: calc(var(--gap) + 1px);
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Content {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1);

  &[data-behind] {
    opacity: 0;
  }

  &[data-expanded] {
    opacity: 1;
  }
}

.Text {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
}

.Title {
  font-weight: 700;
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Description {
  font-size: 0.875rem;
  line-height: 1.25rem;
  margin: 0;
}

.Close {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not([data-disabled]) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not([data-disabled]) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-disabled] {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Button {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

API reference ​

Toast.Provider ​

Holds the toasts and their timers. Renders no element.

Props

PropTypeDefaultDescription
limitnumber | undefined3The number of toasts that show. A toast over it is limited, and its root is inert.
timeoutnumber | undefined5000The time in milliseconds before a toast closes. 0 keeps the toasts open.
toastManagerToastManager<any> | undefinedA manager of Toast.createToastManager() that adds toasts from code outside the provider.

Slots

SlotSlot propsDescription
defaultThe content of the part.

Toast.Portal ​

Moves its content to another place of the DOM, <body> by default. Renders no element.

Props

PropTypeDefaultDescription
deferboolean | undefinedfalseMoves the content after the other content of the same update mounted.
disabledboolean | undefinedfalseRenders the content in place, also on the server.
toPortalTarget | undefinedThe target of the content. With no value, the target of the nearest portal above, else body. While it is null, the portal renders nothing.

Slots

SlotSlot propsDescription
defaultThe content of the part.

Toast.Viewport ​

The region that holds the toasts. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastViewportState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastViewportState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-expanded]The pointer is on the viewport, or the keyboard focus is in it.

CSS variables

VariableDescription
--toast-frontmost-heightThe height of the frontmost toast, in pixels.

Toast.Root ​

One toast. Renders a <div> element.

Props

PropTypeDefaultDescription
swipeDirectionSwipeDirection | SwipeDirection[] | undefined['down', 'right']The directions of a swipe that closes the toast. The root writes a touch-action for them, so a touch swipe does not scroll the page: pan-y for horizontal directions, pan-x for vertical directions, and none for both.
toastToastObject<any>RequiredThe toast of the root.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-expanded]The viewport is expanded: the pointer is on it, or the keyboard focus is in it.
[data-type]The type of the toast. The toast of a promise call has loading, success or error.
[data-swiping]The user swipes the toast.
[data-swipe-direction]The direction of the swipe while the user swipes the toast, and of the swipe that closed it: up, down, left or right.
[inert]The toast is over the limit of the provider.

CSS variables

VariableDescription
--toast-indexThe place of the toast in the list. The frontmost toast has 0.
--toast-offset-yThe vertical offset of the toast in the expanded viewport, in pixels.
--toast-heightThe natural height of the toast, with height: auto, in pixels.
--toast-swipe-movement-xThe horizontal movement of the swipe, in pixels.
--toast-swipe-movement-yThe vertical movement of the swipe, in pixels.

Toast.Content ​

The container of the content of a toast. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastContentState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastContentState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-behind]The toast is behind the frontmost toast.
[data-expanded]The viewport is expanded: the pointer is on it, or the keyboard focus is in it.

Toast.Title ​

A heading that names the toast. Renders an <h2> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe toast names the title by this id in aria-labelledby. With no value, a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastTitleState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastTitleState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-type]The type of the toast.

Toast.Description ​

A paragraph that describes the toast. Renders a <p> element.

Props

PropTypeDefaultDescription
idstring | undefinedThe toast names the description by this id in aria-describedby, or in aria-labelledby when the toast has no title and no name of its own. With no value, a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastDescriptionState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastDescriptionState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-type]The type of the toast.

Toast.Action ​

A button that does the action of the toast. Renders a <button> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot press the button.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastActionState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastActionState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-type]The type of the toast.
:disabledThe button is a native button, and it is disabled.
[aria-disabled="true"]The button is another element, and it is disabled.

Toast.Close ​

A button that closes the toast. Renders a <button> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot press the button.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastCloseState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastCloseState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-type]The type of the toast.
:disabledThe button is a native button, and it is disabled.
[aria-disabled="true"]The button is another element, and it is disabled.

Toast.Positioner ​

Places an anchored toast at its anchor. Renders a <div> element.

Props

PropTypeDefaultDescription
alignAlign | undefined'center'The alignment of the popup on the side.
alignOffsetnumber | OffsetFunction | undefined0The offset on the align axis in pixels, or a function that gives it.
anchorAnchor | (() => Anchor)The element to place the popup against: an element, a virtual element, a template ref of a part, or a function that gives one. With no value, the active trigger.
arrowPaddingnumber | undefined5The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels.
collisionAvoidanceCollisionAvoidance | undefinedHow the popup avoids the edge of the boundary.
collisionBoundaryBoundary | undefined'clipping-ancestors'The area that holds the popup: an element, elements, or a rectangle.
collisionPaddingPadding | undefined5The space between the popup and the edge of the boundary, in pixels.
disableAnchorTrackingboolean | undefinedfalseThe popup does not follow a move of its anchor.
positionMethod"absolute" | "fixed" | undefined'absolute'The CSS position of the positioner.
sideSide | undefined'top'The side of the anchor for the toast. It can change to avoid a collision.
sideOffsetnumber | OffsetFunction | undefined0The distance from the anchor in pixels, or a function that gives it.
stickyboolean | undefinedfalseThe popup stays in the view after its anchor scrolls out of it.
toastToastObject<any>RequiredThe toast of the positioner. A prop that is absent takes the value of its positionerProps.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastPositionerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastPositionerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the toast is on: top, bottom, left, right, inline-start or inline-end. A logical side shows only when the side prop or the side of positionerProps is logical.
[data-align]The alignment of the toast on its side: start, center or end.
[data-anchor-hidden]The anchor is out of view.

CSS variables

VariableDescription
--anchor-widthThe width of the anchor, in pixels.
--anchor-heightThe height of the anchor, in pixels.
--available-widthThe width from the anchor to the edge of the collision boundary, in pixels.
--available-heightThe height from the anchor to the edge of the collision boundary, in pixels.
--transform-originThe point that the toast grows from, for the transform-origin of the toast. It is at the arrow, on the edge of the anchor. With no arrow, it is at the aligned edge of the toast or at the center of the anchor.
--toast-indexThe place of the toast in the list. The frontmost toast has 0.

Toast.Arrow ​

An element that points to the center of the anchor of the toast. Renders a <div> element. Its place is in the padding box of its nearest positioned ancestor: the toast when the toast has a position, else the positioner.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: ToastArrowState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ToastArrowState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-side]The side of the anchor that the toast is on, as on the positioner.
[data-align]The alignment of the toast on its side: start, center or end.
[data-uncentered]The arrow cannot point to the center of the anchor.

createToastManager ​

Creates a toast manager for the toastManager prop of <Toast.Provider>. See Global manager.

Return value

ToastManager<Data>

useToastManager ​

Manages the toasts. Call it in setup of a component inside a <Toast.Provider>.

ts
const toastManager = Toast.useToastManager();

It returns toasts, a ComputedRef of the list of the open toasts. Read toasts.value in a script. A template unwraps a toasts that you destructure at the top level of <script setup>.

Return value

UseToastManagerReturnValue<Data>

add method ​

Makes a toast and adds it to the list of toasts.

When you give an id that the list has, the toast with that id changes in place. The list gets no second toast.

It returns the toastId. Use it to update or close the toast later.

ts
const toastId = toastManager.add({
  description: 'Hello, world!',
});
vue
<script setup lang="ts">
import { Toast } from '@vase-ui/vue/toast';

const toastManager = Toast.useToastManager();

function addToast() {
  toastManager.add({
    description: 'Hello, world!',
  });
}
</script>

<template>
  <button type="button" @click="addToast">Add toast</button>
</template>

The title and the description are a text, a number, or a function that returns the content. Use a function for rich content. A label is not a vnode, because the label of a toast with the priority high shows in the toast and in the announcement of the viewport. A vnode that mounts two times on a hydrated page loses its components.

ts
toastManager.add({
  title: () => h('strong', 'Saved'),
  description: 'The draft is in your folder.',
});

For a toast with the priority high, a screen reader announces the title and the description. A screen reader does not announce other content of <Toast.Root>, the content of <Toast.Title> and <Toast.Description> included, unless the user moves to the toast viewport.

Such a toast, its <Toast.Action> and its <Toast.Close> have no tab stop until the keyboard focus comes into the viewport or a press puts the focus in the toast. The viewport has the tab stop. When Tab moves the focus to the viewport, the viewport moves it to the first toast. When Shift+Tab moves the focus to the viewport from after it, the viewport moves it to the last toast. Put each control of such a toast in <Toast.Action> or <Toast.Close>, because another control keeps its tab stop while a screen reader cannot read it.

<Toast.Title> gives the toast its accessible name, and <Toast.Description> gives its description. A toast with no title gets its name from <Toast.Description>. To give such a toast another name, set aria-label on <Toast.Root>. A toast with no title and no description needs that aria-label.

update method ​

Changes the options of the toast.

ts
toastManager.update(toastId, {
  description: 'New description',
});

The options replace the values of the toast. Custom data is also replaced as a whole. To make the update from the current toast, pass a function. It gets the current toast and returns the options. Custom data is undefined when the toast has none yet:

ts
toastManager.update(toastId, (prevToast) => ({
  data: prevToast.data && { ...prevToast.data, progress: 100 },
}));

close method ​

Closes the toast. The toast goes from the list at once, and its element leaves in the <TransitionGroup>.

ts
toastManager.close(toastId);

To close all toasts, give no id:

ts
toastManager.close();

promise method ​

Makes an asynchronous toast with three states: loading, success and error.

ts
const promise = toastManager.promise(
  new Promise<string>((resolve) => {
    setTimeout(() => resolve('world!'), 1000);
  }),
  {
    // Each is a shortcut for the `description` option
    loading: 'Loading…',
    success: (data) => `Hello ${data}`,
    error: (err) => `Error: ${err}`,
  },
);

It returns a promise. That promise resolves with the value of the given promise. When the given promise rejects, the toast gets the error state, and the returned promise rejects with the same reason. Handle that rejection with .catch(), or with try and catch around an await. Without a handler, the browser reports an unhandled rejection. The Promise demo handles it with .catch().

The loading state accepts the options of the add method. The success and error states accept the options of the update method:

ts
const promise = toastManager.promise(
  new Promise<string>((resolve) => {
    setTimeout(() => resolve('world!'), 1000);
  }),
  {
    loading: {
      id: 'my-toast',
      title: 'Loading…',
      description: 'The promise is loading.',
    },
    success: {
      title: 'Success',
      description: 'The promise resolved successfully.',
    },
    error: {
      title: 'Error',
      description: 'The promise rejected.',
      actionProps: {
        children: 'Contact support',
        onClick() {
          // Redirect to support page
        },
      },
    },
  },
);

Give loading.id to update or close the toast with that id. An id that the list has updates that toast in place, and the list gets no second toast.

Additional types ​

ToastObject ​

The toast prop of <Toast.Root> and each item of toasts have this type.

PropertyTypeDefaultDescription
actionProps?ToastManagerActionProps | undefinedThe attributes and listeners of Toast.Action. children is its content.
data?Data | undefinedThe data of the developer.
description?ToastLabel | undefinedThe description. Toast.Description shows it when the part has no content of its own.
height?number | undefinedThe natural height of the root in pixels, after its measure.
idstringThe id of the toast. An add with the id of an open toast updates that toast.
limited?boolean | undefinedThe toast is over the limit of the provider.
onClose?(() => void) | undefinedRuns when the toast closes.
onRemove?(() => void) | undefinedRuns when the element of the toast is out of the document, after its leave.
positionerProps?ToastManagerPositionerProps | undefinedThe props of Toast.Positioner for an anchored toast.
priority?"low" | "high" | undefined'low'How a screen reader announces the toast: 'low' politely, 'high' at once.
timeout?number | undefinedThe time in milliseconds before the toast closes. 0 keeps the toast open. With no value, the timeout of the provider.
title?ToastLabel | undefinedThe title. Toast.Title shows it when the part has no content of its own.
type?string | undefinedThe type of the toast, such as 'success', for styles and for the content.
updateKey?number | undefinedThe number of updates of the toast.

ToastManagerAddOptions ​

PropertyTypeDefaultDescription
actionProps?ToastManagerActionProps | undefinedThe attributes and listeners of Toast.Action. children is its content.
data?Data | undefinedThe data of the developer.
description?ToastLabel | undefinedThe description. Toast.Description shows it when the part has no content of its own.
id?string | undefinedThe id of the toast. With the id of an open toast, the call updates that toast.
onClose?(() => void) | undefinedRuns when the toast closes.
onRemove?(() => void) | undefinedRuns when the element of the toast is out of the document, after its leave.
positionerProps?ToastManagerPositionerProps | undefinedThe props of Toast.Positioner for an anchored toast.
priority?"low" | "high" | undefined'low'How a screen reader announces the toast: 'low' politely, 'high' at once.
timeout?number | undefinedThe time in milliseconds before the toast closes. 0 keeps the toast open. With no value, the timeout of the provider.
title?ToastLabel | undefinedThe title. Toast.Title shows it when the part has no content of its own.
type?string | undefinedThe type of the toast, such as 'success', for styles and for the content.

ToastManagerUpdateOptions ​

PropertyTypeDefaultDescription
actionProps?ToastManagerActionProps | undefinedThe attributes and listeners of Toast.Action. children is its content.
data?Data | undefinedThe data of the developer.
description?ToastLabel | undefinedThe description. Toast.Description shows it when the part has no content of its own.
onClose?(() => void) | undefinedRuns when the toast closes.
onRemove?(() => void) | undefinedRuns when the element of the toast is out of the document, after its leave.
positionerProps?ToastManagerPositionerProps | undefinedThe props of Toast.Positioner for an anchored toast.
priority?"low" | "high" | undefined'low'How a screen reader announces the toast: 'low' politely, 'high' at once.
timeout?number | undefinedThe time in milliseconds before the toast closes. 0 keeps the toast open. With no value, the timeout of the provider.
title?ToastLabel | undefinedThe title. Toast.Title shows it when the part has no content of its own.
type?string | undefinedThe type of the toast, such as 'success', for styles and for the content.

ToastManagerPromiseOptions ​

The options of the promise method.

PropertyTypeDescription
errorstring | ToastManagerUpdateOptions<Data> | ((error: any) => string | ToastManagerUpdateOptions<Data>)The toast after the promise rejects. A text is the description.
loadingstring | ToastManagerAddOptions<Data>The toast while the promise waits. A text is the description.
successstring | ToastManagerUpdateOptions<Data> | ((result: Value) => string | ToastManagerUpdateOptions<Data>)The toast after the promise resolves. A text is the description.

ToastManagerPositionerProps ​

The props of <Toast.Positioner> in the positionerProps option of a toast.

PropertyTypeDefaultDescription
align?Align | undefined'center'The alignment of the popup on the side.
alignOffset?number | OffsetFunction | undefined0The offset on the align axis in pixels, or a function that gives it.
anchor?Anchor | (() => Anchor)The element to place the popup against: an element, a virtual element, a template ref of a part, or a function that gives one. With no value, the active trigger.
arrowPadding?number | undefined5The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels.
class?unknownThe class of the positioner element, under the class of the developer.
collisionAvoidance?CollisionAvoidance | undefinedHow the popup avoids the edge of the boundary.
collisionBoundary?Boundary | undefined'clipping-ancestors'The area that holds the popup: an element, elements, or a rectangle.
collisionPadding?Padding | undefined5The space between the popup and the edge of the boundary, in pixels.
disableAnchorTracking?boolean | undefinedfalseThe popup does not follow a move of its anchor.
positionMethod?"absolute" | "fixed" | undefined'absolute'The CSS position of the positioner.
side?Side | undefined'top'The side of the anchor for the toast. It can change to avoid a collision.
sideOffset?number | OffsetFunction | undefined0The distance from the anchor in pixels, or a function that gives it.
sticky?boolean | undefinedfalseThe popup stays in the view after its anchor scrolls out of it.
style?StyleValueThe style of the positioner element, under the style of the developer.

Differences from Base UI ​

Vase UI ports Toast from Base UI 1.9.0.

API ​

Base UIVase UI
Toast.Portal prop renderThe portal is a <Teleport> and renders no element of its own, so it has no render, as and element. The developer writes an element in the portal when they need one.
Toast.Root<TransitionGroup> around the roots animates them, and CSS selects a limited root with [inert]. The state thus has no transitionStatus, and the root has no data-starting-style, data-ending-style and data-limited.
Toast.Action prop nativeButtonThe button reads the element that it renders, so it has no nativeButton.
Toast.Close prop nativeButtonThe button reads the element that it renders, so it has no nativeButton.
Toast.PositionerWith a Vue default, the part cannot find out that a prop is absent. The props of the placement thus have no Vue default. A prop that is absent takes the same key of positionerProps of the toast, else the default of Base UI: 'top' for side, 'center' for align, 0 for sideOffset and alignOffset, 5 for arrowPadding and collisionPadding, 'clipping-ancestors' for collisionBoundary, 'absolute' for positionMethod, and false for sticky and disableAnchorTracking.

Added in Vase UI ​

PartMemberWhy
Toast.Portaldisabled propA disabled portal renders at its source, also on the server (VUE-128).
Toast.Portaldefer propThe portal moves its content after the other content of the same update mounted, as defer of <Teleport> does.

Return types ​

FunctionVase UI returnsWhy
Toast.createToastManagerToastManager<Data>The source of Base UI returns the same type, ToastManager<Data>. Its types.md prints the type argument as {}.
Toast.useToastManagerUseToastManagerReturnValue<Data>setup runs one time, so a plain array does not follow a change of the toasts. toasts of the result is thus a ComputedRef of the array. The source of Base UI returns UseToastManagerReturnValue<Data>, and its types.md prints the type argument as {}.

Behavior ​

Vase UIEvidence
The timers of a toast keep running while the focus is in a frame of the page. They pause while the page is hidden, also when a frame has the focus.
close() of a toast provider reads no document while the provider has no viewport element. A call in a server render, or before the viewport is mounted, thus closes the toast. Base UI reads the global document before it reads the viewport, so the same call throws in Node.
When two titles of one toast are mounted, the toast names the title that registered last in aria-labelledby. When that title unmounts, the toast names the title that stays. Base UI gives the toast no aria-labelledby then, because the title that stays does not write its id again. The descriptions follow the same rule for aria-describedby.
A viewport that mounts while the page is hidden starts with the timers paused. The timers start when the page shows and has the focus. Base UI starts them at once, so a toast that a page in a background tab adds can close before the user sees it.
One manager of Toast.createToastManager() can have more than one mounted provider. Its promise method returns one promise that waits for the promise of each provider, so a handler of the caller handles each rejection. Base UI returns the promise of the last provider only, and the browser reports the rejection of each other provider as unhandled.
A toast with no title takes its accessible name from its description: aria-labelledby has the id of the description, and the toast has no aria-describedby. WAI-ARIA requires a name for the roles dialog and alertdialog. When the consumer gives the toast an aria-label or an aria-labelledby that is not blank, the toast takes the name of the consumer and keeps its description, as in Base UI. A blank aria-label or aria-labelledby, and an aria-labelledby of undefined, are not given, so the toast takes its name from its description. An aria-labelledby of null removes the name: the toast has no aria-labelledby and keeps its description. Base UI gives a toast with no title no name.
Escape with the focus in a toast closes only that toast. The toast stops the event, so a popup under the toast, such as a modal dialog, stays open. The toast does not stop an Escape that it does not handle, such as an Escape during its exit. In Base UI, the event goes on to the document, and a popup that listens there closes together with the toast.
After F6, an Escape in the toast viewport that no toast handles returns the focus to the element that had it before F6. The viewport stops that event, so a popup under the toasts, such as a modal dialog, stays open. The viewport does not stop an Escape that moves no focus. In Base UI, the viewport has no Escape handler, so the focus stays in the viewport. The event reaches the document, and a popup that listens there closes.
An arrow points at the center of its anchor. Its place is in the padding box of its nearest positioned ancestor, so the borders of a popup with a position do not move it. arrowPadding keeps the same space from each inner edge of that box. In the Popover demo of Base UI, the popup has position: relative and a 1 px border, and the arrow is 1.5 px off the center of its trigger.
--transform-origin is in the pixels of the popup, from its border box: the arrow or the aligned edge or the center of the anchor, on the edge of the anchor. A margin of the popup, a padding of the positioner, a margin that collapses through the popup and a zoom of the popup do not move it. A Select popup over the trigger grows from the middle of the value, also after it grows while the user scrolls the list. In the Popover demo of Base UI, a 16 px margin on the popup moves the origin 16 px off the edge of the trigger.
An arrow in a popup with a CSS zoom points at the center of its anchor. Its place and arrowPadding are in the pixels of its nearest positioned ancestor. A Select popup over the trigger with a zoom puts the text of the selected item over the text of the value. In the Popover demo of Base UI, a zoom: 2 on the popup moves the arrow 225 px off the center of the trigger.
A toast that closes while it has no mounted root, such as a toast of a provider with no viewport, leaves the list at once, and its onRemove runs at once. An add with its id then makes a new toast. In Base UI, such a toast stays in the list in its exit until a root mounts and ends the exit, so its onRemove waits, and an add with its id replaces it and drops that onRemove.
A swipe that moves back after it went 10 px or more in its direction keeps the toast, whatever the size of each pointer move. A move of more than 40 px in the direction after the move back permits the close again. Base UI compares the move back with the displacement of the move that sets the direction. Real input moves a few pixels at a time, so in Base UI a move back often does not keep the toast.
With the element slot, the developer renders the element of Toast.Title, Toast.Description and Toast.Action, and the part does not know its content before the render. So the part renders the slot also when the toast has no such label. The root names an element of that slot only while the element is in the DOM and has content, so a toast whose title element is empty takes its name from its description (VUE-99). Base UI renders no element for a render prop with no children when the toast has no label.
A pointerdown listener that calls preventDefault() does not stop the swipe of a drawer or a toast. The call keeps its native meaning only: the browser sends no compatibility mouse events. An element in the popup or the toast stops the swipe with data-vase-ui-swipe-ignore, and a listener that calls stopPropagation() keeps the press from the part. Base UI starts no swipe from a press that a listener prevented.
No part of a toast has a tab stop while it has aria-hidden, as ACT rule 6cfa84 asks. A toast of high priority, its Toast.Action and its Toast.Close have no tab stop until the keyboard focus comes into the viewport or a press puts the focus in the toast. Toast.Close has no tab stop while the stack is collapsed and the focus is not in its toast. While a toast of high priority has no tab stop, the viewport has one. Tab to the viewport moves the focus to the first toast, and Shift+Tab from after the viewport moves it to the last toast. So Tab reaches each toast, and axe-core reports no part of a toast under the rule aria-hidden-focus. Base UI gives aria-hidden to the same elements and keeps their tab stops.
A button part knows the element of a component in as or of the element slot only at the mount, and it needs no prop that says what the element is. Until the mount, the element has the attributes that suit a <button> and each other element: type="button", role="button", a tab stop, and the disabled state of both. So in the server HTML, Tab reaches a <div> before a lazy hydration, and a <button> does not submit its form. At the mount, the part keeps the attributes of its element. Base UI needs nativeButton set to false for an element that is not a <button>.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.