项目作者: Daniel-Knights

项目描述 :
Lightweight toast-notification plugin for Vue 3 🍞
高级语言: TypeScript
项目地址: git://github.com/Daniel-Knights/vue-dk-toast.git
创建时间: 2020-10-01T11:11:21Z
项目社区:https://github.com/Daniel-Knights/vue-dk-toast

开源协议:

下载


vue-dk-toast

Lightweight toast-notification plugin for Vue 3 🍞

npm
vue

  • Lightweight ☁️
  • Customizable 🧰
  • Easy to use 🥷
  • Mobile-friendly 📱
  • Built-in TypeScript support 🔒
  • A11y compliant 🧑‍🦯

Demo \
CodePen Demo

  1. ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  2. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  3. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  4. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  5. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  6. ▒▒▒▒▒▒░░░░░░░░░░██░░░░░░░░░░░░░░██░░░░░░▒▒
  7. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  8. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  9. ▒▒▒▒▒▒░░░░░░░░░░░░██████░░░░░░░░▒▒
  10. ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒
  11. ██▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒██
  12. ██▒▒▒▒▒▒░░░░░░░░▒▒░░░░░░░░░░░░░░░░▒▒ ██
  13. ██ ▒▒▒▒▒▒░░░░░░░░░░░░░░░░▒▒░░░░░░░░▒▒ ██
  14. ██ ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒ ██
  15. ██ ▒▒▒▒▒▒░░▒▒░░░░░░░░░░░░░░░░░░▒▒░░▒▒ ██
  16. ██ ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒ ██
  17. ██ ▒▒▒▒▒▒░░░░░░░░░░░░▒▒░░░░░░░░░░░░▒▒ ██
  18. ██ ▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒ ██
  19. ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  20. ██ ██
  21. ██ ██
  22. ██ ██
  23. ██ ██
  24. ████ ████

Install

CLI

  1. npm i vue-dk-toast

or…

  1. yarn add vue-dk-toast

CDN

  1. <script src="https://unpkg.com/vue-dk-toast"></script>

It’s recommended you use a specific version number to guard against breaking changes and load the script faster:

  1. <script src="https://unpkg.com/vue-dk-toast@3.2.1"></script>

Import

CLI

  1. import { createApp } from 'vue';
  2. import DKToast from 'vue-dk-toast';
  3. import App from './App.vue';
  4. createApp(App).use(DKToast).mount('#app');

CDN

  1. const app = Vue.createApp({});
  2. app.use(DKToast);
  3. app.mount('#app');

Usage

Options API:

  1. this.$toast('Simple!');

or…

  1. @click="$toast('Simple!')"

Composition API:

  1. <script>
  2. import { inject } from 'vue';
  3. export default {
  4. setup() {
  5. const toast = inject('$toast');
  6. toast('Simple!');
  7. },
  8. };
  9. </script>

Options

Option Type Default Description
class String \ String[] None Custom CSS class to be added to every toast (alongside .dk__toast). Must be an array of strings for multiple classes.
disableClick Boolean false Disable toast removal on click.
duration Number 5000 Milliseconds before hiding toast.
pauseOnHover Boolean true Pause toast duration on mouseover, resume on mouseout.
max Number None Max number of toasts allowed on the page at any one time. Removes oldest existing toast first.
positionX String right Position of container on the X axis - 'left', 'right' or 'center'.
positionY String bottom Position of container on the Y axis - 'top', or 'bottom'.
styles Object None CSS key/value pairs - supports vendor prefixes.

EXAMPLE:

  1. createApp(App)
  2. .use(DKToast, {
  3. duration: 5000,
  4. pauseOnHover: true,
  5. positionY: 'bottom', // 'top' or 'bottom'
  6. positionX: 'right', // 'left', 'right' or 'center'
  7. disableClick: false,
  8. styles: {
  9. color: '#000',
  10. backgroundColor: '#fff',
  11. // Vendor prefixes also supported
  12. },
  13. class: 'custom-class', // Added to each toast,
  14. max: 10,
  15. })
  16. .mount('#app');

Local Options

Local options override global options where duplicate.

Option Type Default Description
class String \ String[] None CSS class to be added to this toast only (alongside .dk__toast and any globally set custom-class).
disableClick Boolean false Disable individual toast removal on click.
duration Number 5000 Milliseconds before hiding toast.
pauseOnHover Boolean true Pause toast duration on mouseover, resume on mouseout.
link { href: string, targetBlank?: boolean } none Turns the toast into an <a> element which has a href of the one provided and optional target="_blank".
positionX String right Position of container on the X axis - 'left', 'right' or 'center'.
positionY String bottom Position of container on the Y axis - 'top', or 'bottom'.
slotLeft String None Any valid HTML as a string. Displays left of text.
slotRight String None Any valid HTML as a string. Displays right of text.
styles Object None CSS key/value pairs. Supports vendor prefixes.
type String None Default helper class - success, error or passive.

EXAMPLE:

  1. this.$toast('Simple!', {
  2. duration: 1000,
  3. pauseOnHover: false,
  4. styles: {
  5. borderRadius: '25px',
  6. },
  7. link: {
  8. href: 'https://vue-dk-toast.netlify.app/',
  9. targetBlank: true,
  10. },
  11. // Any valid HTML, intended for icons
  12. slotLeft: '<i class="fa fa-user"></i>', // Add icon to left
  13. slotRight: '<i class="fa fa-thumbs-up"></i>', // Add icon to right
  14. class: 'local-class', // Added to this toast only
  15. type: 'success', // Default classes: 'success', 'error' and 'passive'
  16. positionX: 'center',
  17. positionY: 'top',
  18. disableClick: true,
  19. });

TypeScript Support

vue-dk-toast comes with it’s own built-in types for most cases, the exception being with the Composition API:

  1. <script lang="ts">
  2. import { defineComponent, inject } from 'vue';
  3. import type { Toast } from 'vue-dk-toast';
  4. export default defineComponent({
  5. setup() {
  6. const toast = inject<Toast>('$toast');
  7. if (toast) toast('Simple!');
  8. },
  9. });
  10. </script>

Security

For slotRight and slotLeft to work, it uses innerHTML to set the content. innerHTML is not secure as it adds the possibility for XSS attacks. If you set any user-inputted tags with these options, make sure you sanitise the string beforehand, else, adding something like <img src=x onerror="alert("XSS Attack!")" /> would run. You can use a library like DOMPurify to handle this for you.


Contributions welcome!