HTML Veil
Drop a veil (or more than one) into HTML that obscures and prevents user interactions with content that is behind the veil (e.g. lightboxes and modals).
Installation
$ npm add html-veil
Basic Usage
var Veil = // orvar Veil = windowVeil var veil = veil // Drop the veil into the DOM.veil // Turn it on and off.
The default z-index
is 1000
. So position HTML elements accordingly. And note that elements behind the veil are obscured and do not respond to user interactions. If desired, the z-index
can be overridden by passing in an option when constructing a veil.
Basic Options
var lightbox_veil = html_id: 'lightbox-veil' color: 'rgba(255, 255, 255, 0.85)' z_index: 100
var veil = new Veil
(with no options) is the equivalent of:
var veil = html_id: 'veil' color: 'rgba(0, 0, 0, 0.7)' z_index: 1000
Even though color can easily be set via CSS, it is simpler to set color in JavaScript when no extra CSS styling is needed.
Full Interface
var Veil = // Get the default veil (see "Manipulating an Existing Veil")Veil // Destroy the default veil (see "Removing All Traces of a Veil")Veil // For a veil constructed with { html_id: 'lightbox-veil' }VeilVeil var veil = veil // Drop the veil into the DOMveilveilveilveilveil // Alternates between veil.activate() and veil.deactivate()veil // Only removes the HTML element from the DOM (i.e. veil.inject() would put it back)
Advanced Customization
In JavaScript:
var modal_veil = html_id: 'modal-veil' z_index: 999modal_veil var mobile_nav_veil = html_id: 'mobile-nav-veil' z_index: 998mobile_nav_veil
In CSS:
/*** All Veils*/ /*** Modal Veil*/ /*** Mobile Nav Veil*/
Setting no style rules is the equivalent of:
Manipulating an Existing Veil
index.js
In var Veil = // Default veilvar veil = veil var lightbox_veil = html_id: 'lightbox-veil' color: 'rgba(51, 51, 51, 0.8)'lightbox_veil
nav.js
In var Veil = // Default veilvar veil = Veilvar nav_icon = documentnav_icon && nav_icon var lightbox_veil = Veilvar lightbox_images = documentfor var i = 0 n = lightbox_imageslength - 1; i <= n; i++ var image = lightbox_imagesi if 'img' === imagetagName && imagesrc image { veil} { if lightbox_veil veil else // Shouldn't happen, but just to be safe: veil } { // ...} { // ...} { // ...} { // ...}
With modular JavaScript, it is not always obvious whether an expected veil has been constructed/injected yet. Requiring (as a dependency) the module that injects the expected veil may be necessary to guarantee that the veil exists, and can thus be toggled/(de)activated.
Removing All Traces of a Veil
var Veil = // Default veilvar veil = veil var lightbox_veil = html_id: 'lightbox-veil' lightbox_veil // Remove the HTML elements and all knowledge of the Veil objectsVeil // equivalent to Veil.destroy('veil')Veil // Remove the only remaining referencesveil = nulllightbox_veil = null // Let garbage collection take care of the rest