Giter Site home page Giter Site logo

vue-notifyjs's Introduction

vue-notifyjs

Minimalist notification component for Vue 2.x

Why use it?

  • Small: 1.5kb (minified & gzipped), 4kb (minified)
  • Simple this.$notify({message:'My message'})
  • Has multiple themes
  • The animations can be customized through Vue transitions
  • Can be used both through npm and as a script tag

Demos:

3 themes supported

Install

yarn add vue-notifyjs

CDN JS:

CDN CSS:

Usage

<template>
  <notifications></notifications>
</template>

<script>
import Notify from 'vue-notifyjs'
Vue.use(Notify)

export default {
   methods: {
    addNotification() {
      this.$notify({
        message: 'Welcome',
        type: 'success'
      })
    }
  }
}
</script>
<!-- import styles -->
<style src="vue-notifyjs/themes/default.css"></style>

Note: <notifications></notifications> can be declared only once anywhere in your app, preferably in your root component so the notification component is alive inside any other components.

Notification options

You can set notification options in 3 ways

  1. Upon plugin initialization
import Notify from 'vue-notifyjs'
Vue.use(Notify, {type: 'primary', timeout: 2000})
  1. Dynamically via setOptions method
this.$notifications.setOptions({
  type: 'primary', 
  timeout: 2000,
  horizontalAlign: 'right',
  verticalAlign: 'top'
})
  1. When using $notify
this.$notify({
  message: 'Welcome',
  type: 'success'
})

Note: Options sent through this.$notify will override default options and will have higher priority than default options.

Props

Notifications

transitionName: {
 type:String,
 default:'list'
},
transitionMode: {
 type:String,
 default:'in-out'
},
overlap: {
 type: Boolean,
 default: false
}

Notification (passed through the object sent to $notify method)

props: {
  message: String,
  title: String,
  icon: String,
  verticalAlign: {
    type: String,
    default: 'top' // top | bottom
  },
  horizontalAlign: {
    type: String,
    default: 'center' // right | center | left
  },
  type: {
    type: String,
    default: 'info' // info | warning | danger | success | primary
  },
  timeout: {
    type: Number,
    default: 5000
  },
  timestamp: {
    type: Date,
    default: () => new Date()
  },
  component: {  //is rendered instead of notification message
    type: [Object, Function]
  },
  showClose: {
      type: Boolean,
      default: true
  },
  closeOnClick: {
      type: Boolean,
      default: true
  },
  clickHandler: Function,
},

Methods

clear() - Clears all current notifications

this.$notifications.clear();

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

vue-notifyjs's People

Contributors

amandiobm avatar cristijora avatar ffischer1984 avatar karambafe avatar mohannadnaj avatar pongoman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-notifyjs's Issues

Vuex Notification Handling

Is there a best practice for using this lib from Vuex? I am trying to understand how to call this.$notify for any notification that happens within the app.

Yarn lock on gitignore

I think that should be ignored because if some one has a different version os node, npm, or yarn that could end on a failure

CSS conflicts

First of all: thanks for this component!

This library introduces visual changes in other parts of our webapp. I would suggest to prefix all classes with notify- to avoid this. Especially the .close and .alert classes.

Demos do not work

JSfiddle console:

Failed to load resource: the server responded with a status of 404 () - vue-notifyjs.min.js 
Uncaught ReferenceError: vueNotifyjs is not defined at window.onload ((index):54)

Adding a link to the notification container

I'd like to make the notification container itself linkable through router-link, but I don't see a way to do that. I am just able to link the text. Is there someway to make that happen?

Plugin not loading

Hi,

I think you have an issue with your install method. ( latest release from unpkg )

--Hans

[ASK] How to call notify from js files

From the readme, to show the notification we can use

this.$notify({
  message: 'Welcome',
  type: 'success'
})

how to do this in .js files? e.g I have api.js and when the API call returns error, I want to show the notification.
Tried these but it doesn't work

import Vue from 'vue'
// some API call
Vue.prototype.$notify({message: 'some error', type: 'danger'})
/* Cannot read property 'notify' of undefined */
import Vue from 'vue'
import Notify from 'vue-notifyjs'
import '@/assets/css/themify-icons.css'
import 'vue-notifyjs/themes/default.css'

