Vue Toast Component

Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.

Other frameworks

CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and React components. To learn more please visit the following pages.

Vue toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They’re built with flexbox, so they’re easy to align and position.

Overview

Things to know when using the toast plugin:

  • Toasts are opt-in for performance reasons, so you must initialize them yourself.
  • Toasts will automatically hide if you do not specify autohide: false.

Examples

Basic

To encourage extensible and predictable toasts, we recommend a header and body. Toast headers use display: flex, allowing easy alignment of content thanks to our margin and flexbox utilities.

Toasts are as flexible as you need and have very little required markup. At a minimum, we require a single element to contain your "toasted" content and strongly encourage a dismiss button.

<CToast :autohide="false" visible>
  <CToastHeader closeButton>
    <svg
      class="rounded me-2"
      width="20"
      height="20"
      xmlns="http://www.w3.org/2000/svg"
      preserveAspectRatio="xMidYMid slice"
      focusable="false"
      role="img"
    >
      <rect width="100%" height="100%" fill="#007aff"></rect>
    </svg>
    <span class="me-auto fw-bold">CoreUI for Vue.js</span>
    <small>7 min ago</small>
  </CToastHeader>
  <CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>
<template>
  <CButton color="primary" @click="createToast">Send a toast</CButton>
  <CToaster placement="top-end" visible>
    <CToast v-for="(toast, index) in toasts">
      <CToastHeader closeButton>
      <span class="me-auto fw-bold">{{toast.title}}</span>
      <small>7 min ago</small>
      </CToastHeader>
      <CToastBody>
        {{ toast.content }}
      </CToastBody>  
    </CToast>
  </CToaster>
</template>
<script>
  export default {
    data() {
      return {
        toasts: []
      }
    },
    methods: {
      createToast() {
        this.toasts.push({
          title: 'new toast',
          content: 'Lorem ipsum dolor cet emit'
        })
      }
    }
  }
</script>

Translucent

Toasts are slightly translucent to blend in with what's below them.

<CToast :autohide="false" visible>
  <CToastHeader closeButton>
    <svg
      class="rounded me-2"
      width="20"
      height="20"
      xmlns="http://www.w3.org/2000/svg"
      preserveAspectRatio="xMidYMid slice"
      focusable="false"
      role="img"
    >
      <rect width="100%" height="100%" fill="#007aff"></rect>
    </svg>
    <span class="me-auto fw-bold">CoreUI for Vue.js</span>
    <small>7 min ago</small>
  </CToastHeader>
  <CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>

Stacking

You can stack toasts by wrapping them in a toast container, which will vertically add some spacing.

<CToaster>
  <CToast :autohide="false" visible>
    <CToastHeader closeButton>
      <svg
        class="rounded me-2"
        width="20"
        height="20"
        xmlns="http://www.w3.org/2000/svg"
        preserveAspectRatio="xMidYMid slice"
        focusable="false"
        role="img"
      >
        <rect width="100%" height="100%" fill="#007aff"></rect>
      </svg>
      <span class="me-auto fw-bold">CoreUI for Vue.js</span>
      <small>7 min ago</small>
    </CToastHeader>
    <CToastBody>Hello, world! This is a toast message.</CToastBody>
  </CToast>
  <CToast :autohide="false" visible>
    <CToastHeader closeButton>
      <svg
        class="rounded me-2"
        width="20"
        height="20"
        xmlns="http://www.w3.org/2000/svg"
        preserveAspectRatio="xMidYMid slice"
        focusable="false"
        role="img"
      >
        <rect width="100%" height="100%" fill="#007aff"></rect>
      </svg>
      <span class="me-auto fw-bold">CoreUI for Vue.js</span>
      <small>7 min ago</small>
    </CToastHeader>
    <CToastBody>Hello, world! This is a toast message.</CToastBody>
  </CToast>
</CToaster>

Custom content

Customize your toasts by removing sub-components, tweaking them with utilities, or by adding your own markup. Here we've created a simpler toast by removing the default <CToastHeader>, adding a custom hide icon from CoreUI Icons, and using some flexbox utilities to adjust the layout.

<CToast :autohide="false" class="align-items-center" visible>
  <div class="d-flex">
    <CToastBody>Hello, world! This is a toast message.</CToastBody>
    <CToastClose class="me-2 m-auto"/>
  </div>
</CToast>

Alternatively, you can also add additional controls and components to toasts.

