vanillaFlash
A lightweight vanilla JavaScript approach to flash messages.
What is vanillaFlash ?
vanillaFlash
is a library that allows to ease the use of flash messages.
It is also a vanilla JavaScript adaptation of my jQuery plugin jq-flash.
It allows you to flash messages in a very customizable manner.
What is the required structure ?
First you need to include the CSS and JS (included in dist
) files to your page.
This library works bests with a complete CSS reset, one stylesheet is provided for this purpose.
require
/import
shouldprovide the flash function, including it as a script tag will expose the flash
global variable.
Then you can either use a static or dynamic flash message :
A dynamic approach
Sometimes you need to flash a message "on the go", therefore you can use :
;; flash;flash;flash;
A static approach
Sometimes you need to flash a message via your back-end, therefore this is the default structure you need to use (at the very top of your <body>
tag):
✖<!-- a nice X--> This is a message
The flash message type is indicated by adding a class to the wrappingdiv
:
✖<!-- a nice X--> This is a message
How to embed ?
Since v2.0.0 you can use the special class flash-embed
to specify that the flash message isn't in the global scope (i.e. it is within another element than <body>
).
Since v2.0.1 you can now flash has the following signature : flash(type, message, context)
. You can provide the context
argument (which expects an HTMLElement
) to specify where to embed it. This works also with shorthands (e.g. flash.info("PINGAS", document.getElementById("flash_holder"))
).
The following is the basic HTML structure for an embed :
✖ Dem Messages [...]
How to customize vanillaFlash ?
vanillaFlash
uses a class system to design types (flash("type", "")
will use flash-type
as a CSS class for the div
).
Since v2.0.0, vanillaFlash
uses Sass, more specifically the SCSS syntax. You can find the source stylesheets under src
.
You can create a new type by doing the following:
;//webpack .flash-myType
You can also modify variables before importing sthe entire flash
stylesheet because it uses !default
flags everywhere :
;;//webpack
You can also add "components" inside of the div
, in that case you might need to do more modifications:
Associated with
✖ Dem Messages