// some API call
Notify({message: 'some error', type: 'danger'})
/* vue_notifyjs__WEBPACK_IMPORTED_MODULE_2___default(...) is not a function */

Also I'm thinking of putting the initialization in a single js file (notification.js), which then imported to main.js

import Vue from 'vue'

import Notify from 'vue-notifyjs'
import '@/assets/css/themify-icons.css'
import 'vue-notifyjs/themes/default.css'

Vue.use(Notify, {
	timeout         : 3000,
	verticalAlign   : 'bottom',
	horizontalAlign : 'right',
})

Vue.prototype.$notif = {
	success: (message, title = 'Success') => {
		Notify({
			message,
			title,
			type : 'success',
			icon : 'fas fa-check-circle fa-fw',
		})
	},
	error: (message, title = 'Error') => {
		Notify({
			message,
			title,
			type : 'danger',
			icon : 'fas fa-times-circle fa-fw',
		})
	},
	warning: (message, title = 'Warning') => {
		Notify({
			message,
			title,
			type : 'warning',
			icon : 'fas fa-exclamation-circle fa-fw',
		})
	},
	info: (message, title = 'Info') => {
		Notify({
			message,
			title,
			type : 'info',
			icon : 'fas fa-info-circle fa-fw',
		})
	},
}

export default {}

So that in the template I can just call
this.$notif.success("message")

or in the Vuex modules / api.js
Vue.prototype.$notif.error(error.response.data)

Add icon

Hi,
great component! How I can add an icon to a notification?
Regards.

Catch the click event on the entire message

Hi there,
I have a question, is it possible to make a custom handler for the click event on the message?
I see that notification component is emitting a "close" event, but it stops at the notifications component which consumes it. Another possibility could be that I can catch a click event on the entire message inside the custom render function, but I don't see how I could achieve that...

(previous conversation: #9 (comment))

@cristijora thank you for the reply! So what do I do if I want to catch the click on the entire notification component? So basically catch the @close event on the notification. The example you provided was helpful, but only works on the button or the component that you define.

Need support for asynchronous notifications

notifyVue: async (verticalAlign, horizontalAlign) => {
      let color = Math.floor(Math.random() * 4 + 1)
      this.$notify.async({
        message: 'Hello',
        horizontalAlign: horizontalAlign,
        verticalAlign: verticalAlign,
        type: this.type[color]
      })
    }

will this work

SSR (Nuxt) support

Hello,

Do you plan on supporting Nuxt in the future ?

Great plugin, thank you 🙏

Failed to register component after updating to v0.4.3

The latest version rolled out seems to have an issue registering the component, as mentioned in the setup doc

index.js:57 Uncaught TypeError: Cannot use 'in' operator to search for 'default' in undefined
    at index.js:57
    at vue-notifyjs.min.js:6
    at vue-notifyjs.min.js:6
Unknown custom element: <notifications> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

Fiddles dont work

I got this error in the fiddles.

[Vue warn]: Unknown custom element: <notifications> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

(found in <Root>) vue.js:616:9
[Show/hide message details.] TypeError: this.$notify is not a function[Learn More]

Passing props to message component

I have encountered a use case where I want to pass props to the message component.
I haven't seen a way to do that currently.
I was able to work around like the following

[
           this.title !== undefined &&
             h("span", { class: "title" }, [
               h("b", null, [this.title, h("br", null, [])])
             ]),
           this.message !== undefined && this.message,
           this.component !== undefined &&
             h(this.component, { props: this.componentProps }, [])
         ]
       )

I can make a PR if this feature does not conflict with the direction and design decisions of the project.

Jsfiddle Error