<CToast :autohide="false" class="align-items-center" visible>
  <CToastBody>
    Hello, world! This is a toast message.
    <div class="mt-2 pt-2 border-top">
    <CButton type="button" color="primary" size="sm">
    Take action
    </CButton>
    <CToastClose component="CButton" color="secondary" size="sm" class="ms-1">Close</CToastClose>
    </div>
  </CToastBody>
</CToast>

Color schemes

Building on the above example, you can create different toast color schemes with our color and background utilities. Here we've set color="primary" and added .text-white class to the <Ctoast>, and then set white property to our close button. For a crisp edge, we remove the default border with .border-0.

<CToast :autohide="false" color="primary" class="text-white align-items-center" visible>
  <div class="d-flex">
    <CToastBody>Hello, world! This is a toast message.</CToastBody>
    <CToastClose class="me-2 m-auto" white />
  </div>
</CToast>

Customizing

CSS variables

Vue toasts use local CSS variables on .toast for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

--cui-toast-zindex: #{$zindex-toast};
--cui-toast-padding-x: #{$toast-padding-x};
--cui-toast-padding-y: #{$toast-padding-y};
--cui-toast-spacing: #{$toast-spacing};
--cui-toast-max-width: #{$toast-max-width};
@include rfs($toast-font-size, --cui-toast-font-size);
--cui-toast-color: #{$toast-color};
--cui-toast-bg: #{$toast-background-color};
--cui-toast-border-width: #{$toast-border-width};
--cui-toast-border-color: #{$toast-border-color};
--cui-toast-border-radius: #{$toast-border-radius};
--cui-toast-box-shadow: #{$toast-box-shadow};
--cui-toast-header-color: #{$toast-header-color};
--cui-toast-header-bg: #{$toast-header-background-color};
--cui-toast-header-border-color: #{$toast-header-border-color};

How to use CSS variables

const vars = { 
  '--my-css-var': 10,
  '--my-another-css-var': "red" 
}
return <CToast :style="vars">...</CToast>

SASS variables

$toast-max-width:                   350px !default;
$toast-padding-x:                   .75rem !default;
$toast-padding-y:                   .5rem !default;
$toast-font-size:                   .875rem !default;
$toast-color:                       unset !default;
$toast-background-color:            rgba($white, .85) !default;
$toast-border-width:                $border-width !default;
$toast-border-color:                var(--cui-border-color-translucent) !default;
$toast-border-radius:               $border-radius !default;
$toast-box-shadow:                  $box-shadow !default;
$toast-spacing:                     $container-padding-x !default;

$toast-header-color:                $gray-600 !default;
$toast-header-background-color:     rgba($white, .85) !default;
$toast-header-border-color:         rgba($black, .05) !default;

API

CToast

import { CToast } from '@coreui/vue'
// or
import CToast from '@coreui/vue/src/components/toast/CToast'

Props

Prop nameDescriptionTypeValuesDefault
autohideAuto hide the toast.boolean-true
colorSets the color context of the component to one of CoreUI’s themed colors.string'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light', string-
delayDelay hiding the toast (ms).number-5000
dismissibleOptionally add a close button to component and allow it to self dismiss.boolean-true
indexindex of the component.number--
titleTitle node for your component.string--
visibleToggle the visibility of component.boolean--

Events

Event nameDescriptionProperties
closeCallback fired when the component requests to be closed.
showCallback fired when the component requests to be shown.

CToastClose

import { CToastClose } from '@coreui/vue'
// or
import CToastClose from '@coreui/vue/src/components/toast/CToastClose'

Props

Prop nameDescriptionTypeValuesDefault
componentComponent used for the root node. Either a string to use a HTML element or a component.string--

Events

Event nameDescriptionProperties
closeEvent called before the dissmiss animation has started.

CToastHeader

import { CToastHeader } from '@coreui/vue'
// or
import CToastHeader from '@coreui/vue/src/components/toast/CToastHeader'

Props

Prop nameDescriptionTypeValuesDefault
close-buttonAutomatically add a close button to the header.boolean--

Events

Event nameDescriptionProperties
closeEvent called after clicking the close button.

CToaster

import { CToaster } from '@coreui/vue'
// or
import CToaster from '@coreui/vue/src/components/toast/CToaster'

Props

Prop nameDescriptionTypeValuesDefault
placementDescribes the placement of component.string'top-start', 'top', 'top-end', 'middle-start', 'middle', 'middle-end', 'bottom-start', 'bottom', 'bottom-end'-