euvl / vue-notification

:icecream: Vue.js 2 library for showing notifications
http://vue-notification.yev.io/
MIT License
2.39k stars 213 forks source link

Tip: self-updating notifications #214

Closed davestewart closed 3 years ago

davestewart commented 3 years ago

Just thought I would share something I thought might need a feature request, but actually "just works".

I wanted to have a notification which could change its contents, so I could give the user some "actions" and display the results.

It turns out you can update the popup's content by simply updating the item data which is received in the slot scope:

popup

The bit that needed some thinking was, how to get the item in the slot to a function that could update its values.

Very roughly, it works like this...

In the notification call:

In the slot:

In the callback:

In code, this looks something like this:

let count = 0

function getText () {
  return `Click <a data-action="count">here</a> to update the count: ${count}`
}

notify({
  text: getText(),
  data: {
    callback (event, item, close) {
      if (event.target.getAttribute('data-action') === 'count') {
        count++
      }
      if (count > 10) {
        close()
      }
      item.text = getText()
    }
  }
})
<template slot="body" slot-scope="{ item, close }">
  <div v-html="item.text" @click.prevent.stop="event => item.data.callback(event, item, close)" />
</template>

Hopefully this will be useful to someone.

davestewart commented 3 years ago

If this was a feature request, it would be nice to have an object returned from notify() which could be updated as well.

github-actions[bot] commented 3 years ago

Stale issue message