I tried to run the Default theme jsfiddle demo.
I am getting the below error in the console :-
vue.js:634 [Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.
(found in )
as soon as I launch the fiddle.

If i click on any button in the demo, i can see the below error in the console :-
vue.js:634 [Vue warn]: Error in v-on handler: "TypeError: this.$notify is not a function"
(found in )
vue.js:1897 TypeError: this.$notify is not a function
at Vue.addNotification ((index):40)
at click (eval at createFunction (vue.js:11649), :3:137)
at invokeWithErrorHandling (vue.js:1863)
at HTMLButtonElement.invoker (vue.js:2188)
at HTMLButtonElement.original._wrapper (vue.js:7547)

I am running the latest version of chrome.

I am facing the same when i include the plugin in my vue application.

Custom html error

Hello,
I am using Vue-CLI 3 in which I have imported the VUE LBD template from Creative Tim. Everything seems to work fine except for notifications.

The following works:

      this.$notifications.notify({
        message: 'Welcome',
        icon: 'nc-icon nc-app',
        horizontalAlign,
        verticalAlign,
        type: this.type[color],
      });

while the following returns a "You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build." error...

      this.$notifications.notify({
        component: {
                template:'<span><a href="https://www.google.com" target="_blank"> Go to google</a></span>'
         },
        icon: 'nc-icon nc-app',
        horizontalAlign,
        verticalAlign,
        type: this.type[color],
      });

Any advice would be welcome

Sylvain

The vue-notifyjs Usage with Html5 !~~~~~

https://github.com/vue-then/scss-template/blob/main/example-git/dialog-notify/dialog/demo.html
https://github.com/vue-then/scss-template/blob/main/example-git/dialog-notify/dialog/vue-notify.umd.js

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- // Include vuejs -->
    <script type="text/javascript" src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
    <!-- // Include vuejs-dialog plugin -->
    <link href="./vuejs-dialog.min.css" rel="stylesheet">
    <!-- <link href="https://unpkg.com/vue-notifyjs/themes/default.css" rel="stylesheet"> -->
    <!-- <link href="https://unpkg.com/vue-notifyjs/themes/material.css" rel="stylesheet"> -->
    <link href="https://unpkg.com/vue-notifyjs/themes/now-ui.css" rel="stylesheet">
    <link rel="stylesheet" href="./dialog-login.css">
    <script type="text/javascript" src="./vuejs-dialog.min.js"></script>
    <script type="text/javascript" src="./vuejs-dialog-mixin.min.js"></script>
    <script type="text/javascript" src="./vue-notify.umd.js"></script>


    <title>Document</title>
    <style>
        .button {
            -moz-appearance: none;
            -webkit-appearance: none;
            -ms-flex-align: center;
            border: 1px solid #dbdbdb;
            border-radius: 3px;
            -webkit-box-shadow: none;
            box-shadow: none;
            font-size: 1rem;
            height: 2.25em;
            line-height: 1.5;
            position: relative;
            vertical-align: top;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            background-color: #fff;
            color: #363636;
            cursor: pointer;
            -ms-flex-pack: center;
            -webkit-box-pack: center;
            justify-content: center;
            padding: calc(.375em - 1px) 0.75em;
            text-align: center;
            white-space: nowrap;
        }

        main {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
            padding: 0 10px;
        }

        .vue-notification {
            padding: 10px;
            margin: 15px;

            font-size: 18px;
            font-family: "Noto Sans", sans-serif;

            color: #ffffff;
            background: #44A4FC;
            border-left: 5px solid #187FE7;
        }

        li {
            margin: 8px 0;
        }

        .has--list-items li:not(:last-child) {
            margin-bottom: 15px;
        }

        h2 {
            font-weight: bold;
            margin-bottom: 15px;
        }
    </style>
    <style>
       /* .list-leave-active for <2.1.8 */
        .vue-notifyjs .list-move { 
            transition: transform 0.3s, opacity 0.4s;
        }
        .vue-notifyjs .list-item {
            display: inline-block;
            margin-right: 10px;
        }
        .vue-notifyjs .list-enter-active {
            transition: opacity 0.4s;
        }
        .vue-notifyjs.list-leave-active {
            transition: opacity 0.4s;
        }
        .vue-notifyjs .list-enter {
            opacity: 0;
        }
        .vue-notifyjs .list-leave-to {
            opacity: 0;
        }
    </style>
</head>

<body>
    <div id="app">
        <notifications></notifications>
        <main>
            <div>
                <button class="btn btn-default btn-block" @click="addNotification('top', 'left')">Top Left</button>
                <button class="btn btn-default btn-block" @click="addNotification('top', 'center')">Top Center</button>
                <button class="btn btn-default btn-block" @click="addNotification('top', 'right')">Top Right</button>
                <button class="btn btn-default btn-block" @click="addNotification('bottom', 'left')">Bottom Left</button>
                <button class="btn btn-default btn-block" @click="addNotification('bottom', 'center')">Bottom Center</button>
                <button class="btn btn-default btn-block" @click="addNotification('bottom', 'right')">Bottom Right</button>
                <button class="btn btn-default btn-block" @click="addNotificationNoTimeout()">No timeout</button>
                <button class="btn btn-default btn-block" @click="addNotificationNoCloseOnClick()">No close on click</button>
                <button class="btn btn-default btn-block" @click="addNotificationNoCloseIcon()">No close icon</button>
                <button class="btn btn-default btn-block" @click="addNotificationWithClickHandler()">With click handler</button>
                <button class="btn btn-default btn-block" @click="removeNotifications()">Clear all notifications</button>
            </div>
            <section>
                <h2 class="title-case">{{ trans('用法作为一种方法')}}</h2>
                <hr />
                <h4>
                    <button class="button" @click="showAlertDialog()">{{ trans('警报对话框 - 一个按钮')
                            }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showPromptDialog()">{{ trans('提示 - 收集输入')
                            }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showHtmlDialog()">{{ trans('HTML对话框 - 风格/格式内容')
                            }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showBasicDialog()">{{ trans('基本确认 - 立即关闭')
                            }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showLoadingDialog()">{{ trans('加载对话框 - 与ajax有用')
                            }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showReversedDialog()">{{ trans('反转对话框 - 开关按钮')
                            }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showAnimationFadeDialog()">
                        {{ trans('淡化对话框 - 动画') }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showAnimationBounceDialog()">
                        {{ trans('反弹对话框 - 动画') }}
                    </button>
                </h4>
                <h4>
                    <button class="button" @click="showBasicDialogCloseClickOutside()">
                        {{ trans('单击外部对话框时关闭') }}
                    </button>
                </h4>
            </section>

            <section>
                <h2 class="title-case">{{ trans('用法作为指令')}}</h2>
                <hr />

                <h4>
                    <button class="button" v-confirm="trans('这是一个消息')">
                        {{ trans('给它一个字符串')
                            }} v-confirm="'{{ trans('这是一个消息') }}'"
                    </button>
                </h4>

                <h4>
                    <a href="http://example.com" v-confirm="trans('这将带你到 http://example.com. 请谨慎行事')">
                        {{ trans('转到example.com') }}
                    </a>
                </h4>

                <h4>
                    <button class="button" v-confirm="{
                        loader: true,
                        message: trans('此对话框也使用v-confirm指令触发。 它有OK和CANCEL回调'),
                        ok: clickOkHandler,
                        cancel: clickCancelHandler}">
                        {{ trans('给它一个对象') }}
                    </button>
                </h4>

                <form @submit.prevent="submitDemo1Form()" style="display: inline-block;">
                    <fieldset style="padding: 10px;">
                        <input v-model="forms.demo1.name" type="text" name="name" class="input-elem"
                            :placeholder="trans('你的名字')" style="margin-bottom: 10px;" />

                        <button class="button" type="reset" v-confirm="trans('重启')">
                            {{ trans('重启') }}
                        </button>
                        <button class="button" type="submit" v-confirm="trans('提交')">
                            {{ trans('提交') }}
                        </button>
                    </fieldset>
                </form>
            </section>

            <section>
                <h2>{{ trans('确认类型')}}</h2>
                <hr />

                <p>{{ trans('有时,您可能希望通过确保用户真正想要继续进行更严格')}}</p>
                <p>{{ trans('柔软和硬确认对话框有助于此。')}}</p>

                <h4>
                    <button class="button" @click="showSoftConfirmDialog()">
                        {{ trans('软确认 - 需要多次点击') }}
                    </button>
                </h4>

                <h4>
                    <button class="button" @click="showHardConfirmDialog()">
                        {{ trans('硬确认 - 需要验证文本') }}
                    </button>
                </h4>
            </section>

            <section>
                <h2>Extending the dialog</h2>
                <hr />

                <h4>
                    <button class="button" @click="showDialogWithCustomView()">Custom View/Component</button>
                </h4>
                <hr />
                <!-- 弹出框的自定义方法1 -->
                <button @click="showDialog">
                    show dialog
                </button>
                <!-- 弹出框的自定义方法1 -->
                <button @click="showDialogLogin">
                    show dialog login
                </button>

                <iframe width="100%" height="600"
                    src="https://jsfiddle.net/godofbrowser/pktL3mjb/embedded/js,html,css,result/dark/"
                    allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0"></iframe>
            </section>
        </main>
    </div>

    <!-- 弹出框的自定义方法1 -->
    <script id="custom-view-template" type="text/template">
        <p :class="options.customClass">
            {{ messageBody }} ----
        <br/>
        <ul>
            <li v-for="(item, index) in items" :key="item.name">
                <a href="#" @click="handleSelect(index)">
                {{ item.name }}
                <span v-if="item.count">({{ item.count }})</span>
            </a>
            </li>
        </ul>
        <br/>
        <a href="#" @click="handleDismiss()">cancel</a>
        <a href="#" @click="handleProceed()">Save</a>
        </p>
    </script>
    <!-- 弹出框的自定义方法1 -->
    <script id="dialog-login-template" type="text/template">
        <div :class="options.customClass">
            <div class="modal-head" @click="handleSelect()">
                <p>
                    {{ messageBody }}
                </p>
                <span class="popClose" @click="handleDismiss()">
                    <img src="./cha.png" alt="">
                </span>
            </div>
            <div class="modal-body">
                <div class="login-box">
                    <label>
                        <span>用户名:</span>
                        <input type="text" id="userNameID" placeholder="请输入用户名" />
                    </label>

                    <label><span>密&nbsp;&nbsp;码 : </span><input type="password" id="passwordID" placeholder="请输入密码" /></label>
                    <label class="dis-block">
                        <span>&nbsp; </span><a href="/forgetPwd.html" class="forgetPass">忘记密码?</a>
                        <a href="javascript:;" onclick='getPager("-","zhuce");' class="registLink">
                            免费<div class="registYello">注册账号</div>
                        </a>
                    </label>
                    <div class="thirdlogin">
                        <div class="thirdTit">第三方登录</div>
                        <img class="thirdQQ" src="./QQ_ICON.png" />
                    </div>
                </div>
            </div>
            <div class="modal-foot">
                <a class="enterBtn">登录</a>
            </div>
            <div class="clearboth"></div>
            <!-- <a href="#" @click="handleDismiss()">cancel</a>
            <a href="#" @click="handleProceed()">Save</a> -->
        </div>
    </script>
    <script src="./demo.js"></script>
