+
Skip to content

Glideh/vue-flash3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vue Flash 3

A very simple and lightweight notification toolbox.

Getting started

  • Install the package
npm i vue-flash3
  • Include the component once somewhere on the DOM
<style>
@import 'vue-flash3';
</style>

<template>
  <Flash />
</template>

<script setup lang="ts">
import { Flash } from 'vue-flash3'
</script>
  • Show a message
import { instance as FlashI } from 'vue-flash3'

FlashI.add('That\'s a very nice success message')

First notification

Clicking on the notification makes it disappear. It can also disappear after a duration if specified

Message types

There are 3 builtin types of messages: success (the default), error, info

import { instance as FlashI } from 'vue-flash3'

FlashI.add('That\'s a very nice success message', 'success')
FlashI.add('Oups some error has occured !', 'error')
FlashI.add('Some info won\'t hurt', 'info')

Builtin notifications

However any other types can be defined simply by setting a color and an icon

Message duration

A third optionnal param is here to set the duration in milliseconds after which the notification disappears. The default is 0 meaning the notification should stay

import { instance as FlashI } from 'vue-flash3'

FlashI.add('That\'s a very nice success message', 'success', 5000) // Will be shown 5 seconds

Removing a message

The add() function returns the notification so it can be removed programmatically with remove()

<template>
  <button @click="remove">Remove</button>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { instance as FlashI } from 'vue-flash3'

const notif

onMounted(() => {
  notif = FlashI.add('Removable notif')
})

function remove() {
  FlashI.remove(notif)
}
</script>

Customizing

Colors

This default sass map can be overridden

$flash-colors: (
    info: #018495,
    error: #b13229,
    success: #429344,
);

Any type can be added and colors defined

Example

@use "vue-flash3/scss" with (
  $flash-colors: (
      toto: rgb(157, 32, 136),
  )
);
import { instance as FlashI } from 'vue-flash3'

FlashI.add('A Violet notification', 'toto')

Custom color

Icons

The icon part can contain anything by implementing the icons slot

Example

<template>
  <Flash>
    <template #icons="slotProps">
      <IconInfo v-if="slotProps.type === 'info'" />
    </template>
  </Flash>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { Flash, instance as FlashI } from 'vue-flash3'
import IconInfo from './icons/info-circle-solid.svg'

onMounted(() => {
    FlashI.add('Some info won\'t hurt', 'info')
})
</script>

We are using vite-svg-loader here with an icon from Font-Awesome

Custom icon

Icons can also be disabled by simply setting the noicon prop

<Flash :noicon="true" />

No icon

HTML

HTML content can be defined on the message (watch out for XSS)

FlashI.add('A Violet<br />notification', 'toto')

HTML content

Reactive content can also be used

import { onMounted, h } from 'vue'
import { Flash, instance as FlashI } from './flash'

function popFlash(e: Event) {
  e.stopPropagation()
  FlashI.remove(FlashI.notifications.value[0])
}

onMounted(() => {
  FlashI.add({
    render: () => h('div', [
      'This is a reactive message ',
      h('a', {
        onClick: popFlash
      }, 'now do something reactive')
    ]),
  }, 'success')
})

Here we are removing the first message of the stack at each click

Putting it all together

<style lang="scss">
@use "vue-flash3/scss" with (
  $flash-colors: (
      toto: rgb(120, 137, 19),
  )
);
</style>

<template>
  <Flash>
    <template #icons="slotProps">
      <span v-if="slotProps.type === 'toto'">&clubs;</span>
    </template>
  </Flash>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { Flash, instance as FlashI } from './flash'

onMounted(() => {
  FlashI.add('<b>Nice title</b><div>We are feeling lucky today</div>', 'toto')
})
</script>

All together

Develop

To run the dev env, a Vite Vue app is contained into the app directory, Docker can be used to have it installed out of the box:

docker compose up -d

The dev application is now accessible on http://localhost

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载