npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-veil

v1.0.0

Published

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)

Downloads

3

Readme

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 = require('html-veil')
// or
var Veil = window.Veil

var veil = new Veil
veil.inject() // Drop the veil into the DOM.
veil.toggle() // 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 = new 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 = new 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 = require('html-veil')

// Get the default veil (see "Manipulating an Existing Veil")
Veil.get()

// Destroy the default veil (see "Removing All Traces of a Veil")
Veil.destroy()

// For a veil constructed with { html_id: 'lightbox-veil' }
Veil.get('lightbox-veil')
Veil.destroy('lightbox-veil')

var veil = new Veil
veil.inject() // Drop the veil into the DOM
veil.is_activated()
veil.is_deactivated()
veil.activate()
veil.deactivate()
veil.toggle() // Alternates between veil.activate() and veil.deactivate()
veil.remove() // Only removes the HTML element from the DOM (i.e. veil.inject() would put it back)

Advanced Customization

In JavaScript:
var modal_veil = new Veil({
    html_id: 'modal-veil',
    z_index: 999,
})
modal_veil.inject()

var mobile_nav_veil = new Veil({
    html_id: 'mobile-nav-veil',
    z_index: 998,
})
mobile_nav_veil.inject()
In CSS:
/*
** All Veils
*/
.html-veil {
    transition-property: opacity, transform !important;
    transition-duration: 0.3s !important;
    transition-timing-function: ease !important;
}
.html-veil-activated {
    opacity: 0.7 !important;
}
.html-veil-changing_state {
    /* rules that apply while a veil is in the process of activating or deactivating */
}

/*
** Modal Veil
*/
#modal-veil {
    background-image: url('/images/bg-blur-texture.png');
    transform: scale(0.95); /* slight zoom-in effect */
}
.modal-veil-activated {
    opacity: 0.9 !important;
    transform: scale(1) !important;
}
.modal-veil-changing_state {}

/*
** Mobile Nav Veil
*/
#mobile-nav-veil {
    background-color: #333333;
    transform: translateX(100%); /* animate in from right */
}
.mobile-nav-veil-activated {
    transform: translateX(0) !important;
}
.mobile-nav-veil-changing_state {}

Setting no style rules is the equivalent of:

.html-veil {
    background-color: rgba(0, 0, 0, 0.7);
    position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: -1;
    opacity: 0;
    transition-property: opacity;
    transition-duration: 0.5s;
    transition-timing-function: linear;
    transition-delay: 0s;
}
.html-veil.html-veil-activated {
    z-index: 1000;
    opacity: 1;
}
.html-veil.html-veil-changing_state {
    z-index: 1000;
}

Manipulating an Existing Veil

In index.js
var Veil = require('html-veil')

// Default veil
var veil = new Veil
veil.inject()

var lightbox_veil = new Veil({
    html_id: 'lightbox-veil'
    color: 'rgba(51, 51, 51, 0.8)',
})
lightbox_veil.inject()
In nav.js
var Veil = require('html-veil')

// Default veil
var veil = Veil.get()
var nav_icon = document.getElementById('nav-super-menu-icon')
nav_icon && nav_icon.addEventListener('click', handle_nav_icon_click)

var lightbox_veil = Veil.get('lightbox-veil')
var lightbox_images = document.getElementsByClassName('lightbox')
for (var i = 0, n = lightbox_images.length - 1; i <= n; i++) {
    var image = lightbox_images[i]
    if ('img' === image.tagName.toLowerCase() && image.src) {
        image.addEventListener('click', handle_lightbox_image_click)
    }
}

function handle_nav_icon_click () {
    open_nav_menu()
    veil.toggle({
        deactivator: close_nav_menu,
        // ^ Optionally respond to when the user clicks on the veil itself
    })
}
function handle_lightbox_image_click (click_event) {
    if (lightbox_veil.is_deactivated()) {
        show_lightbox(click_event.target)
        veil.activate({
            deactivator: hide_lightbox,
        })
    } else {
        // Shouldn't happen, but just to be safe:
        hide_lightbox()
        veil.deactivate()
    }
}

function open_nav_menu () {
    // ...
}
function close_nav_menu () {
    // ...
}
function show_lightbox (image_element) {
    // ...
}
function hide_lightbox () {
    // ...
}

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 = require('html-veil')

// Default veil
var veil = new Veil
veil.inject()

var lightbox_veil = new Veil({ html_id: 'lightbox-veil' })
lightbox_veil.inject()

// Remove the HTML elements and all knowledge of the Veil objects
Veil.destroy() // equivalent to Veil.destroy('veil')
Veil.destroy('lightbox-veil')

// Remove the only remaining references
veil = null
lightbox_veil = null

// Let garbage collection take care of the rest