</body>

</html>

The common.js to umd.js

https://github.com/vue-then/scss-template/blob/main/example-git/webpacks/package.json

  • npm run build

webpack.config.js

module.exports = {
  entry: './vue-notifyjs/dist/vue-notifyjs.common.js',
  output: {
    filename: './vue-notify.umd.js',
    // export to AMD, CommonJS, or window
    libraryTarget: 'umd',
    // the name exported to window
    library: 'notify'
  }
};

Avoid using object as key, use primitive instead

Great plugin, I enjoy using it. However, I installed a fresh copy directly from npm (npm install --save vue-notifyjs) and it fetched version 0.1.7. I was getting warnings in my browsers console that stated the key should not be a complex but a primitive type like string or integer.

Looking in the source code of the module in my node_modules/vue-notifyjs/dist I could find the culprit rather easy:

                        component: notification.component,
                        timestamp: notification.timestamp
                    },
                    key: notification, on: {
                        'close': function close() {
                            return _this.removeNotification(index);
                        }
                    }

I've manually changed key: notification to key: index and the error was gone. Now the funky bits click in: I forked your repo on GitHub and tried to create a pull request. But then I saw that your source code already contains a fix: You use the timestamp value of the notification to create a unique key. Very nice, but why is this not included on the NPM repo? I have never published anything on NPM so I can only assume that maybe you've uploaded the wrong version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.