Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to use render function as message #36

Closed

Conversation

stafyniaksacha
Copy link

@stafyniaksacha stafyniaksacha commented Aug 12, 2022

Replace usage of v-html with a dynamic <component> so we can use components within messages:

// use render function
// see: https://vuejs.org/api/render-function.html#h
this.$toast.show(() => h('span', `Hey! I'm here`));
import Comp from './Comp.vue'

this.$toast.show(() => h(Comp, { title: `Hey! I'm here`, message: 'foo bar' }));
this.$toast.show(() => h(Comp, { myProp: 42 }, `Hey! I'm here`));

Inside the custom toast component, a toast object is provided and can be injected like:

<script setup>
// Comp.vue
import { inject } from 'vue'

const props = defineProps({
  title: {
    type: String,
    required: false,
  },
  message: {
    type: String,
    required: false,
  },
  myProp: {
    type: Number,
    required: false,
  },
})

const toast = inject('toast')
/*
toast.value.isActive: boolean
toast.value.isHovered: boolean
toast.value.duration: number
toast.value.timer: Timer instance
toast.value.click: function
toast.value.close: function
*/
</script>

<template>
  <div>
    <strong>{{ props.title }}</strong>
    <p>{{ props.message }}</p>
    <slot></slot>
    <button @click="toast.close">close</button>
    <button @click="toast.click">trigger click</button>
  </div>
</template>

You can test it with this in your package.json

 "devDependencies": {
    "@meforma/vue-toaster": "stafyniaksacha/vue-toaster#feat/render-function",
  }

close #33
close #32

@stafyniaksacha
Copy link
Author

@jprodrigues70 do you have time to review this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom CSS Calling this.$toast.clear() doesn't clear any toasts (using Vite)
1 participant