Skip to content

Toast

Simple notification pop-up.

Usage

Simple Usage

ts
import { toast } from '@privyid/persona/core'

toast('Far far away, behind the word.')

Advance Usage

ts
import { toast } from '@privyid/persona/core'

toast({
  text    : 'Far far away, behind the word.',
  duration: 5000,
  position: 'top-right'
})

Position

There available 6 position for toast: top-left, top-center, top-right, bottom-left, bottom-center, top-right default is bottom-left

Try it:

ts
import { toast } from '@privyid/persona/core'

// top-left
toast({
  text    : 'Far far away, behind the word.',
  position: 'top-left'
})

// top-center
toast({
  text    : 'Far far away, behind the word.',
  position: 'top-center'
})

// top-right
toast({
  text    : 'Far far away, behind the word.',
  position: 'top-right'
})

// bottom-left
toast({
  text    : 'Far far away, behind the word.',
  position: 'bottom-left'
})

// bottom-center
toast({
  text    : 'Far far away, behind the word.',
  position: 'bottom-center'
})

// bottom-right
toast({
  text    : 'Far far away, behind the word.',
  position: 'bottom-right'
})

Icon

It's possible to custom icon of toast, like color and icon-name. There are 6 icon colors available for toast: default, primary, info, success, warning and danger. Default icon color is toast icon color it's self.

preview
ts
import { toast } from '@privyid/persona/core'
import IconSuccess from '@privyid/persona-icon/vue/checkmark/24.vue'

toast({
  text     : 'Member added to the group',
  icon     : markRaw(IconSuccess),
  iconColor: 'success',
})

Dismissable

Dismiss button of toast can be show or hide by dismissable props. If dismissable set to true, dismiss button will show.

preview
ts
import { toast } from '@privyid/persona/core'

toast({
  text       : 'Lorem ipsum dolor sit amet.',
  variant    : 'filled',
  dismissable: false,
})

Actions

Add additional button using option actions.

preview
ts
toast({
  text   : 'Upload document successfully.',
  actions: [
    {
      text: 'Goto Doc',
      onClick (event) {
        // Do something
        // ...
        event.close()
      }
    }
  ],
})

Progress

Show loading progress using toast with .runProgress.

preview
ts
import { toast } from '@privyid/persona/core'

async function doUpload () {
  // ...
  const progressToast = await toast.withProgress('Uploading...')
  const response      = await axios.put('/endpoint/url', formData, {
    onUploadProgress (progressEvent) {
      const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total)

      progressToast.setProgress(`${progress}%`)
    }
  })

  progressToast.close()
  toast('Upload successfully')
}

Customization

You can add some custom class to Toast element via toastClass and toastAttrs.

ts
import { toast } from '@privyid/persona/core'

toast({
  text      : 'A wonderful serenity has taken.',
  toastClass: 'h-20',
  toastAttrs: { 'data-e2eid': 'toast-upload' },
})

API

toast

toast(options: ToastOptions): Promise<ToastInstance>

OptionsTypeDefaultDescription
textString-Toast text message (support markdown)
positionStringbottom-leftPosition for toast, valid value is top-left, top-center, top-right, bottom-left, bottom-center, top-right
durationNumber3000Duration for which the toast should be displayed. -1 to permanent toast
iconString or Component-Custom toast icon (will replace default icon)
iconColorString-Custom color of toast icon, valid value is default, primary, info, success, warning, danger
toastClassString-Add class to toast component
toastAttrsString-Add attribute to toast component
dismissableBooleantrueEnable dismiss button
actionsArray<ActionOption>-Add action button

In actions contains:

PropsTypeDefaultDescription
textString-Action button's text
attrsObject-Action additional attribute
onClickFunction-Action on-click handler

Shorthand

toast(text: string): Promise<ToastInstance>

.withProgress

.withProgress(options: ToastOption): Promise<ToastProgressInstance>

Shorthand

.withProgress(text: string, loadingText?: string): Promise<ToastProgressInstance>

See Also

Released under the MIT License.