Toast
#

Component for sending a notification to the user. To make it easier to create toast we have a shorthands. For options API it is $vaToast. For composition api we have useToast hook. Hook and global object have similar API.

In options API you can use VaToastPlugin (automatically installed with createVuestic). $vaToast have a few methods: init, close, closeAll.

In composition API you can use useToast hook.

export default {
  methods: {
    onButtonClick() {
      this.$vaToast.notify('Toast example!')
    },
  },

  beforeRouteLeave (to, from, next) {
    this.$vaToast.closeAll()
    next()
  },
}
Method nameApi Typedescription
init(options: string | NotificationOptions)

Options | Composition

Creates new toast instance. Returns toast instance id

close(id: string)

Options | Composition

Closes specific using its id.

closeAll(allApps?: boolean = false)

Options | Composition

Closes all instances created in this Vue App. If you want to close all toasts on webpage, set allApps to true.

closeAllCreatedInThisHook()

Composition

Using this method you can close all toasts created in one setup context

Init Options
#

nametype
title

string

message

string | VNode

iconClass

string

customClass

string

duration

number

closeable

boolean

dangerouslyUseHtmlString

boolean

render

() => VNode

onClose

() => void

onClick

() => void

offsetX

number

position

NotificationPosition

offsetY

number

visible

boolean

color

string

Examples
#

Basic usage
#

By default, run this component in events by using the init method with a setting object.

Open in GitHub

Color
#

Set different colors using color prop. You can either user theme string HEX color value.

Open in GitHub

Offset
#

Use offset property to set the offset of the toast.

Open in GitHub

Position
#

Use position property to set the custom position of the toast. Available are top-right, top-left, bottom-right, bottom-left.

Open in GitHub

Close
#

You can use close method to close the notification and you can set custom onClose event.

Open in GitHub

Click
#

You can set custom onClick event to handle the click on button.

Open in GitHub

Accessibility
#

Toasts are intended to be small interruptions to your visitors or users, it's wrap with an aria-live region, so that changes to live regions are automatically announced by screen reader without needding to move the user's focus or otherwise interrupt the user. If the conveying messages is important, you should not add the role attribute that can either be alert or alertdialog depending on closeable prop, aria-live will be computed and set to assertive. if messages is not important, you should manually set the role attribute to status, and aria-live should be computed to polite.

Note that the live region needs to be present in the markup before the toast is generated or updated. If you dynamically generate both at the same time and inject them into the page, they will generally not be announced by assistive technologies.

API
#

Props #

NameDescriptionTypesDefault
ariaCloseLabel

The aria-label of the close button

String

"$t:close"

closeable

Provides the ability to close the component

Boolean

true

color

Color of the component (theme string or HEX string).

String

""

customClass

Applies custom class to the component

String

""

dangerouslyUseHtmlString

Sets the ability to use message as innerHtml. Please note that this can lead to XSS attacks, so make sure that you sanitize the message by yourself

Boolean

false

duration

Sets the duration of the notification display

Number

5000

icon

Sets the close icon

String

"close"

inline

Boolean

false

message

Notification message

String | Function

""

multiLine

Sets more space for the Toast component

Boolean

false

offsetX

Sets the X offset

Number

16

offsetY

Sets the Y offset

Number

16

onClick

Applies a function to use when clicked

Function

-

onClose

Applies a function to use when pressed a close button

Function

-

position

Sets the position of the notification

String

"top-right"

preset

Named preset combination of component props.

String

-

render

Render function to use a custom content

Function

-

role

Sets the role attribute.

String

-

title

Sets the title for the notification

String

""

Events #

NameDescription

onClick

Emits when toast is clicked

onClose

Emits when close button is clicked

Css Variables #

NameDefault Value
--va-toast-display flex
--va-toast-width 330px
--va-toast-padding 14px 26px 14px 13px
--va-toast-border-radius 8px
--va-toast-border-color transparent
--va-toast-border 1px solid var(--va-toast-border-color)
--va-toast-background-color var(--va-background-secondary)
--va-toast-box-shadow 0 2px 12px 0 var(--va-shadow)
--va-toast-transition opacity 0.3s, transform 0.3s, left 0.3s, right 0.3s, top 0.4s, bottom 0.3s
--va-toast-z-index calc(var(--va-z-index-teleport-overlay) + 100)
--va-toast-group-margin-left 13px
--va-toast-group-margin-right 8px
--va-toast-title-font-weight bold
--va-toast-title-font-size 1rem
--va-toast-title-color #303133
--va-toast-title-margin 0 0 6px
--va-toast-content-font-size 1rem
--va-toast-content-line-height 1.3125
--va-toast-content-padding-right 20px
--va-toast-icon-height 24px
--va-toast-icon-width 24px
--va-toast-icon-font-size 24px
--va-toast-close-icon-top 18px
--va-toast-close-icon-right 15px
--va-toast-close-icon-font-size 1rem

Change log #

FAQ
#

What is the difference between a component and a service?
#

The difference is in implementation. The service behaves more flexibly and it is easier to bind it to certain events than to render the whole element. Each prop you provide to the component you can provide to the service too by using the setting object.